Passed
Push — master ( e7acbe...c65cd9 )
by Esteban De La Fuente
19:00
created

Document::setMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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\Helper\Str;
28
use Derafu\Lib\Core\Support\Store\Bag;
29
use Derafu\Lib\Core\Support\Store\Contract\BagInterface;
30
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\AttachmentInterface;
31
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\DocumentInterface;
32
use libredte\lib\Core\Package\Billing\Component\Exchange\Enum\DocumentType;
33
34
/**
35
 * Clase que representa un documento que se enviará en un sobre.
36
 *
37
 * Contiene el documento y los metadatos de dicho documento.
38
 */
39
class Document implements DocumentInterface
40
{
41
    /**
42
     * Tipo del documento.
43
     *
44
     * @var DocumentType
45
     */
46
    private DocumentType $type;
47
48
    /**
49
     * Datos del documento que se está intercambiando.
50
     *
51
     * Típicamente será el XML de un documento tributario electrónico (DTE).
52
     *
53
     * @var string
54
     */
55
    private string $content;
56
57
    /**
58
     * Listado de archivos adjuntos que se enviarán junto al documento.
59
     *
60
     * @var AttachmentInterface[]
61
     */
62
    private array $attachments;
63
64
    /**
65
     * Metadatos del documento.
66
     *
67
     * Estos metadatos están directamente relacionados con el documento que se
68
     * está intercambiando y el tipo de transporte que se esté usando para dicho
69
     * intercambio.
70
     *
71
     * @var BagInterface
72
     */
73
    private BagInterface $metadata;
74
75
    /**
76
     * Constructor del documento que se está intercambiando.
77
     *
78
     * @param string $content
79
     * @param AttachmentInterface[] $attachments
80
     * @param DocumentType $type
81
     * @param BagInterface|array $metadata
82
     */
83
    public function __construct(
84
        string $content = '',
85
        array $attachments = [],
86
        DocumentType $type = DocumentType::B2B,
87
        BagInterface|array $metadata = []
88
    ) {
89
        $this->content = $content;
90
        $this->attachments = $attachments;
91
        $this->type = $type;
92
        $this->setMetadata($metadata);
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function getType(): DocumentType
99
    {
100
        return $this->type;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    public function getID(): string
107
    {
108
        $id = $this->metadata->get('id');
109
110
        if ($id !== null) {
111
            return $id;
112
        }
113
114
        $id = Str::uuid4();
115
116
        $this->metadata->set('id', $id);
117
118
        return $id;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function getContent(): string
125
    {
126
        return $this->content;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function getContentSize(): int
133
    {
134
        return strlen($this->content);
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function addAttachment(AttachmentInterface $attachment): static
141
    {
142
        $this->attachments[] = $attachment;
143
144
        return $this;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150
    public function getAttachments(): array
151
    {
152
        return $this->attachments;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158
    public function setMetadata(BagInterface|array $metadata): static
159
    {
160
        $this->metadata = is_array($metadata)
0 ignored issues
show
introduced by
The condition is_array($metadata) is always true.
Loading history...
161
            ? new Bag($metadata)
162
            : $metadata
163
        ;
164
165
        return $this;
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    public function addMetadata(string $key, mixed $value): static
172
    {
173
        $this->metadata->set($key, $value);
174
175
        return $this;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    public function getMetadata(): BagInterface
182
    {
183
        return $this->metadata;
184
    }
185
}
186