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

AbstractHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 54
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\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