|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of gpupo/netshoes-sdk |
|
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* For more information, see <http://www.g1mr.com/>. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gpupo\NetshoesSdk\Entity\Product\Sku; |
|
12
|
|
|
|
|
13
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
|
14
|
|
|
use Gpupo\NetshoesSdk\Entity\AbstractManager; |
|
15
|
|
|
|
|
16
|
|
|
class Manager extends AbstractManager |
|
17
|
|
|
{ |
|
18
|
|
|
protected $entity = 'SkuCollection'; |
|
19
|
|
|
|
|
20
|
|
|
protected $maps = [ |
|
21
|
|
|
'save' => ['POST', '/products/{productId}/skus'], //Create a new sku for a product |
|
22
|
|
|
'findSkuById' => ['GET', '/products/{productId}/skus/{itemId}'], // Get the a sku by product Id and sku Id |
|
23
|
|
|
'update' => ['PUT', '/products/{productId}/skus/{itemId}'], //Update a product based on SKU |
|
24
|
|
|
'findById' => ['GET', '/products/{itemId}/skus'], //Get the list of product skus |
|
25
|
|
|
'saveStatus' => ['PUT', '/skus/{sku}/bus/{buId}/status'], //Enable or disable sku for sale |
|
26
|
|
|
'savePriceSchedule' => ['POST', '/skus/{sku}/priceSchedules'], //Save a price schedule |
|
27
|
|
|
'getPriceSchedule' => ['GET', '/skus/{sku}/priceSchedules'], //Get PriceSchedule |
|
28
|
|
|
'getPrice' => ['GET', '/skus/{sku}/prices'], //Get a base price |
|
29
|
|
|
'savePrice' => ['PUT', '/skus/{sku}/prices'], //Save a base price |
|
30
|
|
|
'saveStock' => ['PUT', '/skus/{sku}/stocks'], //Update stock quantity by sku |
|
31
|
|
|
'getStock' => ['GET', '/skus/{sku}/stocks'], //Get Stock |
|
32
|
|
|
'saveStatus' => ['GET', '/skus/{sku}/bus/{buId}/status'], //Save Status |
|
33
|
|
|
'getStatus' => ['GET', '/skus/{sku}/bus/{buId}/status'], //Get Status |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public function save(EntityInterface $product, $route = 'save') |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->execute($this->factoryMap($route), $product->toJson()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return Gpupo\Common\Entity\CollectionAbstract|null |
|
43
|
|
|
*/ |
|
44
|
|
|
public function findSkuById($itemId) |
|
45
|
|
|
{ |
|
46
|
|
|
$response = $this->perform($this->factoryMap('findSkuById', [ |
|
47
|
|
|
'productId' => $itemId, |
|
48
|
|
|
'itemId' => $itemId, |
|
49
|
|
|
])); |
|
50
|
|
|
|
|
51
|
|
|
$data = $this->processResponse($response); |
|
52
|
|
|
|
|
53
|
|
|
if (empty($data)) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$sku = new Item($data->toArray()); |
|
58
|
|
|
|
|
59
|
|
|
return $this->hydrate($sku); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function getDetail(EntityInterface $sku, $type) |
|
63
|
|
|
{ |
|
64
|
|
|
$response = $this->perform($this->factoryMap('get'.$type, ['sku' => $sku->getId()])); |
|
65
|
|
|
$className = 'Gpupo\NetshoesSdk\Entity\Product\Sku\\'.$type; |
|
66
|
|
|
$data = $this->processResponse($response); |
|
67
|
|
|
|
|
68
|
|
|
$o = new $className($data->toArray()); |
|
69
|
|
|
|
|
70
|
|
|
$this->getLogger()->addInfo('Detail', [ |
|
71
|
|
|
'sku' => $sku->getId(), |
|
72
|
|
|
'typ' => $type, |
|
73
|
|
|
'response' => $data, |
|
74
|
|
|
'className' => $className, |
|
75
|
|
|
'object' => $o, |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
return $o; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function saveDetail(Item $sku, $type) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->execute($this->factoryMap('save'.$type, ['sku' => $sku->getId()]), $sku->toJson($type)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function hydrate(EntityInterface $sku) |
|
87
|
|
|
{ |
|
88
|
|
|
$sku->setPrice($this->getDetail($sku, 'Price')) |
|
|
|
|
|
|
89
|
|
|
->setPriceSchedule($this->getDetail($sku, 'PriceSchedule')) |
|
90
|
|
|
->setStock($this->getDetail($sku, 'Stock')) |
|
91
|
|
|
->setStatus($this->getDetail($sku, 'Status')); |
|
92
|
|
|
|
|
93
|
|
|
return $sku; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
|
100
|
|
|
{ |
|
101
|
|
|
parent::update($entity, $existent); |
|
102
|
|
|
|
|
103
|
|
|
$m = $this->factoryMap('update', [ |
|
104
|
|
|
'productId' => $entity->getId(), |
|
105
|
|
|
'itemId' => $entity->getId(), |
|
106
|
|
|
]); |
|
107
|
|
|
|
|
108
|
|
|
return $this->execute($m, $entity->toJson()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: