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

ExchangeStatus::isOk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\ExchangeStatusInterface;
30
use Throwable;
31
32
/**
33
 * Clase que representa el estado del resultado de la ejecución de una
34
 * estrategia de intercambio.
35
 */
36
class ExchangeStatus implements ExchangeStatusInterface
37
{
38
    /**
39
     * Código de la estrategia a la que está asociado este estado.
40
     *
41
     * @var string
42
     */
43
    private string $strategy;
44
45
    /**
46
     * El error que la estrategia generó al ser ejecutada.
47
     *
48
     * Por ejemplo: una excepción lanzada.
49
     *
50
     * @var Throwable|null
51
     */
52
    private ?Throwable $error;
53
54
    /**
55
     * Metadatos del estado de resultado.
56
     *
57
     * @var BagInterface
58
     */
59
    private BagInterface $metadata;
60
61
    /**
62
     * Constructor del estado de la estrategia.
63
     *
64
     * @param string $strategy
65
     * @param Throwable|null $error
66
     * @param BagInterface|array $metadata
67
     */
68
    public function __construct(
69
        string $strategy,
70
        ?Throwable $error = null,
71
        BagInterface|array $metadata = []
72
    ) {
73
        $this->strategy = $strategy;
74
        $this->error = $error;
75
        $this->setMetadata($metadata);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function getStrategy(): string
82
    {
83
        return $this->strategy;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function isOk(): bool
90
    {
91
        return !$this->hasError();
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function setError(Throwable $error): static
98
    {
99
        $this->error = $error;
100
101
        return $this;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function getError(): ?Throwable
108
    {
109
        return $this->error;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function hasError(): bool
116
    {
117
        return isset($this->error);
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    public function setMetadata(BagInterface|array $metadata): static
124
    {
125
        $this->metadata = is_array($metadata)
0 ignored issues
show
introduced by
The condition is_array($metadata) is always true.
Loading history...
126
            ? new Bag($metadata)
127
            : $metadata
128
        ;
129
130
        return $this;
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136
    public function addMetadata(string $key, mixed $value): static
137
    {
138
        $this->metadata->set($key, $value);
139
140
        return $this;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function getMetadata(): BagInterface
147
    {
148
        return $this->metadata;
149
    }
150
}
151