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

Commit a166426

Browse files
committed
feat(Store): support Store model
1 parent 53ac8b0 commit a166426

24 files changed

+880
-69
lines changed

src/Core/Builder/Request/RequestBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ public function states()
215215
return new StateRequestBuilder();
216216
}
217217

218+
/**
219+
* @return StoreRequestBuilder
220+
*/
221+
public function stores()
222+
{
223+
return new StoreRequestBuilder();
224+
}
225+
218226
/**
219227
* @return SubscriptionRequestBuilder
220228
*/
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
// phpcs:disable Generic.Files.LineLength
3+
namespace Commercetools\Core\Builder\Request;
4+
5+
use Commercetools\Core\Request\Stores\StoreByIdGetRequest;
6+
use Commercetools\Core\Request\Stores\StoreByKeyGetRequest;
7+
use Commercetools\Core\Request\Stores\StoreCreateRequest;
8+
use Commercetools\Core\Model\Store\StoreDraft;
9+
use Commercetools\Core\Request\Stores\StoreDeleteByKeyRequest;
10+
use Commercetools\Core\Model\Store\Store;
11+
use Commercetools\Core\Request\Stores\StoreDeleteRequest;
12+
use Commercetools\Core\Request\Stores\StoreQueryRequest;
13+
use Commercetools\Core\Request\Stores\StoreUpdateByKeyRequest;
14+
use Commercetools\Core\Request\Stores\StoreUpdateRequest;
15+
16+
class StoreRequestBuilder
17+
{
18+
19+
/**
20+
* @link https://docs.commercetools.com/http-api-projects-stores#get-a-store-by-id
21+
* @param string $id
22+
* @return StoreByIdGetRequest
23+
*/
24+
public function getById($id)
25+
{
26+
$request = StoreByIdGetRequest::ofId($id);
27+
return $request;
28+
}
29+
30+
/**
31+
* @link https://docs.commercetools.com/http-api-projects-stores#get-a-store-by-key
32+
* @param string $key
33+
* @return StoreByKeyGetRequest
34+
*/
35+
public function getByKey($key)
36+
{
37+
$request = StoreByKeyGetRequest::ofKey($key);
38+
return $request;
39+
}
40+
41+
/**
42+
* @link https://docs.commercetools.com/http-api-projects-stores#create-a-store
43+
* @param StoreDraft $store
44+
* @return StoreCreateRequest
45+
*/
46+
public function create(StoreDraft $store)
47+
{
48+
$request = StoreCreateRequest::ofDraft($store);
49+
return $request;
50+
}
51+
52+
/**
53+
* @link https://docs.commercetools.com/http-api-projects-stores#delete-store-by-key
54+
* @param Store $store
55+
* @return StoreDeleteByKeyRequest
56+
*/
57+
public function deleteByKey(Store $store)
58+
{
59+
$request = StoreDeleteByKeyRequest::ofKeyAndVersion($store->getKey(), $store->getVersion());
60+
return $request;
61+
}
62+
63+
/**
64+
* @link https://docs.commercetools.com/http-api-projects-stores#delete-store-by-id
65+
* @param Store $store
66+
* @return StoreDeleteRequest
67+
*/
68+
public function delete(Store $store)
69+
{
70+
$request = StoreDeleteRequest::ofIdAndVersion($store->getId(), $store->getVersion());
71+
return $request;
72+
}
73+
74+
/**
75+
* @link https://docs.commercetools.com/http-api-projects-stores#query-stores
76+
*
77+
* @return StoreQueryRequest
78+
*/
79+
public function query()
80+
{
81+
$request = StoreQueryRequest::of();
82+
return $request;
83+
}
84+
85+
/**
86+
* @link https://docs.commercetools.com/http-api-projects-stores#update-store-by-key
87+
* @param Store $store
88+
* @return StoreUpdateByKeyRequest
89+
*/
90+
public function updateByKey(Store $store)
91+
{
92+
$request = StoreUpdateByKeyRequest::ofKeyAndVersion($store->getKey(), $store->getVersion());
93+
return $request;
94+
}
95+
96+
/**
97+
* @link https://docs.commercetools.com/http-api-projects-stores#update-store-by-id
98+
* @param Store $store
99+
* @return StoreUpdateRequest
100+
*/
101+
public function update(Store $store)
102+
{
103+
$request = StoreUpdateRequest::ofIdAndVersion($store->getId(), $store->getVersion());
104+
return $request;
105+
}
106+
107+
/**
108+
* @return StoreRequestBuilder
109+
*/
110+
public function of()
111+
{
112+
return new self();
113+
}
114+
}

src/Core/Builder/Update/ActionBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ public function states()
180180
return new StatesActionBuilder();
181181
}
182182

