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

Commit a06a00d

Browse files
committed
feat(ProductDiscount): support key attribute
1 parent 298c141 commit a06a00d

File tree

10 files changed

+296
-1
lines changed

10 files changed

+296
-1
lines changed

src/Core/Builder/Request/ProductDiscountRequestBuilder.php

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

55
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountByIdGetRequest;
6+
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountByKeyGetRequest;
67
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountCreateRequest;
78
use Commercetools\Core\Model\ProductDiscount\ProductDiscountDraft;
8-
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountDeleteRequest;
9+
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountDeleteByKeyRequest;
910
use Commercetools\Core\Model\ProductDiscount\ProductDiscount;
11+
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountDeleteRequest;
1012
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountMatchingRequest;
1113
use Commercetools\Core\Model\Common\Price;
1214
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountQueryRequest;
15+
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountUpdateByKeyRequest;
1316
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountUpdateRequest;
1417

1518
class ProductDiscountRequestBuilder
@@ -26,6 +29,17 @@ public function getById($id)
2629
return $request;
2730
}
2831

32+
/**
33+
*
34+
* @param string $key
35+
* @return ProductDiscountByKeyGetRequest
36+
*/
37+
public function getByKey($key)
38+
{
39+
$request = ProductDiscountByKeyGetRequest::ofKey($key);
40+
return $request;
41+
}
42+
2943
/**
3044
* @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#create-a-productdiscount
3145
* @param ProductDiscountDraft $productDiscount
@@ -37,6 +51,17 @@ public function create(ProductDiscountDraft $productDiscount)
3751
return $request;
3852
}
3953

54+
/**
55+
*
56+
* @param ProductDiscount $productDiscount
57+
* @return ProductDiscountDeleteByKeyRequest
58+
*/
59+
public function deleteByKey(ProductDiscount $productDiscount)
60+
{
61+
$request = ProductDiscountDeleteByKeyRequest::ofKeyAndVersion($productDiscount->getKey(), $productDiscount->getVersion());
62+
return $request;
63+
}
64+
4065
/**
4166
* @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#delete-productdiscount
4267
* @param ProductDiscount $productDiscount
@@ -72,6 +97,17 @@ public function query()
7297
return $request;
7398
}
7499

100+
/**
101+
*
102+
* @param ProductDiscount $productDiscount
103+
* @return ProductDiscountUpdateByKeyRequest
104+
*/
105+
public function updateByKey(ProductDiscount $productDiscount)
106+
{
107+
$request = ProductDiscountUpdateByKeyRequest::ofKeyAndVersion($productDiscount->getKey(), $productDiscount->getVersion());
108+
return $request;
109+
}
110+
75111
/**
76112
* @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#update-productdiscount
77113
* @param ProductDiscount $productDiscount

src/Core/Builder/Update/ProductDiscountsActionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountChangeSortOrderAction;
1111
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountChangeValueAction;
1212
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetDescriptionAction;
13+
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetKeyAction;
1314
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidFromAction;
1415
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidFromAndUntilAction;
1516
use Commercetools\Core\Request\ProductDiscounts\Command\ProductDiscountSetValidUntilAction;
@@ -84,6 +85,17 @@ public function setDescription($action = null)
8485
return $this;
8586
}
8687

88+
/**
89+
*
90+
* @param ProductDiscountSetKeyAction|callable $action
91+
* @return $this
92+
*/
93+
public function setKey($action = null)
94+
{
95+
$this->addAction($this->resolveAction(ProductDiscountSetKeyAction::class, $action));
96+
return $this;
97+
}
98+
8799
/**
88100
* @link https://docs.commercetools.com/http-api-projects-productDiscounts.html#set-valid-from
89101
* @param ProductDiscountSetValidFromAction|callable $action

src/Core/Model/ProductDiscount/ProductDiscount.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
* @method ProductDiscount setCreatedBy(CreatedBy $createdBy = null)
4747
* @method LastModifiedBy getLastModifiedBy()
4848
* @method ProductDiscount setLastModifiedBy(LastModifiedBy $lastModifiedBy = null)
49+
* @method string getKey()
50+
* @method ProductDiscount setKey(string $key = null)
4951
* @method ProductDiscountReference getReference()
5052
*/
5153
class ProductDiscount extends Resource
@@ -64,6 +66,7 @@ public function fieldDefinitions()
6466
static::DECORATOR => DateTimeDecorator::class
6567
],
6668
'name' => [static::TYPE => LocalizedString::class],
69+
'key' => [static::TYPE => 'string'],
6770
'description' => [static::TYPE => LocalizedString::class],
6871
'value' => [static::TYPE => ProductDiscountValue::class],
6972
'predicate' => [],

