Passed
Push — master ( 57c539...d81008 )
by Esteban De La Fuente
14:52
created

AbstractHandler::getStrategy()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 18
ccs 0
cts 12
cp 0
crap 20
rs 9.9
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\OptionsAwareTrait;
28
use Derafu\Lib\Core\Foundation\Contract\HandlerInterface;
29
use Derafu\Lib\Core\Foundation\Contract\StrategyInterface;
30
use Derafu\Lib\Core\Foundation\Exception\StrategyException;
31
32
/**
33
 * Clase base para los handlers de los workers de la aplicación.
34
 */
35
abstract class AbstractHandler extends AbstractService implements HandlerInterface
36
{
37
    use OptionsAwareTrait;
0 ignored issues
show
Bug introduced by
The trait Derafu\Lib\Core\Common\Trait\OptionsAwareTrait requires the property $optionsSchema which is not provided by Derafu\Lib\Core\Foundati...bstract\AbstractHandler.
Loading history...
38
39
    /**
40
     * Estrategias que el handler del worker puede utilizar.
41
     *
42
     * @var StrategyInterface[]
43
     */
44
    protected array $strategies;
45
46
    /**
47
     * Constructor del handler.
48
     *
49
     * @param array $strategies Estrategias que este handler puede manejar.
50
     */
51
    public function __construct(iterable $strategies = [])
52
    {
53
        $this->strategies = is_array($strategies)
54
            ? $strategies
55
            : iterator_to_array($strategies)
56
        ;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function getStrategy(string $strategy): StrategyInterface
63
    {
64
        $strategies = [$strategy];
65
        if (!str_contains($strategy, '.')) {
66
            $strategies[] = 'default.' . $strategy;
67
        }
68
69
        foreach ($strategies as $name) {
70
            if (isset($this->strategies[$name])) {
71
                return $this->strategies[$name];
72
            }
73
        }
74
75
        throw new StrategyException(sprintf(
76
            'No se encontró la estrategia %s en el handler %s (%s).',
77
            $strategy,
78
            $this->getName(),
79
            $this->getId(),
80
        ));
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function getStrategies(): array
87
    {
88
        return $this->strategies;
89
    }
90
}
91