183+
/**
184+
* @return StoresActionBuilder
185+
*/
186+
public function stores()
187+
{
188+
return new StoresActionBuilder();
189+
}
190+
183191
/**
184192
* @return SubscriptionsActionBuilder
185193
*/
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Builder\Update;
4+
5+
use Commercetools\Core\Error\InvalidArgumentException;
6+
use Commercetools\Core\Request\AbstractAction;
7+
use Commercetools\Core\Request\Stores\Command\StoreSetNameAction;
8+
9+
class StoresActionBuilder
10+
{
11+
private $actions = [];
12+
13+
/**
14+
* @link https://docs.commercetools.com/http-api-projects-stores#set-name
15+
* @param StoreSetNameAction|callable $action
16+
* @return $this
17+
*/
18+
public function setName($action = null)
19+
{
20+
$this->addAction($this->resolveAction(StoreSetNameAction::class, $action));
21+
return $this;
22+
}
23+
24+
/**
25+
* @return StoresActionBuilder
26+
*/
27+
public static function of()
28+
{
29+
return new self();
30+
}
31+
32+
/**
33+
* @param $class
34+
* @param $action
35+
* @return AbstractAction
36+
* @throws InvalidArgumentException
37+
*/
38+
private function resolveAction($class, $action = null)
39+
{
40+
if (is_null($action) || is_callable($action)) {
41+
$callback = $action;
42+
$emptyAction = $class::of();
43+
$action = $this->callback($emptyAction, $callback);
44+
}
45+
if ($action instanceof $class) {
46+
return $action;
47+
}
48+
throw new InvalidArgumentException(
49+
sprintf('Expected method to be called with or callable to return %s', $class)
50+
);
51+
}
52+
53+
/**
54+
* @param $action
55+
* @param callable $callback
56+
* @return AbstractAction
57+
*/
58+
private function callback($action, callable $callback = null)
59+
{
60+
if (!is_null($callback)) {
61+
$action = $callback($action);
62+
}
63+
return $action;
64+
}
65+
66+
/**
67+
* @param AbstractAction $action
68+
* @return $this;
69+
*/
70+
public function addAction(AbstractAction $action)
71+
{
72+
$this->actions[] = $action;
73+
return $this;
74+
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public function getActions()
80+
{
81+
return $this->actions;
82+
}
83+
84+
85+
/**
86+
* @param array $actions
87+
* @return $this
88+
*/
89+
public function setActions(array $actions)
90+
{
91+
$this->actions = $actions;
92+
return $this;
93+
}
94+
}

src/Core/Model/Cart/Cart.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Commercetools\Core\Model\CustomField\CustomFieldObject;
1717
use Commercetools\Core\Model\Common\DateTimeDecorator;
1818
use Commercetools\Core\Model\Payment\PaymentInfo;
19+
use Commercetools\Core\Model\Store\StoreReference;
1920
use DateTime;
2021

2122
/**
@@ -80,6 +81,8 @@
8081
* @method Cart setShippingRateInput(ShippingRateInput $shippingRateInput = null)
8182
* @method AddressCollection getItemShippingAddresses()
8283
* @method Cart setItemShippingAddresses(AddressCollection $itemShippingAddresses = null)
84+
* @method StoreReference getStore()
85+
* @method Cart setStore(StoreReference $store = null)
8386
* @method CartReference getReference()
8487
*/
8588
class Cart extends Resource
@@ -139,6 +142,7 @@ public function fieldDefinitions()
139142
'taxCalculationMode' => [static::TYPE => 'string'],
140143
'shippingRateInput' => [static::TYPE => ShippingRateInput::class],
141144
'itemShippingAddresses' => [static::TYPE => AddressCollection::class],
145+
'store' => [static::TYPE => StoreReference::class],
142146
];
143147
}
144148

src/Core/Model/Cart/CartDraft.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
1414
use Commercetools\Core\Model\Common\Address;
1515
use Commercetools\Core\Model\ShippingMethod\ShippingMethodReference;
16+
use Commercetools\Core\Model\Store\StoreReference;
1617
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
1718

