Kernel::initialize()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 37
ccs 21
cts 21
cp 1
crap 3
rs 9.6666
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;
26
27
use Derafu\Lib\Core\Foundation\Contract\ConfigurationInterface;
28
use Derafu\Lib\Core\Foundation\Contract\KernelInterface;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
31
/**
32
 * Núcleo de la aplicación.
33
 */
34
class Kernel implements KernelInterface
35
{
36
    /**
37
     * Contenedor de servicios de la aplicación.
38
     *
39
     * @var ContainerBuilder
40
     */
41
    private ContainerBuilder $container;
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 17
    public function __construct(ConfigurationInterface $configuration)
47
    {
48 17
        $this->initialize($configuration);
49
    }
50
51
    /**
52
     * Inicializa el núcleo.
53
     *
54
     * @param ConfigurationInterface $configuration
55
     * @return void
56
     */
57 17
    private function initialize(ConfigurationInterface $configuration): void
58
    {
59
        // Crear el contenedor de dependencias.
60 17
        $this->container = new ContainerBuilder();
61
62
        // Registrar configuración como servicio.
63 17
        $this->container
64 17
            ->register(ConfigurationInterface::class)
65 17
            ->setSynthetic(true)
66 17
        ;
67 17
        $this->container->set(ConfigurationInterface::class, $configuration);
68
69
        // Cargar parámetros al contenedor.
70 17
        foreach ($configuration->getParameters() as $name => $value) {
0 ignored issues
show
Bug introduced by
The method getParameters() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        foreach ($configuration->/** @scrutinizer ignore-call */ getParameters() as $name => $value) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71 17
            $this->container->setParameter($name, $value);
72
        }
73
74
        // Registrar servicios.
75 17
        $serviceRegistryClass = $configuration->getServiceRegistryClass();
76 17
        $serviceRegistry = new $serviceRegistryClass();
77 17
        $serviceRegistry->register($this->container);
78
79
        // Procesar servicios con un Compiler Pass si está definido.
80 17
        $compilerPassClass = $configuration->getCompilerPassClass();
81 17
        if ($compilerPassClass !== null) {
82 17
            $servicesPrefix = $configuration->getServicesPrefix();
83 17
            $this->container->addCompilerPass(
84 17
                new $compilerPassClass($servicesPrefix)
85 17
            );
86
        }
87
88
        // Agregar el compiler pass para inyectar las configuraciones.
89 17
        $compilerPassClass = ServiceConfigurationCompilerPass::class;
90 17
        $this->container->addCompilerPass(new $compilerPassClass());
91
92
        // Compilar el contenedor de servicios.
93 17
        $this->container->compile();
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 16
    public function getContainer(): ContainerBuilder
100
    {
101 16
        return $this->container;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 9
    public function getConfiguration(): ConfigurationInterface
108
    {
109 9
        return $this->container->get(ConfigurationInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->container-...rationInterface::class) could return the type null which is incompatible with the type-hinted return Derafu\Lib\Core\Foundati...\ConfigurationInterface. Consider adding an additional type-check to rule them out.
Loading history...
110
    }
111
}
112