src/Core/Model/ProductDiscount/ProductDiscountDraft.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
* @method ProductDiscountDraft setValidFrom(DateTime $validFrom = null)
3131
* @method DateTimeDecorator getValidUntil()
3232
* @method ProductDiscountDraft setValidUntil(DateTime $validUntil = null)
33+
* @method string getKey()
34+
* @method ProductDiscountDraft setKey(string $key = null)
3335
*/
3436
class ProductDiscountDraft extends JsonObject
3537
{
3638
public function fieldDefinitions()
3739
{
3840
return [
3941
'name' => [static::TYPE => LocalizedString::class],
42+
'key' => [static::TYPE => 'string'],
4043
'description' => [static::TYPE => LocalizedString::class],
4144
'value' => [static::TYPE => ProductDiscountValue::class],
4245
'predicate' => [static::TYPE => 'string'],
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\ProductDiscounts\Command;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Request\AbstractAction;
9+
10+
/**
11+
* @package Commercetools\Core\Request\ProductDiscounts\Command
12+
*
13+
* @method string getAction()
14+
* @method ProductDiscountSetKeyAction setAction(string $action = null)
15+
* @method string getKey()
16+
* @method ProductDiscountSetKeyAction setKey(string $key = null)
17+
*/
18+
class ProductDiscountSetKeyAction 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+
* @param string $key
40+
* @param Context|callable $context
41+
* @return ProductDiscountSetKeyAction
42+
*/
43+
public static function ofKey($key, $context = null)
44+
{
45+
return static::of($context)->setKey($key);
46+
}
47+
}
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\ProductDiscounts;
6+
7+
use Commercetools\Core\Model\ProductDiscount\ProductDiscount;
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractByKeyGetRequest;
10+
use Commercetools\Core\Response\ApiResponseInterface;
11+
use Commercetools\Core\Model\MapperInterface;
12+
13+
/**
14+
* @package Commercetools\Core\Request\ProductDiscounts
15+
*
16+
* @method ProductDiscount mapResponse(ApiResponseInterface $response)
17+
* @method ProductDiscount mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ProductDiscountByKeyGetRequest extends AbstractByKeyGetRequest
20+
{
21+
protected $resultClass = ProductDiscount::class;
22+
23+
/**
24+
* @param string $key
25+
* @param Context $context
26+
*/
27+
public function __construct($key, Context $context = null)
28+
{
29+
parent::__construct(ProductDiscountsEndpoint::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\ProductDiscounts;
6+
7+
use Commercetools\Core\Model\ProductDiscount\ProductDiscount;
8+
use Commercetools\Core\Model\Common\Context;
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\ProductDiscounts
15+
*
16+
* @method ProductDiscount mapResponse(ApiResponseInterface $response)
17+
* @method ProductDiscount mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ProductDiscountDeleteByKeyRequest extends AbstractDeleteByKeyRequest
20+
{
21+
protected $resultClass = ProductDiscount::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(ProductDiscountsEndpoint::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\ProductDiscounts;
6+
7+
use Commercetools\Core\Model\ProductDiscount\ProductDiscount;
8+
use Commercetools\Core\Model\Common\Context;
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\ProductDiscounts
15+
*
16+
* @method ProductDiscount mapResponse(ApiResponseInterface $response)
17+
* @method ProductDiscount mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
18+
*/
19+
class ProductDiscountUpdateByKeyRequest extends AbstractUpdateByKeyRequest
20+
{
21+
protected $resultClass = ProductDiscount::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(ProductDiscountsEndpoint::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/ProductDiscount/ProductDiscountQueryRequestTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Commercetools\Core\Model\ProductDiscount\ProductDiscountDraft;
1515
use Commercetools\Core\Model\ProductDiscount\ProductDiscountValue;
1616
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountByIdGetRequest;
17+
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountByKeyGetRequest;
1718
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountCreateRequest;
1819
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountDeleteRequest;
1920
use Commercetools\Core\Request\ProductDiscounts\ProductDiscountQueryRequest;
@@ -79,4 +80,18 @@ public function testGetById()
7980
$this->assertInstanceOf(ProductDiscount::class, $productDiscount);
8081
$this->assertSame($productDiscount->getId(), $result->getId());
8182
}
83+
84+
public function testGetByKey()
85+
{
86+
$draft = $this->getDraft()->setKey('key-' . $this->getTestRun());
87+
$productDiscount = $this->createProductDiscount($draft);
88+
89+
$request = ProductDiscountByKeyGetRequest::ofKey($productDiscount->getKey());
90+
$response = $request->executeWithClient($this->getClient());
91+
$result = $request->mapResponse($response);
92+
93+
$this->assertInstanceOf(ProductDiscount::class, $productDiscount);
94+
$this->assertSame($productDiscount->getId(), $result->getId());
95+
$this->assertSame($productDiscount->getKey(), $result->getKey());
96+
}
8297
}

0 commit comments

Comments
 (0)