Passed
Push — master ( be9ce7...13a902 )
by Esteban De La Fuente
04:18
created

ConfigurableTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 63
ccs 15
cts 22
cp 0.6818
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveConfiguration() 0 11 2
A getConfigurationSchema() 0 3 1
A setConfiguration() 0 13 2
A getConfiguration() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Derafu: Biblioteca PHP (Núcleo).
7
 * Copyright (C) Derafu <https://www.derafu.org>
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 GNU
20
 * junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace Derafu\Lib\Core\Common\Trait;
26
27
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface;
28
use Derafu\Lib\Core\Support\Store\DataContainer;
29
30
/**
31
 * Trait para clases que deben implementar un sistema simple de configuración.
32
 *
33
 * @see Derafu\Lib\Core\Common\Contract\ConfigurableInterface
34
 */
35
trait ConfigurableTrait
36
{
37
    /**
38
     * Contenedor para las configuraciones.
39
     *
40
     * @var DataContainerInterface
41
     */
42
    protected DataContainerInterface $configuration;
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 46
    public function setConfiguration(
48
        array|DataContainerInterface $configuration
49
    ): static {
50 46
        if (is_array($configuration)) {
0 ignored issues
show
introduced by
The condition is_array($configuration) is always true.
Loading history...
51 46
            $configuration = new DataContainer(
52 46
                $configuration,
53 46
                $this->getConfigurationSchema()
54 46
            );
55
        }
56
57 46
        $this->configuration = $configuration;
58
59 46
        return $this;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 38
    public function getConfiguration(): DataContainerInterface
66
    {
67 38
        if (!isset($this->configuration)) {
68 38
            $this->setConfiguration([]);
69
        }
70
71 38
        $this->configuration->validate();
72
73 38
        return $this->configuration;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function resolveConfiguration(
80
        array $configuration = []
81
    ): DataContainerInterface {
82
        if (isset($this->configuration)) {
83
            $configuration = array_merge(
84
                $this->configuration->all(),
85
                $configuration
86
            );
87
        }
88
89
        return $this->setConfiguration($configuration)->configuration;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 46
    public function getConfigurationSchema(): array
96
    {
97 46
        return $this->configurationSchema ?? [];
0 ignored issues
show
Bug introduced by
The property configurationSchema does not exist on Derafu\Lib\Core\Common\Trait\ConfigurableTrait. Did you mean configuration?
Loading history...
98
    }
99
}
100