Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 526d0ba

Browse files
committed
feat(Store): support in-store requests
1 parent a166426 commit 526d0ba

27 files changed

+235
-12
lines changed

src/Core/Request/Carts/CartByCustomerIdGetRequest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace Commercetools\Core\Request\Carts;
77

8+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
9+
use Commercetools\Core\Request\InStores\InStoreTrait;
810
use Psr\Http\Message\ResponseInterface;
911
use Commercetools\Core\Client\HttpMethod;
1012
use Commercetools\Core\Client\HttpRequest;
@@ -21,10 +23,12 @@
2123
* @link https://docs.commercetools.com/http-api-projects-carts.html#get-cart-by-customer-id
2224
* @method Cart mapResponse(ApiResponseInterface $response)
2325
* @method Cart mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
26+
* @method CartByCustomerIdGetRequest|InStoreRequestDecorator inStore($storeKey)
2427
*/
2528
class CartByCustomerIdGetRequest extends AbstractApiRequest
2629
{
2730
use CustomerIdTrait;
31+
use InStoreTrait;
2832

2933
protected $resultClass = Cart::class;
3034

src/Core/Request/Carts/CartByIdGetRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Commercetools\Core\Model\Cart\Cart;
99
use Commercetools\Core\Model\Common\Context;
1010
use Commercetools\Core\Request\AbstractByIdGetRequest;
11+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
12+
use Commercetools\Core\Request\InStores\InStoreTrait;
1113
use Commercetools\Core\Response\ApiResponseInterface;
1214
use Commercetools\Core\Model\MapperInterface;
1315

@@ -16,9 +18,12 @@
1618
* @link https://docs.commercetools.com/http-api-projects-carts.html#get-cart-by-id
1719
* @method Cart mapResponse(ApiResponseInterface $response)
1820
* @method Cart mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
21+
* @method CartByIdGetRequest|InStoreRequestDecorator inStore($storeKey)
1922
*/
2023
class CartByIdGetRequest extends AbstractByIdGetRequest
2124
{
25+
use InStoreTrait;
26+
2227
protected $resultClass = Cart::class;
2328

2429
/**

src/Core/Request/Carts/CartCreateRequest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
use Commercetools\Core\Model\Cart\CartDraft;
99
use Commercetools\Core\Model\Common\Context;
10-
use Commercetools\Core\Request\AbstractCreateRequest;
1110
use Commercetools\Core\Model\Cart\Cart;
11+
use Commercetools\Core\Request\AbstractCreateRequest;
12+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
13+
use Commercetools\Core\Request\InStores\InStoreTrait;
1214
use Commercetools\Core\Response\ApiResponseInterface;
1315
use Commercetools\Core\Model\MapperInterface;
1416

@@ -17,9 +19,12 @@
1719
* @link https://docs.commercetools.com/http-api-projects-carts.html#create-cart
1820
* @method Cart mapResponse(ApiResponseInterface $response)
1921
* @method Cart mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
22+
* @method CartCreateRequest|InStoreRequestDecorator inStore($storeKey)
2023
*/
2124
class CartCreateRequest extends AbstractCreateRequest
2225
{
26+
use InStoreTrait;
27+
2328
protected $resultClass = Cart::class;
2429

2530
/**
@@ -34,7 +39,7 @@ public function __construct(CartDraft $cartDraft, Context $context = null)
3439
/**
3540
* @param CartDraft $cartDraft
3641
* @param Context $context
37-
* @return static
42+
* @return CartCreateRequest
3843
*/
3944
public static function ofDraft(CartDraft $cartDraft, Context $context = null)
4045
{

src/Core/Request/Carts/CartDeleteRequest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Commercetools\Core\Request\AbstractDeleteRequest;
1111
use Commercetools\Core\Model\Cart\Cart;
1212
use Commercetools\Core\Request\DataErasureTrait;
13+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
14+
use Commercetools\Core\Request\InStores\InStoreTrait;
1315
use Commercetools\Core\Response\ApiResponseInterface;
1416
use Commercetools\Core\Model\MapperInterface;
1517

@@ -18,10 +20,12 @@
1820
* @link https://docs.commercetools.com/http-api-projects-carts.html#delete-cart
1921
* @method Cart mapResponse(ApiResponseInterface $response)
2022
* @method Cart mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
23+
* @method CartDeleteRequest|InStoreRequestDecorator inStore($storeKey)
2124
*/
2225
class CartDeleteRequest extends AbstractDeleteRequest
2326
{
2427
use DataErasureTrait;
28+
use InStoreTrait;
2529

2630
protected $resultClass = Cart::class;
2731

src/Core/Request/Carts/CartQueryRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Commercetools\Core\Model\Cart\CartCollection;
99
use Commercetools\Core\Model\Common\Context;
1010
use Commercetools\Core\Request\AbstractQueryRequest;
11+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
12+
use Commercetools\Core\Request\InStores\InStoreTrait;
1113
use Commercetools\Core\Response\ApiResponseInterface;
1214
use Commercetools\Core\Model\MapperInterface;
1315

@@ -16,9 +18,12 @@
1618
* @link https://docs.commercetools.com/http-api-projects-carts.html#query-carts
1719
* @method CartCollection mapResponse(ApiResponseInterface $response)
1820
* @method CartCollection mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
21+
* @method CartQueryRequest|InStoreRequestDecorator inStore($storeKey)
1922
*/
2023
class CartQueryRequest extends AbstractQueryRequest
2124
{
25+
use InStoreTrait;
26+
2227
protected $resultClass = CartCollection::class;
2328

2429
/**

src/Core/Request/Carts/CartUpdateRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Commercetools\Core\Model\Cart\Cart;
99
use Commercetools\Core\Model\Common\Context;
1010
use Commercetools\Core\Request\AbstractUpdateRequest;
11+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
12+
use Commercetools\Core\Request\InStores\InStoreTrait;
1113
use Commercetools\Core\Response\ApiResponseInterface;
1214
use Commercetools\Core\Model\MapperInterface;
1315

@@ -16,9 +18,12 @@
1618
* @link https://docs.commercetools.com/http-api-projects-carts.html#update-cart
1719
* @method Cart mapResponse(ApiResponseInterface $response)
1820
* @method Cart mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
21+
* @method CartUpdateRequest|InStoreRequestDecorator inStore($storeKey)
1922
*/
2023
class CartUpdateRequest extends AbstractUpdateRequest
2124
{
25+
use InStoreTrait;
26+
2227
protected $resultClass = Cart::class;
2328

2429
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\InStores;
6+
7+
use Commercetools\Core\Client\JsonEndpoint;
8+
9+
/**
10+
* @package Commercetools\Core\Request\InStores
11+
*/
12+
class InStoreEndpoint
13+
{
14+
/**
15+
* @param string $storeKey
16+
* @return JsonEndpoint
17+
*/
18+
public static function endpoint($storeKey)
19+
{
20+
return new JsonEndpoint('in-store/key=' . $storeKey . '/');
21+
}
22+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
6+
namespace Commercetools\Core\Request\InStores;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\MapperInterface;
10+
use Commercetools\Core\Request\ClientRequestInterface;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Psr\Http\Message\RequestInterface;
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class InStoreRequestDecorator implements ClientRequestInterface
16+
{
17+
18+
private $request;
19+
private $endpoint;
20+
21+
/**
22+
* InStoreDecorator constructor.
23+
* @param string $storeKey
24+
* @param ClientRequestInterface $request
25+
*/
26+
public function __construct($storeKey, ClientRequestInterface $request)
27+
{
28+
//
29+
$this->request = $request;
30+
$this->endpoint = InStoreEndpoint::endpoint($storeKey);
31+
}
32+
33+
public function getIdentifier()
34+
{
35+
return $this->request->getIdentifier();
36+
}
37+
38+
public function setIdentifier($identifier)
39+
{
40+
return $this->request->setIdentifier($identifier);
41+
}
42+
43+
public function httpRequest()
44+
{
45+
return $this->decorateUri($this->request->httpRequest());
46+
}
47+
48+
private function decorateUri(RequestInterface $request)
49+
{
50+
return $request->withUri($request->getUri()->withPath($this->endpoint . $request->getUri()->getPath()));
51+
}
52+
53+
public function buildResponse(ResponseInterface $response)
54+
{
55+
return $this->request->buildResponse($response);
56+
}
57+
58+
public function mapResult(array $result, Context $context = null)
59+
{
60+
return $this->request->mapResult($result, $context);
61+
}
62+
63+
public function mapResponse(ApiResponseInterface $response)
64+
{
65+
return $this->request->mapResponse($response);
66+
}
67+
68+
public function map(array $data, Context $context = null, MapperInterface $mapper = null)
69+
{
70+
return $this->request->map($data, $context, $mapper);
71+
}
72+
73+
public function mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
74+
{
75+
return $this->request->mapFromResponse($response, $mapper);
76+
}
77+
78+
public static function ofStoreAndRequest($storeKey, ClientRequestInterface $request)
79+
{
80+
return new static($storeKey, $request);
81+
}
82+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\InStores;
6+
7+
trait InStoreTrait
8+
{
9+
/**
10+
* @param $storeKey
11+
* @return InStoreRequestDecorator
12+
*/
13+
public function inStore($storeKey)
14+
{
15+
return new InStoreRequestDecorator($storeKey, $this);
16+
}
17+
}

src/Core/Request/Me/MeActiveCartRequest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use Commercetools\Core\Client\HttpRequest;
1010
use Commercetools\Core\Model\Common\Context;
1111
use Commercetools\Core\Request\AbstractApiRequest;
12-
use Commercetools\Core\Request\AbstractByIdGetRequest;
1312
use Commercetools\Core\Model\Cart\Cart;
13+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
14+
use Commercetools\Core\Request\InStores\InStoreTrait;
1415
use Commercetools\Core\Response\ApiResponseInterface;
1516
use Commercetools\Core\Response\ResourceResponse;
1617
use Psr\Http\Message\ResponseInterface;
@@ -21,9 +22,12 @@
2122
* @link https://docs.commercetools.com/http-api-projects-me-carts.html#get-active-cart
2223
* @method Cart mapResponse(ApiResponseInterface $response)
2324
* @method Cart mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
25+
* @method MeActiveCartRequest|InStoreRequestDecorator inStore($storeKey)
2426
*/
2527
class MeActiveCartRequest extends AbstractApiRequest
2628
{
29+
use InStoreTrait;
30+
2731
protected $resultClass = Cart::class;
2832

2933
/**

0 commit comments

Comments
 (0)