Skip to content

Commit 5339e4f

Browse files
authored
Adding custom endpoint (#24)
* Adding custom endpoint * cs
1 parent f6940bd commit 5339e4f

File tree

5 files changed

+89
-20
lines changed

5 files changed

+89
-20
lines changed

src/Api/Checkout.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,12 @@ public function updateAddress(int $cartId, array $shippingAddress, bool $differe
8181
*
8282
* @return ResponseInterface|void
8383
*/
84-
public function updatePaymentMethod(int $cartId, string $paymentMethodCode)
84+
public function updatePaymentMethod(int $cartId, array $params = [])
8585
{
8686
if (empty($cartId)) {
8787
throw new InvalidArgumentException('Cart id cannot be empty');
8888
}
8989

90-
if (empty($paymentMethodCode)) {
91-
throw new InvalidArgumentException('Payment method code cannot be empty');
92-
}
93-
94-
$params = [
95-
'payments' => [
96-
[
97-
'method' => $paymentMethodCode,
98-
],
99-
],
100-
];
101-
10290
$response = $this->httpPut('/api/v1/checkouts/select-payment/'.$cartId, $params);
10391
if (!$this->hydrator) {
10492
return $response;

src/Api/Custom.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This software may be modified and distributed under the terms
7+
* of the MIT license. See the LICENSE file for details.
8+
*/
9+
10+
namespace FAPI\Sylius\Api;
11+
12+
/**
13+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
14+
*/
15+
final class Custom extends HttpApi
16+
{
17+
public function get(string $path, array $params = [], array $requestHeaders = [], string $class = '')
18+
{
19+
$response = parent::httpGet($path, $params, $requestHeaders);
20+
if (!$this->hydrator) {
21+
return $response;
22+
}
23+
24+
return $this->hydrator->hydrate($response, $class);
25+
}
26+
27+
public function post(string $path, array $params = [], array $requestHeaders = [], string $class = '')
28+
{
29+
$response = parent::httpPost($path, $params, $requestHeaders);
30+
if (!$this->hydrator) {
31+
return $response;
32+
}
33+
34+
return $this->hydrator->hydrate($response, $class);
35+
}
36+
37+
public function postRaw(string $path, $body, array $requestHeaders = [], string $class = '')
38+
{
39+
$response = parent::httpPostRaw($path, $body, $requestHeaders);
40+
if (!$this->hydrator) {
41+
return $response;
42+
}
43+
44+
return $this->hydrator->hydrate($response, $class);
45+
}
46+
47+
public function put(string $path, array $params = [], array $requestHeaders = [], string $class = '')
48+
{
49+
$response = parent::httpPut($path, $params, $requestHeaders);
50+
if (!$this->hydrator) {
51+
return $response;
52+
}
53+
54+
return $this->hydrator->hydrate($response, $class);
55+
}
56+
57+
public function patch(string $path, array $params = [], array $requestHeaders = [], string $class = '')
58+
{
59+
$response = parent::httpPatch($path, $params, $requestHeaders);
60+
if (!$this->hydrator) {
61+
return $response;
62+
}
63+
64+
return $this->hydrator->hydrate($response, $class);
65+
}
66+
67+
public function delete(string $path, array $params = [], array $requestHeaders = [], string $class = '')
68+
{
69+
$response = parent::httpDelete($path, $params, $requestHeaders);
70+
if (!$this->hydrator) {
71+
return $response;
72+
}
73+
74+
return $this->hydrator->hydrate($response, $class);
75+
}
76+
}

src/Api/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function create(string $productCode, array $params = [])
7575
*
7676
* @throws Exception
7777
*
78-
* @return void|ResponseInterface
78+
* @return ResponseInterface|void
7979
*/
8080
public function update(string $productCode, array $params = [])
8181
{

src/Api/Product/Variant.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getAll(string $productCode, array $params = [])
5050
*/
5151
public function get(string $productCode, string $code)
5252
{
53-
$response = $this->httpGet(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code));
53+
$response = $this->httpGet(\sprintf('/api/v1/products/%s/variants/%s', $productCode, $code));
5454
if (!$this->hydrator) {
5555
return $response;
5656
}
@@ -73,7 +73,7 @@ public function get(string $productCode, string $code)
7373
public function create(string $productCode, string $code, array $params = [])
7474
{
7575
$params['code'] = $code;
76-
$response = $this->httpPost(sprintf('/api/v1/products/%s/variants/', $productCode), $params);
76+
$response = $this->httpPost(\sprintf('/api/v1/products/%s/variants/', $productCode), $params);
7777
if (!$this->hydrator) {
7878
return $response;
7979
}
@@ -93,11 +93,11 @@ public function create(string $productCode, string $code, array $params = [])
9393
*
9494
* @throws Exception
9595
*
96-
* @return void|ResponseInterface
96+
* @return ResponseInterface|void
9797
*/
9898
public function update(string $productCode, string $code, array $params = [])
9999
{
100-
$response = $this->httpPatch(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code), $params);
100+
$response = $this->httpPatch(\sprintf('/api/v1/products/%s/variants/%s', $productCode, $code), $params);
101101
if (!$this->hydrator) {
102102
return $response;
103103
}
@@ -113,11 +113,11 @@ public function update(string $productCode, string $code, array $params = [])
113113
*
114114
* @throws Exception
115115
*
116-
* @return void|ResponseInterface
116+
* @return ResponseInterface|void
117117
*/
118118
public function delete(string $productCode, string $code)
119119
{
120-
$response = $this->httpDelete(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code));
120+
$response = $this->httpDelete(\sprintf('/api/v1/products/%s/variants/%s', $productCode, $code));
121121
if (!$this->hydrator) {
122122
return $response;
123123
}

src/SyliusClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public function getAccessToken(): ?string
116116
return $this->authenticator->getAccessToken();
117117
}
118118

119+
public function custom(Hydrator $hydrator = null): Api\Custom
120+
{
121+
return new Api\Custom($this->getHttpClient(), $hydrator ?? $this->hydrator, $this->requestBuilder);
122+
}
123+
119124
public function customer(): Api\Customer
120125
{
121126
return new Api\Customer($this->getHttpClient(), $this->hydrator, $this->requestBuilder);

0 commit comments

Comments
 (0)