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

Commit d331cff

Browse files
committed
feat(Zone): support Key on shipping Zones
closes #448
1 parent 7ba30dd commit d331cff

File tree

11 files changed

+293
-1
lines changed

11 files changed

+293
-1
lines changed

src/Core/Builder/Request/ZoneRequestBuilder.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Commercetools\Core\Builder\Request;
44

55
use Commercetools\Core\Request\Zones\ZoneByIdGetRequest;
6+
use Commercetools\Core\Request\Zones\ZoneByKeyGetRequest;
67
use Commercetools\Core\Request\Zones\ZoneCreateRequest;
78
use Commercetools\Core\Model\Zone\ZoneDraft;
8-
use Commercetools\Core\Request\Zones\ZoneDeleteRequest;
9+
use Commercetools\Core\Request\Zones\ZoneDeleteByKeyRequest;
910
use Commercetools\Core\Model\Zone\Zone;
11+
use Commercetools\Core\Request\Zones\ZoneDeleteRequest;
1012
use Commercetools\Core\Request\Zones\ZoneQueryRequest;
13+
use Commercetools\Core\Request\Zones\ZoneUpdateByKeyRequest;
1114
use Commercetools\Core\Request\Zones\ZoneUpdateRequest;
1215

1316
class ZoneRequestBuilder
@@ -24,6 +27,17 @@ public function getById($id)
2427
return $request;
2528
}
2629

30+
/**
31+
* @link https://docs.commercetools.com/http-api-projects-zones.html#get-zone-by-key
32+
* @param string $key
33+
* @return ZoneByKeyGetRequest
34+
*/
35+
public function getByKey($key)
36+
{
37+
$request = ZoneByKeyGetRequest::ofKey($key);
38+
return $request;
39+
}
40+
2741
/**
2842
* @link https://docs.commercetools.com/http-api-projects-zones.html#create-zone
2943
* @param ZoneDraft $zone
@@ -35,6 +49,17 @@ public function create(ZoneDraft $zone)
3549
return $request;
3650
}
3751

52+
/**
53+
* @link https://docs.commercetools.com/http-api-projects-zones.html#delete-zone-by-key
54+
* @param Zone $zone
55+
* @return ZoneDeleteByKeyRequest
56+
*/
57+
public function deleteByKey(Zone $zone)
58+
{
59+
$request = ZoneDeleteByKeyRequest::ofKeyAndVersion($zone->getKey(), $zone->getVersion());
60+
return $request;
61+
}
62+
3863
/**
3964
* @link https://docs.commercetools.com/http-api-projects-zones.html#delete-zone
4065
* @param Zone $zone
@@ -57,6 +82,17 @@ public function query()
5782
return $request;
5883
}
5984

85+
/**
86+
* @link https://docs.commercetools.com/http-api-projects-zones.html#update-zone-by-key
87+
* @param Zone $zone
88+
* @return ZoneUpdateByKeyRequest
89+
*/
90+
public function updateByKey(Zone $zone)
91+
{
92+
$request = ZoneUpdateByKeyRequest::ofKeyAndVersion($zone->getKey(), $zone->getVersion());
93+
return $request;
94+
}
95+
6096
/**
6197
* @link https://docs.commercetools.com/http-api-projects-zones.html#update-zone
6298
* @param Zone $zone

src/Core/Builder/Update/ZonesActionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Commercetools\Core\Request\Zones\Command\ZoneChangeNameAction;
99
use Commercetools\Core\Request\Zones\Command\ZoneRemoveLocationAction;
1010
use Commercetools\Core\Request\Zones\Command\ZoneSetDescriptionAction;
11+
use Commercetools\Core\Request\Zones\Command\ZoneSetKeyAction;
1112

1213
class ZonesActionBuilder
1314
{
@@ -57,6 +58,17 @@ public function setDescription($action = null)
5758
return $this;
5859
}
5960

61+
/**
62+
* @link https://docs.commercetools.com/http-api-projects-zones.html#set-key
63+
* @param ZoneSetKeyAction|callable $action
64+
* @return $this
65+
*/
66+
public function setKey($action = null)
67+
{
68+
$this->addAction($this->resolveAction(ZoneSetKeyAction::class, $action));
69+
return $this;
70+
}
71+
6072
/**
6173
* @return ZonesActionBuilder
6274
*/

