1 | <?php |
||
21 | final class Manager extends AbstractManager |
||
22 | { |
||
23 | protected $entity = 'Sku'; |
||
24 | |||
25 | protected $strategy = [ |
||
26 | 'info' => false, |
||
27 | 'pricing' => 'simples', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @codeCoverageIgnore |
||
32 | */ |
||
33 | protected function setUp() |
||
37 | |||
38 | /** |
||
39 | * @return Gpupo\Common\Entity\CollectionAbstract|null |
||
40 | */ |
||
41 | 1 | public function findById($itemId) |
|
42 | { |
||
43 | 1 | $response = $this->perform($this->factoryMap('findById', [ |
|
44 | 1 | 'productId' => $itemId, |
|
45 | 1 | 'itemId' => $itemId, |
|
46 | ])); |
||
47 | |||
48 | 1 | $data = $this->processResponse($response); |
|
49 | |||
50 | 1 | if (empty($data)) { |
|
51 | return; |
||
52 | } |
||
53 | |||
54 | 1 | $sku = new Item($data->toArray()); |
|
55 | |||
56 | 1 | return $this->hydrate($sku); |
|
57 | } |
||
58 | |||
59 | protected function getDetail(EntityInterface $sku, $type) |
||
60 | { |
||
61 | $response = $this->perform($this->factoryMap('get'.$type, ['sku' => $sku->getId()])); |
||
62 | $className = 'Gpupo\NetshoesSdk\Entity\Product\Sku\\'.$type; |
||
63 | $data = $this->processResponse($response); |
||
64 | |||
65 | $o = new $className($data->toArray()); |
||
66 | |||
67 | $this->log('info', 'Detail', [ |
||
68 | 'sku' => $sku->getId(), |
||
69 | 'typ' => $type, |
||
70 | 'response' => $data, |
||
71 | 'className' => $className, |
||
72 | 'object' => $o, |
||
73 | ]); |
||
74 | |||
75 | return $o; |
||
76 | } |
||
77 | |||
78 | protected function getPriceScheduleCollection(EntityInterface $sku) |
||
79 | { |
||
80 | try { |
||
81 | $response = $this->perform($this->factoryMap('getPriceSchedule', ['sku' => $sku->getId()])); |
||
82 | $data = $this->processResponse($response); |
||
83 | |||
84 | if (empty($data)) { |
||
85 | throw new ManagerException('No price schedule on SKU #'.$sku->getId()); |
||
86 | } |
||
87 | } catch (ManagerException $e) { |
||
88 | $this->log('error', $e->getMessage()); |
||
89 | |||
90 | return; |
||
91 | } |
||
92 | |||
93 | $collection = new PriceScheduleCollection($data->toArray()); |
||
94 | |||
95 | return $collection; |
||
96 | } |
||
97 | |||
98 | public function add(EntityInterface $entity, $productId) |
||
99 | { |
||
100 | return $this->execute($this->factoryMap('add', [ |
||
101 | 'productId' => $productId, |
||
102 | ]), $entity->toJson()); |
||
103 | } |
||
104 | |||
105 | 1 | public function saveDetail(Item $sku, $type) |
|
112 | |||
113 | protected function hydratePriceSchedule(EntityInterface $sku) |
||
114 | { |
||
115 | $ps = $this->getPriceScheduleCollection($sku); |
||
116 | |||
117 | $sku->setPriceSchedule(false); |
||
|
|||
118 | if ($ps instanceof PriceScheduleCollection) { |
||
119 | $sku->setPriceSchedule($ps->getCurrent()); |
||
120 | } |
||
121 | |||
122 | return $sku; |
||
123 | } |
||
124 | |||
125 | public function hydrate(EntityInterface $sku) |
||
126 | { |
||
127 | $sku->setPrice($this->getDetail($sku, 'Price')) |
||
128 | ->setStock($this->getDetail($sku, 'Stock')) |
||
129 | ->setStatus($this->getDetail($sku, 'Status')); |
||
130 | |||
131 | return $this->hydratePriceSchedule($sku); |
||
132 | } |
||
133 | |||
134 | protected function resolvePrevious(Item $entity) |
||
135 | { |
||
136 | try { |
||
137 | $p = $this->findById($entity->getId()); |
||
138 | if ($p instanceof Item) { |
||
139 | return $p; |
||
140 | } |
||
141 | } catch (\Exception $e) { |
||
142 | throw new \Exception('Sku #'.$entity->getId().' not found on marketplace!'); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 1 | public function update(EntityInterface $entity, EntityInterface $existent = null) |
|
150 | { |
||
151 | 1 | parent::update($entity, $existent); |
|
152 | |||
153 | 1 | if (empty($existent)) { |
|
154 | $existent = $this->resolvePrevious($entity); |
||
155 | } |
||
156 | |||
157 | $response = [ |
||
158 | 1 | 'sku' => $entity->getId(), |
|
159 | 'bypassed' => [], |
||
160 | 'code' => [], |
||
161 | 'updated' => [], |
||
162 | ]; |
||
163 | |||
164 | $list = [ |
||
165 | 1 | 'updateDetails', |
|
166 | ]; |
||
167 | |||
168 | 1 | if (true === $this->strategy['info']) { |
|
169 | $list[] = 'updateInfo'; |
||
170 | } |
||
171 | |||
172 | 1 | foreach ($list as $method) { |
|
173 | 1 | $response = $this->$method($entity, $existent, $response); |
|
174 | } |
||
175 | |||
176 | 1 | $this->log('info', 'Operação de Atualização', $response); |
|
177 | |||
178 | 1 | return $response; |
|
179 | } |
||
180 | |||
181 | 2 | public function updateInfo(Item $entity, $existent = null, array $response = []) |
|
200 | |||
201 | 2 | public function updateDetails(Item $entity, Item $existent = null, array $response = []) |
|
202 | { |
||
203 | $list = [ |
||
204 | 2 | 'Status' => ['active'], |
|
205 | 'Stock' => ['available'], |
||
206 | 'Price' => ['price'], |
||
207 | ]; |
||
208 | |||
229 | } |
||
230 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.