Passed
Push — master ( ae3156...6fb22a )
by Esteban De La Fuente
03:24
created

Kernel::initialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 34
ccs 20
cts 20
cp 1
crap 3
rs 9.7
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
     * Constructor del núcleo de la aplicación.
45
     */
46 16
    public function __construct()
47
    {
48 16
        $this->container = new ContainerBuilder();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 16
    public function initialize(ConfigurationInterface $configuration): void
55
    {
56
        // Registrar configuración como servicio.
57 16
        $this->container
58 16
            ->register(ConfigurationInterface::class)
59 16
            ->setSynthetic(true)
60 16
        ;
61 16
        $this->container->set(ConfigurationInterface::class, $configuration);
62
63
        // Cargar parámetros al contenedor.
64 16
        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

64
        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...
65 16
            $this->container->setParameter($name, $value);
66
        }
67
68
        // Registrar servicios.
69 16
        $serviceRegistryClass = $configuration->getServiceRegistryClass();
70 16
        $serviceRegistry = new $serviceRegistryClass();
71 16
        $serviceRegistry->register($this->container);
72
73
        // Procesar servicios con un Compiler Pass si está definido.
74 16
        $compilerPassClass = $configuration->getCompilerPassClass();
75 16
        if ($compilerPassClass !== null) {
76 16
            $servicesPrefix = $configuration->getServicesPrefix();
77 16
            $this->container->addCompilerPass(
78 16
                new $compilerPassClass($servicesPrefix)
79 16
            );
80
        }
81
82
        // Agregar el compiler pass para inyectar las configuraciones.
83 16
        $compilerPassClass = ServiceConfigurationCompilerPass::class;
84 16
        $this->container->addCompilerPass(new $compilerPassClass());
85
86
        // Compilar el contenedor de servicios.
87 16
        $this->container->compile();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 15
    public function getContainer(): ContainerBuilder
94
    {
95 15
        return $this->container;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 7
    public function getConfiguration(): ConfigurationInterface
102
    {
103 7
        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...
104
    }
105
}
106