1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/netshoes-sdk |
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
6
|
|
|
* For the information of copyright and license you should read the file |
7
|
|
|
* LICENSE which is distributed with this source code. |
8
|
|
|
* Para a informação dos direitos autorais e de licença você deve ler o arquivo |
9
|
|
|
* LICENSE que é distribuído com este código-fonte. |
10
|
|
|
* Para obtener la información de los derechos de autor y la licencia debe leer |
11
|
|
|
* el archivo LICENSE que se distribuye con el código fuente. |
12
|
|
|
* For more information, see <https://www.gpupo.com/>. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Gpupo\NetshoesSdk\Entity\Order; |
16
|
|
|
|
17
|
|
|
use DateInterval; |
18
|
|
|
use DateTime; |
19
|
|
|
use Gpupo\CommonSdk\Entity\EntityInterface; |
20
|
|
|
use Gpupo\CommonSdk\Response; |
21
|
|
|
use Gpupo\CommonSdk\Traits\LoadTrait; |
22
|
|
|
use Gpupo\CommonSdk\Traits\TranslatorManagerTrait; |
23
|
|
|
use Gpupo\NetshoesSdk\Entity\AbstractManager; |
24
|
|
|
use Gpupo\NetshoesSdk\Traits\DateTimeTrait; |
25
|
|
|
|
26
|
|
|
final class Manager extends AbstractManager |
27
|
|
|
{ |
28
|
|
|
use TranslatorManagerTrait; |
29
|
|
|
use LoadTrait; |
30
|
|
|
use DateTimeTrait; |
31
|
|
|
|
32
|
|
|
protected $entity = 'Order'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @codeCoverageIgnore |
36
|
|
|
*/ |
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->maps = $this->loadArrayFromFile(__DIR__.'/map/restful.map.php'); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function factoryDecorator(Order $order, $decoratorName) |
43
|
|
|
{ |
44
|
1 |
|
$className = __NAMESPACE__.'\\Decorator\\'.$decoratorName; |
45
|
1 |
|
$instance = new $className(); |
46
|
1 |
|
$instance->setOrder($order); |
47
|
|
|
|
48
|
1 |
|
return $instance; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
protected function resolvePrevious(Order $entity) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
1 |
|
$o = $this->findById($entity->getId()); |
55
|
1 |
|
if ($o instanceof Order) { |
56
|
1 |
|
return $o; |
57
|
|
|
} |
58
|
|
|
} catch (\Exception $o) { |
59
|
|
|
throw new \Exception('Order #'.$entity->getId().' not found on marketplace!'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
protected function normalizeShipping(Order $entity, Order $existent) |
64
|
|
|
{ |
65
|
1 |
|
if (1000 > intval($entity->getShipping()->getShippingCode())) { |
66
|
1 |
|
$code = $existent->getShipping()->getShippingCode(); |
67
|
1 |
|
$entity->getShipping()->setShippingCode($code); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $entity; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
7 |
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
77
|
|
|
{ |
78
|
7 |
|
parent::update($entity, $existent); |
79
|
|
|
|
80
|
7 |
|
$factory204 = function ($message) { |
81
|
|
|
return new Response([ |
82
|
|
|
'raw' => '{"message":"'.$message.'"}', |
83
|
|
|
'httpStatusCode' => 204, |
84
|
|
|
]); |
85
|
7 |
|
}; |
86
|
|
|
|
87
|
7 |
|
if (empty($existent)) { |
88
|
7 |
|
$existent = $this->resolvePrevious($entity); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
if ($entity->getOrderStatus() === $existent->getOrderStatus()) { |
|
|
|
|
92
|
|
|
$this->log('info', 'Order sem atualização'); |
93
|
|
|
|
94
|
|
|
return $factory204('Order status not changed!'); |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
if ('processing' === $entity->getOrderStatus()) { |
|
|
|
|
98
|
|
|
return $factory204('Order status not used!'); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$entity = $this->normalizeShipping($entity, $existent); |
|
|
|
|
102
|
|
|
|
103
|
7 |
|
if (in_array($entity->getOrderStatus(), ['approved', 'canceled', |
104
|
7 |
|
'delivered', 'invoiced', 'shipped', ], true)) { |
105
|
6 |
|
$decorator = $this->factoryDecorator($entity, 'Status\\'.ucfirst($entity->getOrderStatus())); |
106
|
6 |
|
$json = $decorator->toJson(); |
107
|
6 |
|
$mapKey = 'to'.ucfirst($entity->getOrderStatus()); |
108
|
6 |
|
$shipping = $entity->getShipping(); |
109
|
6 |
|
$code = $shipping->getShippingCode(); |
110
|
6 |
|
$shipping->toJson(); |
111
|
5 |
|
$map = $this->factoryMap($mapKey, [ |
112
|
5 |
|
'orderNumber' => $entity->getOrderNumber(), |
113
|
5 |
|
'itemId' => $entity->getOrderNumber(), |
114
|
5 |
|
'shippingCode' => $code, |
115
|
|
|
]); |
116
|
|
|
|
117
|
5 |
|
return $this->execute($map, $json); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
throw new \InvalidArgumentException('Order Status ['.$entity->getOrderStatus().'] não suportado', 1); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function factoryTranslator(array $data = []) |
124
|
|
|
{ |
125
|
|
|
$translator = new Translator($data); |
126
|
|
|
|
127
|
|
|
return $translator; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function fetchQueue($offset = 0, $limit = 50, array $parameters = []) |
131
|
|
|
{ |
132
|
1 |
|
return $this->translatorFetch($offset, $limit, array_merge([ |
133
|
1 |
|
'orderStatus' => 'approved', |
134
|
1 |
|
'orderStartDate' => $this->dateMove('P4D'), |
135
|
|
|
], $parameters)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.