src/Core/Model/Zone/Zone.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
* @method Zone setDescription(string $description = null)
2727
* @method LocationCollection getLocations()
2828
* @method Zone setLocations(LocationCollection $locations = null)
29+
* @method string getKey()
30+
* @method Zone setKey(string $key = null)
2931
* @method ZoneReference getReference()
3032
*/
3133
class Zone extends Resource
@@ -35,6 +37,7 @@ public function fieldDefinitions()
3537
return [
3638
'id' => [static::TYPE => 'string'],
3739
'version' => [static::TYPE => 'int'],
40+
'key' => [static::TYPE => 'string'],
3841
'createdAt' => [
3942
static::TYPE => DateTime::class,
4043
static::DECORATOR => DateTimeDecorator::class

src/Core/Model/Zone/ZoneDraft.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
* @method ZoneDraft setDescription(string $description = null)
1818
* @method LocationCollection getLocations()
1919
* @method ZoneDraft setLocations(LocationCollection $locations = null)
20+
* @method string getKey()
21+
* @method ZoneDraft setKey(string $key = null)
2022
*/
2123
class ZoneDraft extends JsonObject
2224
{
2325
public function fieldDefinitions()
2426
{
2527
return [
28+
'key' => [static::TYPE => 'string'],
2629
'name' => [static::TYPE => 'string'],
2730
'description' => [static::TYPE => 'string'],
2831
'locations' => [static::TYPE => LocationCollection::class],

src/Core/Model/Zone/ZoneReference.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ public static function ofId($id, $context = null)
3535
{
3636
return static::ofTypeAndId(static::TYPE_ZONE, $id, $context);
3737
}
38+
39+
/**
40+
* @param $key
41+
* @param Context|callable $context
42+
* @return ZoneReference
43+
*/
44+
public static function ofKey($key, $context = null)
45+
{
46+
return static::ofTypeAndKey(static::TYPE_ZONE, $key, $context);
47+
}
3848
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\Zones\Command;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Request\AbstractAction;
9+
10+
/**
11+
* @package Commercetools\Core\Request\Zones\Command
12+
* @link https://docs.commercetools.com/http-api-projects-zones.html#set-key
13+
* @method string getAction()
14+
* @method ZoneSetKeyAction setAction(string $action = null)
15+
* @method string getKey()
16+
* @method ZoneSetKeyAction setKey(string $key = null)
17+
*/
18+
class ZoneSetKeyAction extends AbstractAction
19+
{
20+
public function fieldDefinitions()
21+
{
22+
return [
23+
'action' => [static::TYPE => 'string'],
24+
'key' => [static::TYPE => 'string'],
25+
];
26+
}
27+
28+
/**
29+
* @param array $data
30+
* @param Context|callable $context
31+
*/
32+
public function __construct(array $data = [], $context = null)
33+
{
34+
parent::__construct($data, $context);
35+
$this->setAction('setKey');
36+
}
37+
38+
39+
/**
40+
* @param string $key
41+
* @param Context|callable $context
42+
* @return ZoneSetKeyAction
43+
*/
44+
public static function ofKey($key, $context = null)
45+
{
46+
return static::of($context)->setKey($key);
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\Zones;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Model\Zone\Zone;
9+
use Commercetools\Core\Request\AbstractByKeyGetRequest;
10+
use Commercetools\Core\Model\MapperInterface;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
13+
/**
14+
* @package Commercetools\Core\Request\Zones
15+
* @link https://docs.commercetools.com/http-api-projects-zones.html#get-zone-by-key
16+
* @method Zone mapResponse(ApiResponseInterface $response)
17+
* @method Zone mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ZoneByKeyGetRequest extends AbstractByKeyGetRequest
20+
{
21+
protected $resultClass = Zone::class;
22+
23+
/**
24+
* @param string $key
25+
* @param Context $context
26+
*/
27+
public function __construct($key, Context $context = null)
28+
{
29+
parent::__construct(ZonesEndpoint::endpoint(), $key, $context);
30+
}
31+
32+
/**
33+
* @param string $key
34+
* @param Context $context
35+
* @return static
36+
*/
37+
public static function ofKey($key, Context $context = null)
38+
{
39+
return new static($key, $context);
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\Zones;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Model\Zone\Zone;
9+
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
10+
use Commercetools\Core\Response\ApiResponseInterface;
11+
use Commercetools\Core\Model\MapperInterface;
12+
13+
/**
14+
* @package Commercetools\Core\Request\Zones
15+
* @link https://docs.commercetools.com/http-api-projects-zones.html#delete-zone-by-key
16+
* @method Zone mapResponse(ApiResponseInterface $response)
17+
* @method Zone mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ZoneDeleteByKeyRequest extends AbstractDeleteByKeyRequest
20+
{
21+
protected $resultClass = Zone::class;
22+
23+
/**
24+
* @param string $key
25+
* @param int $version
26+
* @param Context $context
27+
*/
28+
public function __construct($key, $version, Context $context = null)
29+
{
30+
parent::__construct(ZonesEndpoint::endpoint(), $key, $version, $context);
31+
}
32+
33+
/**
34+
* @param string $key
35+
* @param int $version
36+
* @param Context $context
37+
* @return static
38+
*/
39+
public static function ofKeyAndVersion($key, $version, Context $context = null)
40+
{
41+
return new static($key, $version, $context);
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\Zones;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Model\Zone\Zone;
9+
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
10+
use Commercetools\Core\Response\ApiResponseInterface;
11+
use Commercetools\Core\Model\MapperInterface;
12+
13+
/**
14+
* @package Commercetools\Core\Request\Zones
15+
* @link https://docs.commercetools.com/http-api-projects-zones.html#update-zone-by-key
16+
* @method Zone mapResponse(ApiResponseInterface $response)
17+
* @method Zone mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ZoneUpdateByKeyRequest extends AbstractUpdateByKeyRequest
20+
{
21+
protected $resultClass = Zone::class;
22+
23+
/**
24+
* @param string $key
25+
* @param int $version
26+
* @param array $actions
27+
* @param Context $context
28+
*/
29+
public function __construct($key, $version, array $actions = [], Context $context = null)
30+
{
31+
parent::__construct(ZonesEndpoint::endpoint(), $key, $version, $actions, $context);
32+
}
33+
34+
/**
35+
* @param string $key
36+
* @param int $version
37+
* @param Context $context
38+
* @return static
39+
*/
40+
public static function ofKeyAndVersion($key, $version, Context $context = null)
41+
{
42+
return new static($key, $version, [], $context);
43+
}
44+
}

tests/integration/Zone/ZoneQueryRequestTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use Commercetools\Core\Model\Zone\Location;
1313
use Commercetools\Core\Model\Zone\LocationCollection;
1414
use Commercetools\Core\Request\Zones\ZoneByIdGetRequest;
15+
use Commercetools\Core\Request\Zones\ZoneByKeyGetRequest;
1516
use Commercetools\Core\Request\Zones\ZoneCreateRequest;
17+
use Commercetools\Core\Request\Zones\ZoneDeleteByKeyRequest;
1618
use Commercetools\Core\Request\Zones\ZoneDeleteRequest;
1719
use Commercetools\Core\Request\Zones\ZoneQueryRequest;
1820

@@ -72,6 +74,33 @@ public function testGetById()
7274

7375
$this->assertInstanceOf(Zone::class, $zone);
7476
$this->assertSame($zone->getId(), $result->getId());
77+
}
78+
79+
public function testGetByKey()
80+
{
81+
$draft = $this->getDraft()->setKey('key-' . $this->getTestRun());
82+
$zone = $this->createZone($draft);
83+
84+
$request = ZoneByKeyGetRequest::ofKey($zone->getKey());
85+
$response = $request->executeWithClient($this->getClient());
86+
$result = $request->mapResponse($response);
87+
88+
$this->assertInstanceOf(Zone::class, $zone);
89+
$this->assertSame($zone->getKey(), $result->getKey());
90+
}
91+
92+
public function testDeleteByKey()
93+
{
94+
$draft = $this->getDraft()->setKey('key-' . $this->getTestRun());
95+
$zone = $this->createZone($draft);
96+
97+
$request = ZoneDeleteByKeyRequest::ofKeyAndVersion(
98+
$zone->getKey(),
99+
$zone->getVersion()
100+
);
101+
$result = $this->getClient()->execute($request);
102+
$response = $request->mapFromResponse($result);
75103

104+
$this->assertSame($zone->getId(), $response->getId());
76105
}
77106
}

0 commit comments

Comments
 (0)