1819
/**
@@ -61,6 +62,8 @@
6162
* @method CartDraft setShippingRateInput(ShippingRateInputDraft $shippingRateInput = null)
6263
* @method AddressCollection getItemShippingAddresses()
6364
* @method CartDraft setItemShippingAddresses(AddressCollection $itemShippingAddresses = null)
65+
* @method StoreReference getStore()
66+
* @method CartDraft setStore(StoreReference $store = null)
6467
*/
6568
class CartDraft extends JsonObject
6669
{
@@ -91,6 +94,7 @@ public function fieldDefinitions()
9194
'externalTaxRateForShippingMethod' => [static::TYPE => ExternalTaxRateDraft::class],
9295
'shippingRateInput' => [static::TYPE => ShippingRateInputDraft::class],
9396
'itemShippingAddresses' => [static::TYPE => AddressCollection::class],
97+
'store' => [static::TYPE => StoreReference::class],
9498
];
9599
}
96100

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Model\Common;
6+
7+
use Commercetools\Core\Model\Cart\CartReference;
8+
use Commercetools\Core\Model\CartDiscount\CartDiscountReference;
9+
use Commercetools\Core\Model\Category\CategoryReference;
10+
use Commercetools\Core\Model\Channel\ChannelReference;
11+
use Commercetools\Core\Model\Customer\CustomerReference;
12+
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
13+
use Commercetools\Core\Model\CustomObject\CustomObjectReference;
14+
use Commercetools\Core\Model\DiscountCode\DiscountCodeReference;
15+
use Commercetools\Core\Model\Order\OrderReference;
16+
use Commercetools\Core\Model\OrderEdit\OrderEditReference;
17+
use Commercetools\Core\Model\Payment\PaymentReference;
18+
use Commercetools\Core\Model\Product\ProductReference;
19+
use Commercetools\Core\Model\ProductDiscount\ProductDiscountReference;
20+
use Commercetools\Core\Model\ProductType\ProductTypeReference;
21+
use Commercetools\Core\Model\ShippingMethod\ShippingMethodReference;
22+
use Commercetools\Core\Model\ShoppingList\ShoppingListReference;
23+
use Commercetools\Core\Model\State\StateReference;
24+
use Commercetools\Core\Model\Store\StoreReference;
25+
use Commercetools\Core\Model\TaxCategory\TaxCategoryReference;
26+
use Commercetools\Core\Model\Type\TypeReference;
27+
use Commercetools\Core\Model\Zone\ZoneReference;
28+
29+
/**
30+
* @package Commercetools\Core\Model\Common
31+
* @link https://docs.commercetools.com/http-api-types.html#keyreference
32+
* @method string getTypeId()
33+
* @method KeyReference setTypeId(string $typeId = null)
34+
* @method string getId()
35+
* @method KeyReference setId(string $id = null)
36+
* @method string getKey()
37+
* @method KeyReference setKey(string $key = null)
38+
*/
39+
class KeyReference extends ResourceIdentifier
40+
{
41+
const TYPE_CLASS = JsonObject::class;
42+
43+
/**
44+
* @param array $data
45+
* @param Context|callable $context
46+
* @return static
47+
*/
48+
public static function fromArray(array $data, $context = null)
49+
{
50+
if (get_called_class() == Reference::class && isset($data[static::TYPE_ID])) {
51+
$className = static::referenceType($data[static::TYPE_ID]);
52+
if (class_exists($className)) {
53+
return new $className($data, $context);
54+
}
55+
}
56+
return new static($data, $context);
57+
}
58+
59+
protected static function referenceType($typeId)
60+
{
61+
$types = [
62+
CartReference::TYPE_CART => CartReference::class,
63+
CartDiscountReference::TYPE_CART_DISCOUNT => CartDiscountReference::class,
64+
CategoryReference::TYPE_CATEGORY => CategoryReference::class,
65+
ChannelReference::TYPE_CHANNEL => ChannelReference::class,
66+
CustomerReference::TYPE_CUSTOMER => CustomerReference::class,
67+
CustomObjectReference::TYPE_CUSTOM_OBJECT => CustomObjectReference::class,
68+
CustomerGroupReference::TYPE_CUSTOMER_GROUP =>
69+
CustomerGroupReference::class,
70+
DiscountCodeReference::TYPE_DISCOUNT_CODE => DiscountCodeReference::class,
71+
OrderEditReference::TYPE_ORDER_EDIT => OrderEditReference::class,
72+
PaymentReference::TYPE_PAYMENT => PaymentReference::class,
73+
ProductReference::TYPE_PRODUCT => ProductReference::class,
74+
ProductDiscountReference::TYPE_PRODUCT_DISCOUNT =>
75+
ProductDiscountReference::class,
76+
ProductTypeReference::TYPE_PRODUCT_TYPE => ProductTypeReference::class,
77+
OrderReference::TYPE_ORDER => OrderReference::class,
78+
ShippingMethodReference::TYPE_SHIPPING_METHOD =>
79+
ShippingMethodReference::class,
80+
ShoppingListReference::TYPE_SHOPPING_LIST => ShoppingListReference::class,
81+
StateReference::TYPE_STATE => StateReference::class,
82+
StoreReference::TYPE_STORE => StoreReference::class,
83+
TaxCategoryReference::TYPE_TAX_CATEGORY => TaxCategoryReference::class,
84+
TypeReference::TYPE_TYPE => TypeReference::class,
85+
ZoneReference::TYPE_ZONE => ZoneReference::class,
86+
];
87+
return isset($types[$typeId]) ? $types[$typeId] : Reference::class;
88+
}
89+
}

0 commit comments

Comments
 (0)