AbstractHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 52
ccs 0
cts 20
cp 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStrategy() 0 18 4
A __construct() 0 5 2
A getStrategies() 0 3 1
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\Foundation\Contract\HandlerInterface;
28
use Derafu\Lib\Core\Foundation\Contract\StrategyInterface;
29
use Derafu\Lib\Core\Foundation\Exception\StrategyException;
30
31
/**
32
 * Clase base para los handlers de los workers de la aplicación.
33
 */
34
abstract class AbstractHandler extends AbstractService implements HandlerInterface
35
{
36
    /**
37
     * Estrategias que el handler del worker puede utilizar.
38
     *
39
     * @var StrategyInterface[]
40
     */
41
    protected array $strategies;
42
43
    /**
44
     * Constructor del handler.
45
     *
46
     * @param array $strategies Estrategias que este handler puede manejar.
47
     */
48
    public function __construct(iterable $strategies = [])
49
    {
50
        $this->strategies = is_array($strategies)
51
            ? $strategies
52
            : iterator_to_array($strategies)
53
        ;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function getStrategy(string $strategy): StrategyInterface
60
    {
61
        $strategies = [$strategy];
62
        if (!str_contains($strategy, '.')) {
63
            $strategies[] = 'default.' . $strategy;
64
        }
65
66
        foreach ($strategies as $name) {
67
            if (isset($this->strategies[$name])) {
68
                return $this->strategies[$name];
69
            }
70
        }
71
72
        throw new StrategyException(sprintf(
73
            'No se encontró la estrategia %s en el handler %s (%s).',
74
            $strategy,
75
            $this->getName(),
76
            $this->getId(),
77
        ));
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function getStrategies(): array
84
    {
85
        return $this->strategies;
86
    }
87
}
88