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\Decorator; |
16
|
|
|
|
17
|
|
|
use Gpupo\Common\Entity\Collection; |
18
|
|
|
use Gpupo\CommonSdk\Traits\LoggerTrait; |
19
|
|
|
use Gpupo\NetshoesSdk\Entity\Order\Order; |
20
|
|
|
|
21
|
|
|
abstract class AbstractDecorator extends Collection |
22
|
|
|
{ |
23
|
|
|
use LoggerTrait; |
24
|
|
|
|
25
|
|
|
protected $name = ''; |
26
|
|
|
|
27
|
|
|
protected function factoryArray() |
28
|
|
|
{ |
29
|
|
|
throw new \InvalidArgumentException('factoryArray() deve ser sobrecarregado!'); |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
protected function fail($string = '') |
33
|
|
|
{ |
34
|
5 |
|
$message = 'Order incomplete for status ['.$string.']'; |
35
|
5 |
|
$this->log('error', $message, [ |
36
|
5 |
|
'order' => $this->getOrder(), |
37
|
|
|
]); |
38
|
|
|
|
39
|
5 |
|
throw new \InvalidArgumentException($message); |
40
|
|
|
} |
41
|
|
|
|
42
|
5 |
|
protected function invalid($string = '') |
43
|
|
|
{ |
44
|
5 |
|
$message = 'Attribute invalid: '.$string.' '; |
45
|
|
|
|
46
|
5 |
|
throw new \InvalidArgumentException($message); |
47
|
|
|
} |
48
|
|
|
|
49
|
10 |
|
public function setOrder(Order $order) |
50
|
|
|
{ |
51
|
10 |
|
$this->set('order', $order); |
52
|
10 |
|
$this->initLogger($order->getLogger()); |
53
|
|
|
|
54
|
10 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
public function getOrder() |
58
|
|
|
{ |
59
|
5 |
|
return $this->get('order'); |
60
|
|
|
} |
61
|
|
|
|
62
|
14 |
|
public function validate() |
63
|
|
|
{ |
64
|
14 |
|
$order = $this->getOrder(); |
65
|
|
|
|
66
|
14 |
|
if (empty($order)) { |
67
|
4 |
|
$this->invalid('Order'); |
68
|
|
|
} |
69
|
|
|
|
70
|
10 |
|
$this->getOrder()->check(); |
71
|
|
|
|
72
|
10 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
15 |
|
public function toArray() |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
15 |
|
$this->validate(); |
79
|
10 |
|
$array = $this->factoryArray(); |
80
|
5 |
|
} catch (\Exception $e) { |
81
|
5 |
|
return $this->fail($this->name.' ('.$e->getMessage().')'); |
82
|
|
|
} |
83
|
|
|
|
84
|
10 |
|
return $array; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|