|
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\Contract\DataContainerInterface; |
|
28
|
|
|
use Derafu\Lib\Core\Support\Store\DataContainer; |
|
29
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\EnvelopeInterface; |
|
30
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\ExchangeBagInterface; |
|
31
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\ExchangeResultInterface; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Clase que representa una bolsa con sobres con documentos para ser |
|
35
|
|
|
* intercambiada. |
|
36
|
|
|
* |
|
37
|
|
|
* Una bolsa podrá contener sobres de diferentes emisores o receptores, pero los |
|
38
|
|
|
* documentos dentro de cada sobre serán del mismo emisor y receptor. |
|
39
|
|
|
*/ |
|
40
|
|
|
class ExchangeBag implements ExchangeBagInterface |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* Listado de sobres que se están intercambiando en este lote. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array<string, EnvelopeInterface> |
|
46
|
|
|
*/ |
|
47
|
|
|
private array $envelopes = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Opciones para los workers y, especialmente, las estrategias asociadas al |
|
51
|
|
|
* proceso de intercambio de documentos. |
|
52
|
|
|
* |
|
53
|
|
|
* Se usarán las opciones por defecto en cada worker si no se indican los |
|
54
|
|
|
* índices en el arreglo $options. |
|
55
|
|
|
* |
|
56
|
|
|
* @var DataContainerInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
private DataContainerInterface $options; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Listado con los resultados del intercambio. |
|
62
|
|
|
* |
|
63
|
|
|
* @var array<string, ExchangeResultInterface> |
|
64
|
|
|
*/ |
|
65
|
|
|
private array $results = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Reglas de esquema de las opciones del intercambio de documentos. |
|
69
|
|
|
* |
|
70
|
|
|
* El formato del esquema es el utilizado por |
|
71
|
|
|
* Symfony\Component\OptionsResolver\OptionsResolver. |
|
72
|
|
|
* |
|
73
|
|
|
* Acá solo se indicarán los índices que deben pueden existir en las |
|
74
|
|
|
* opciones. No se define el esquema de cada opción pues cada clase que |
|
75
|
|
|
* utilice estas opciones deberá resolver y validar sus propias opciones. |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected array $optionsSchema = [ |
|
80
|
|
|
'strategy' => [ |
|
81
|
|
|
'types' => 'string', |
|
82
|
|
|
], |
|
83
|
|
|
'transport' => [ |
|
84
|
|
|
'types' => 'array', |
|
85
|
|
|
'default' => [], |
|
86
|
|
|
], |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Constructor de la bolsa de intercambio. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $options |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct(DataContainerInterface|array $options = []) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->setOptions($options); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function addEnvelope(EnvelopeInterface $envelope): static |
|
103
|
|
|
{ |
|
104
|
|
|
$this->envelopes[$envelope->getBusinessMessageID()] = $envelope; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritDoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getEnvelopes(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return array_values($this->envelopes); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritDoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function hasEnvelopes(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return !empty($this->envelopes); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritDoc} |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setOptions(DataContainerInterface|array $options): static |
|
129
|
|
|
{ |
|
130
|
|
|
if (is_array($options)) { |
|
|
|
|
|
|
131
|
|
|
$options = new DataContainer($options, $this->optionsSchema); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->options = $options; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritDoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getOptions(): DataContainerInterface |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->options; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritDoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getResults(): array |
|
151
|
|
|
{ |
|
152
|
|
|
return array_values($this->results); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritDoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function addResult(ExchangeResultInterface $result): static |
|
159
|
|
|
{ |
|
160
|
|
|
$envelope = $result->getEnvelope(); |
|
161
|
|
|
|
|
162
|
|
|
$this->results[$envelope->getBusinessMessageID()] = $result; |
|
163
|
|
|
|
|
164
|
|
|
if (!isset($this->envelopes[$envelope->getBusinessMessageID()])) { |
|
165
|
|
|
$this->envelopes[$envelope->getBusinessMessageID()] = $envelope; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|