AbstractComponent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 52.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 51
ccs 9
cts 17
cp 0.5294
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getWorkerConfiguration() 0 6 1
A getName() 0 10 2
A getWorker() 0 15 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\Foundation\Abstract;
26
27
use Derafu\Lib\Core\Common\Trait\ConfigurableTrait;
28
use Derafu\Lib\Core\Foundation\Contract\ComponentInterface;
29
use Derafu\Lib\Core\Foundation\Contract\WorkerInterface;
30
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface;
31
use LogicException;
32
33
/**
34
 * Clase base para los componentes de la aplicación.
35
 */
36
abstract class AbstractComponent extends AbstractService implements ComponentInterface
37
{
38
    use ConfigurableTrait;
0 ignored issues
show
Bug introduced by
The trait Derafu\Lib\Core\Common\Trait\ConfigurableTrait requires the property $configurationSchema which is not provided by Derafu\Lib\Core\Foundati...tract\AbstractComponent.
Loading history...
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 1
    public function getName(): string
44
    {
45 1
        $regex = "/\\\\Package\\\\([A-Za-z0-9_]+)\\\\Component\\\\([A-Za-z0-9_]+)\\\\/";
46
47 1
        $class = (string) $this;
48 1
        if (preg_match($regex, $class, $matches)) {
49 1
            return $matches[1] . ' ' . $matches[2];
50
        }
51
52
        return parent::getName();
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 1
    public function getWorker(string $worker): WorkerInterface
59
    {
60
        // Obtener todos los workers del paquete.
61 1
        $workers = $this->getWorkers();
62
63
        // No se encontró el worker.
64 1
        if (!isset($workers[$worker])) {
65
            throw new LogicException(sprintf(
66
                'El worker %s no existe en el componente.',
67
                $worker
68
            ));
69
        }
70
71
        // Entregar el worker encontrado.
72 1
        return $workers[$worker];
73
    }
74
75
    /**
76
     * Entrega la configuración de un worker del componente.
77
     *
78
     * @param string $worker
79
     * @return array|DataContainerInterface
80
     */
81
    protected function getWorkerConfiguration(
82
        string $worker
83
    ): array|DataContainerInterface {
84
        $config = $this->getConfiguration()->get('workers.' . $worker);
85
86
        return $config ?? [];
87
    }
88
}
89