Test Failed
Push — master ( 8cbe19...3a660b )
by Esteban De La Fuente
06:47
created

Application   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
dl 0
loc 95
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A getPackageRegistry() 0 3 1
A getService() 0 3 1
A getInstance() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * LibreDTE: Biblioteca PHP (Núcleo).
7
 * Copyright (C) LibreDTE <https://www.libredte.cl>
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
20
 * GNU junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace libredte\lib\Core;
26
27
use Derafu\Backbone\Contract\PackageRegistryInterface;
28
use Derafu\Backbone\DependencyInjection\ServiceConfigurationCompilerPass;
29
use Derafu\Backbone\DependencyInjection\ServiceProcessingCompilerPass;
30
use Derafu\Kernel\Contract\EnvironmentInterface;
31
use Derafu\Kernel\MicroKernel;
32
use libredte\lib\Core\Contract\ApplicationInterface;
33
use Symfony\Component\DependencyInjection\ContainerBuilder;
34
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
35
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
36
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
37
38
/**
39
 * Clase principal de la aplicación.
40
 */
41
final class Application extends MicroKernel implements ApplicationInterface
42
{
43
    /**
44
     * Instancia de la aplicación.
45
     *
46
     * @var self
47
     */
48
    private static self $instance;
49
50
    /**
51
     * Archivos de configuración.
52
     *
53
     * @var array<string,string>
54
     */
55
    protected const CONFIG_FILES = [
56
        'services.yaml' => 'yaml',
57
    ];
58
59
    /**
60
     * Cargadores de archivos de configuración.
61
     *
62
     * @var array<class-string>
63
     */
64
    protected const CONFIG_LOADERS = [
65
        PhpFileLoader::class,
66
        YamlFileLoader::class,
67
    ];
68
69
    /**
70
     * Configura el contenedor de dependencias.
71
     *
72
     * @param ContainerConfigurator $configurator
73
     * @param ContainerBuilder $container
74
     */
75 58
    protected function configure(
76
        ContainerConfigurator $configurator,
77
        ContainerBuilder $container
78
    ): void {
79
        // Cargar configuración.
80 58
        $configurator->import(__DIR__ . '/../config/services.yaml');
81
82
        // Agregar compiler passes.
83 58
        $container->addCompilerPass(
84 58
            new ServiceProcessingCompilerPass('libredte.lib.')
85 58
        );
86 58
        $container->addCompilerPass(
87 58
            new ServiceConfigurationCompilerPass('libredte.lib.')
88 58
        );
89
    }
90
91
    /**
92
     * Entrega el registro de paquetes de la aplicación.
93
     *
94
     * @return PackageRegistry
95
     */
96 57
    public function getPackageRegistry(): PackageRegistry
97
    {
98 57
        return $this->getContainer()->get(PackageRegistryInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getContain...gistryInterface::class) could return the type null which is incompatible with the type-hinted return libredte\lib\Core\PackageRegistry. Consider adding an additional type-check to rule them out.
Loading history...
99
    }
100
101
    /**
102
     * Entrega un servicio de la aplicación.
103
     *
104
     * @param string $id
105
     * @return mixed
106
     */
107 1
    public function getService(string $id): mixed
108
    {
109 1
        return $this->getContainer()->get($id);
110
    }
111
112
    /**
113
     * Entrega la instancia de la aplicación.
114
     *
115
     * Este método se asegura de entregar una única instancia de la aplicación
116
     * mediante el patrón singleton.
117
     *
118
     * Al utilizar inyección de dependencias y registrar la aplicación de
119
     * LibreDTE en un contenedor de dependencias no será necesario, ni
120
     * recomendado, utilizar este método. En ese caso se debe utilizar solo el
121
     * contenedor de dependencias para obtener la aplicación de LibreDTE.
122
     *
123
     * @param string|EnvironmentInterface $environment
124
     * @param bool $debug
125
     * @return self
126
     */
127 59
    public static function getInstance(
128
        string|EnvironmentInterface $environment = 'dev',
129
        bool $debug = true
130
    ): self {
131 59
        if (!isset(self::$instance)) {
132 59
            self::$instance = new self($environment, $debug);
133
        }
134
135 59
        return self::$instance;
136
    }
137
}
138