Passed
Push — develop ( 429d81...1647ec )
by Brent
02:48
created

App::loadPluginServices()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher;
4
5
use Illuminate\Support\Arr;
6
use Pageon\Config;
7
use Stitcher\Application\DevelopmentServer;
8
use Stitcher\Application\ProductionServer;
9
use Stitcher\Application\Router;
10
use Stitcher\Exception\InvalidConfiguration;
11
use Stitcher\Exception\InvalidPlugin;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
16
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
17
18
class App
19
{
20
    /** @var ContainerBuilder */
21
    protected static $container;
22
23 9
    public static function init()
24
    {
25 9
        Config::init();
26
27 9
        self::$container = new ContainerBuilder();
28
29 9
        self::loadConfig(Config::all());
30
31 9
        self::loadServices('services.yaml');
32
33 9
        self::loadPlugins();
34
35 9
        self::loadRoutes();
36 9
    }
37
38 8
    public static function get(string $id)
39
    {
40 8
        return self::$container->get($id);
41
    }
42
43 1 View Code Duplication
    public static function developmentServer(): DevelopmentServer
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        try {
46
            /** @var DevelopmentServer $server */
47 1
            $server = self::get(DevelopmentServer::class);
48
49 1
            return $server;
50
        } catch (ParameterNotFoundException $e) {
51
            throw InvalidConfiguration::missingParameter($e->getKey());
52
        }
53
    }
54
55 1 View Code Duplication
    public static function productionServer(): ProductionServer
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        try {
58
            /** @var ProductionServer $server */
59 1
            $server = self::get(ProductionServer::class);
60
61 1
            return $server;
62
        } catch (ParameterNotFoundException $e) {
63
            throw InvalidConfiguration::missingParameter($e->getKey());
64
        }
65
    }
66
67 2
    public static function router(): Router
68
    {
69
        /** @var Router $router */
70 2
        $router = self::get(Router::class);
71
72 2
        return $router;
73
    }
74
75 9
    protected static function loadConfig(array $config): void
76
    {
77 9
        foreach ($config as $key => $value) {
78 9
            self::$container->setParameter($key, $value);
79
        }
80 9
    }
81
82 9
    protected static function loadServices(string $servicesPath): void
83
    {
84 9
        $loader = new YamlFileLoader(self::$container, new FileLocator(__DIR__));
85
86 9
        $loader->load($servicesPath);
87
88
        /** @var Definition $definition */
89 9
        foreach (self::$container->getDefinitions() as $id => $definition) {
90 9
            self::$container->setAlias($definition->getClass(), $id);
91
        }
92 9
    }
93
94 9
    protected static function loadPlugins(): void
95
    {
96 9
        foreach (Config::plugins() as $pluginClass) {
97 9
            if (!class_implements($pluginClass, Plugin::class)) {
98
                throw InvalidPlugin::doesntImplementPluginInterface($pluginClass);
99
            }
100
101 9
            self::loadPluginConfiguration($pluginClass);
102
103 9
            self::loadPluginServices($pluginClass);
104
105 9
            self::registerPluginDefinition($pluginClass);
106
        }
107 9
    }
108
109 9
    protected static function loadPluginConfiguration(string $pluginClass): void
110
    {
111 9
        $configurationPath = forward_static_call([$pluginClass, 'getConfigurationPath']);
112
113 9
        if (!$configurationPath) {
114
            return;
115
        }
116
117 9
        if (!file_exists($configurationPath)) {
118
            throw InvalidPlugin::configurationFileNotFound($pluginClass, $configurationPath);
119
        }
120
121 9
        $pluginConfiguration = require $configurationPath;
122
123 9
        if (!is_array($pluginConfiguration)) {
124
            throw InvalidPlugin::configurationMustBeArray($pluginClass, $configurationPath);
125
        }
126
127 9
        self::loadConfig(Arr::dot($pluginConfiguration));
128 9
    }
129
130 9
    protected static function loadPluginServices(string $pluginClass): void
131
    {
132 9
        $servicesPath = forward_static_call([$pluginClass, 'getServicesPath']);
133
134 9
        if (!$servicesPath) {
135
            return;
136
        }
137
138 9
        if (!file_exists($servicesPath)) {
139
            throw InvalidPlugin::serviceFileNotFound($pluginClass, $servicesPath);
140
        }
141
142 9
        self::loadServices($servicesPath);
143 9
    }
144
145 9
    protected static function registerPluginDefinition(string $pluginClass): void
146
    {
147 9
        $definition = new Definition($pluginClass);
148
149 9
        $definition->setAutowired(true);
150
151 9
        self::$container->setDefinition($pluginClass, $definition);
152 9
    }
153
154 9
    protected static function loadRoutes(): void
155
    {
156 9
        $routeFile = File::path('src/routes.php');
157
158 9
        if (! file_exists($routeFile)) {
159
            return;
160
        }
161
162 9
        require_once $routeFile;
163 9
    }
164
}
165