1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* LibreDTE: Biblioteca PHP (Núcleo). |
7
|
|
|
* Copyright (C) LibreDTE <https://www.libredte.cl> |
8
|
|
|
* |
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
13
|
|
|
* |
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
18
|
|
|
* |
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de |
20
|
|
|
* GNU junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace libredte\lib\Core\Package\Billing\Component\Exchange\Support; |
26
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Support\Store\Bag; |
28
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\BagInterface; |
29
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\EnvelopeInterface; |
30
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\ExchangeResultInterface; |
31
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\ExchangeStatusInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Clase que representan un resultado de intercambio de sobres. |
35
|
|
|
* |
36
|
|
|
* Este resultado está asociado a un único sobre, pero puede tener como |
37
|
|
|
* resultado múltiples estados. Loanterior ocurre porque un sobre puede haber |
38
|
|
|
* sido procesado por más de una estrategia y cada una asigna un estado. |
39
|
|
|
*/ |
40
|
|
|
class ExchangeResult implements ExchangeResultInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* Sobre al que está asociado el resultado. |
44
|
|
|
* |
45
|
|
|
* @var EnvelopeInterface |
46
|
|
|
*/ |
47
|
|
|
private EnvelopeInterface $envelope; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Listado de resultados de las estrategias que procesaron el sobre. |
51
|
|
|
* |
52
|
|
|
* @var array<string, ExchangeStatusInterface> |
53
|
|
|
*/ |
54
|
|
|
private array $statuses; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Metadatos del resultado. |
58
|
|
|
* |
59
|
|
|
* @var BagInterface |
60
|
|
|
*/ |
61
|
|
|
private BagInterface $metadata; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor del resultado del intercambio de un sobre. |
65
|
|
|
* |
66
|
|
|
* @param EnvelopeInterface $envelope |
67
|
|
|
* @param BagInterface|array $metadata |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
EnvelopeInterface $envelope, |
71
|
|
|
BagInterface|array $metadata = [] |
72
|
|
|
) { |
73
|
|
|
$this->envelope = $envelope; |
74
|
|
|
$this->setMetadata($metadata); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function getEnvelope(): EnvelopeInterface |
81
|
|
|
{ |
82
|
|
|
return $this->envelope; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function getStrategies(): array |
89
|
|
|
{ |
90
|
|
|
return array_keys($this->statuses); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
public function getStatuses(): array |
97
|
|
|
{ |
98
|
|
|
return array_values($this->statuses); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
|
|
public function addStatus(ExchangeStatusInterface $status): static |
105
|
|
|
{ |
106
|
|
|
$this->statuses[$status->getStrategy()] = $status; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
|
|
public function setMetadata(BagInterface|array $metadata): static |
115
|
|
|
{ |
116
|
|
|
$this->metadata = is_array($metadata) |
|
|
|
|
117
|
|
|
? new Bag($metadata) |
118
|
|
|
: $metadata |
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function addMetadata(string $key, mixed $value): static |
128
|
|
|
{ |
129
|
|
|
$this->metadata->set($key, $value); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
|
|
public function getMetadata(): BagInterface |
138
|
|
|
{ |
139
|
|
|
return $this->metadata; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|