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 <http://www.g1mr.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
|
|
|
|
25
|
|
|
final class Manager extends AbstractManager |
26
|
|
|
{ |
27
|
|
|
use TranslatorManagerTrait; |
28
|
|
|
use LoadTrait; |
29
|
|
|
|
30
|
|
|
protected $entity = 'Order'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
*/ |
35
|
|
|
protected function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->maps = $this->loadArrayFromFile(__DIR__.'/map/restful.map.php'); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function factoryDecorator(Order $order, $decoratorName) |
41
|
|
|
{ |
42
|
1 |
|
$className = __NAMESPACE__.'\\Decorator\\'.$decoratorName; |
43
|
1 |
|
$instance = new $className(); |
44
|
1 |
|
$instance->setOrder($order); |
45
|
|
|
|
46
|
1 |
|
return $instance; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function resolvePrevious(Order $entity) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
7 |
|
$o = $this->findById($entity->getId()); |
53
|
|
|
if ($o instanceof Order) { |
54
|
7 |
|
return $o; |
55
|
|
|
} |
56
|
|
|
} catch (\Exception $o) { |
57
|
|
|
throw new \Exception('Order #'.$entity->getId().' not found on marketplace!'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function normalizeShipping(Order $entity, Order $existent) |
62
|
7 |
|
{ |
63
|
|
|
if (1000 > intval($entity->getShipping()->getShippingCode())) { |
64
|
|
|
$code = $existent->getShipping()->getShippingCode(); |
65
|
|
|
$entity->getShipping()->setShippingCode($code); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $entity; |
69
|
7 |
|
} |
70
|
7 |
|
|
71
|
6 |
|
/** |
72
|
6 |
|
* {@inheritdoc} |
73
|
6 |
|
*/ |
74
|
6 |
|
public function update(EntityInterface $entity, EntityInterface $existent = null) |
75
|
6 |
|
{ |
76
|
6 |
|
parent::update($entity, $existent); |
77
|
5 |
|
|
78
|
5 |
|
$factory204 = function ($message) { |
79
|
5 |
|
return new Response([ |
80
|
5 |
|
'raw' => '{"message":"'.$message.'"}', |
81
|
|
|
'httpStatusCode' => 204, |
82
|
|
|
]); |
83
|
5 |
|
}; |
84
|
|
|
|
85
|
|
|
if (empty($existent)) { |
86
|
1 |
|
$existent = $this->resolvePrevious($entity); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($entity->getOrderStatus() === $existent->getOrderStatus()) { |
|
|
|
|
90
|
|
|
$this->log('info', 'Order sem atualização'); |
91
|
|
|
|
92
|
|
|
return $factory204('Order status not changed!'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ('processing' === $entity->getOrderStatus()) { |
|
|
|
|
96
|
1 |
|
return $factory204('Order status not used!'); |
97
|
|
|
} |
98
|
1 |
|
|
99
|
1 |
|
$entity = $this->normalizeShipping($entity, $existent); |
|
|
|
|
100
|
|
|
|
101
|
1 |
|
if (in_array($entity->getOrderStatus(), ['approved', 'canceled', |
102
|
1 |
|
'delivered', 'invoiced', 'shipped', ], true)) { |
103
|
1 |
|
$decorator = $this->factoryDecorator($entity, 'Status\\'.ucfirst($entity->getOrderStatus())); |
104
|
|
|
$json = $decorator->toJson(); |
105
|
|
|
$mapKey = 'to'.ucfirst($entity->getOrderStatus()); |
106
|
|
|
$shipping = $entity->getShipping(); |
107
|
|
|
$code = $shipping->getShippingCode(); |
108
|
|
|
$shipping->toJson(); |
109
|
|
|
$map = $this->factoryMap($mapKey, [ |
110
|
|
|
'orderNumber' => $entity->getOrderNumber(), |
111
|
|
|
'itemId' => $entity->getOrderNumber(), |
112
|
|
|
'shippingCode' => $code, |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
return $this->execute($map, $json); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
throw new \InvalidArgumentException('Order Status ['.$entity->getOrderStatus().'] não suportado', 1); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function factoryTranslator(array $data = []) |
122
|
|
|
{ |
123
|
|
|
$translator = new Translator($data); |
124
|
|
|
|
125
|
|
|
return $translator; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function fetchQueue($offset = 0, $limit = 50, array $parameters = []) |
129
|
|
|
{ |
130
|
|
|
$date = new DateTime(); |
131
|
|
|
$date->sub(new DateInterval('P4D')); |
132
|
|
|
|
133
|
|
|
return $this->translatorFetch($offset, $limit, array_merge([ |
134
|
|
|
'orderStatus' => 'approved', |
135
|
|
|
'orderStartDate' => $date->format('Y-m-d'), |
136
|
|
|
], $parameters)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.