Completed
Pull Request — master (#9)
by ANTHONIUS
02:18
created

ApplicationFactory   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 48
dl 0
loc 308
rs 8.4864
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getCachePathPrefix() 0 8 1
B getConfiguration() 0 35 6
D loadDirectoryFromAutoloader() 0 25 9
B compileContainer() 0 37 6
A __construct() 0 14 3
D loadDotEnv() 0 38 9
A addAutoload() 0 10 2
A loadPlugins() 0 18 4
A hasPlugin() 0 3 1
A getContainer() 0 3 1
A ensureDir() 0 7 1
A processCoreConfig() 0 13 2
A registerPlugin() 0 7 2
A boot() 0 9 1

How to fix   Complexity   

Complex Class

Complex classes like ApplicationFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ApplicationFactory, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core;
15
16
use Composer\Autoload\ClassLoader;
17
use Dotfiles\Core\DI\Compiler\CommandPass;
18
use Dotfiles\Core\DI\Compiler\ListenerPass;
19
use Dotfiles\Core\DI\Parameters;
20
use Dotfiles\Core\Util\Toolkit;
21
use Symfony\Component\Config\ConfigCache;
22
use Symfony\Component\Config\Definition\Processor;
23
use Symfony\Component\Config\FileLocator;
24
use Symfony\Component\Config\Resource\FileResource;
25
use Symfony\Component\DependencyInjection\Container;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dotfiles\Core\Container. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
28
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
29
use Symfony\Component\Dotenv\Dotenv;
30
use Symfony\Component\Finder\Finder;
31
use Symfony\Component\Finder\SplFileInfo;
32
use Symfony\Component\Yaml\Yaml;
33
34
class ApplicationFactory
35
{
36
    /**
37
     * @var Container
38
     */
39
    private $container;
40
41
    /**
42
     * @var bool
43
     */
44
    private $debug = false;
45
46
    /**
47
     * @var string
48
     */
49
    private $env;
50
51
    /**
52
     * @var array
53
     */
54
    private $envFiles = array();
55
56
    /**
57
     * @var Plugin[]
58
     */
59
    private $plugins = array();
60
61
    public function __construct()
62
    {
63
        $files = array(__DIR__.'/Resources/default.env');
64
65
        // $PWD/.env always win
66
        $cwd = getcwd();
67
        if (is_file($file = $cwd.'/.env.dist')) {
68
            $files[] = $file;
69
        }
70
        if (is_file($file = $cwd.'/.env')) {
71
            $files[] = $file;
72
        }
73
74
        $this->envFiles = $files;
75
    }
76
77
    /**
78
     * @return $this
79
     */
80
    public function boot(): self
81
    {
82
        $this->loadDotEnv();
83
        $this->addAutoload();
84
        $this->loadPlugins();
85
        $this->compileContainer();
86
        $this->ensureDir();
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return Container
93
     */
94
    public function getContainer(): Container
95
    {
96
        return $this->container;
97
    }
98
99
    /**
100
     * @param string $name
101
     *
102
     * @return bool
103
     */
104
    public function hasPlugin(string $name): bool
105
    {
106
        return array_key_exists($name, $this->plugins);
107
    }
108
109
    private function addAutoload(): void
110
    {
111
        $baseDir = getenv('DOTFILES_BACKUP_DIR');
112
        $autoloadFile = $baseDir.'/vendor/autoload.php';
113
114
        // ignore if files is already loaded in phar file
115
        if (
116
            is_file($autoloadFile)
117
        ) {
118
            include_once $autoloadFile;
119
        }
120
    }
121
122
    private function compileContainer(): void
123
    {
124
        $configs = $this->getConfiguration();
125
        //$paramaterBag = new ParameterBag();
126
        $builder = new ContainerBuilder();
127
        $this->processCoreConfig($configs, $builder);
128
        // processing core configuration
129
130
        /* @var Plugin $plugin */
131
        foreach ($this->plugins as $name => $plugin) {
132
            $pluginConfig = array_key_exists($name, $configs) ? $configs[$name] : array();
133
            $plugin->load($pluginConfig, $builder);
134
        }
135
136
        $cachePath = $this->getCachePathPrefix().'/container.php';
137
        $cache = new ConfigCache($cachePath, $this->debug);
138
        if (!$cache->isFresh() || 'dev' == $this->env) {
139
            $builder->addCompilerPass(new CommandPass());
140
            $builder->addCompilerPass(new ListenerPass());
141
            $builder->compile(true);
142
            $dumper = new PhpDumper($builder);
143
            $resources = $this->envFiles;
144
            array_walk($resources, function (&$item): void {
145
                $item = new FileResource($item);
146
            });
147
            $resources = array_merge($resources, $builder->getResources());
148
            $cache->write($dumper->dump(array('class' => 'CachedContainer')), $resources);
0 ignored issues
show
Bug introduced by
It seems like $dumper->dump(array('cla... => 'CachedContainer')) can also be of type array; however, parameter $content of Symfony\Component\Config...kerConfigCache::write() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

148
            $cache->write(/** @scrutinizer ignore-type */ $dumper->dump(array('class' => 'CachedContainer')), $resources);
Loading history...
149
        }
150
        if (!class_exists('CachedContainer')) {
151
            include_once $cachePath;
152
        }
153
        $container = new \CachedContainer();
0 ignored issues
show
Bug introduced by
The type CachedContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
155
        $parameters = new Parameters();
156
        $parameters->setConfigs($container->getParameterBag()->all());
157
        $container->set('dotfiles.parameters', $parameters);
158
        $this->container = $container;
159
    }
160
161
    private function ensureDir(): void
162
    {
163
        /* @var Parameters $parameters */
164
        $parameters = $this->container->get('dotfiles.parameters');
165
        Toolkit::ensureDir($parameters->get('dotfiles.install_dir'));
166
        Toolkit::ensureDir($parameters->get('dotfiles.bin_dir'));
167
        Toolkit::ensureDir($parameters->get('dotfiles.vendor_dir'));
168
    }
169
170
    private function getCachePathPrefix()
171
    {
172
        // using argv command to differ each dotfiles executable file
173
        global $argv;
174
        $command = $argv[0];
175
        $cacheDir = getenv('DOTFILES_CACHE_DIR');
176
177
        return $cacheDir.DIRECTORY_SEPARATOR.crc32($command);
178
    }
179
180
    private function getConfiguration()
181
    {
182
        $configDir = getenv('DOTFILES_CONFIG_DIR');
183
        if (!is_dir($configDir)) {
184
            return array();
185
        }
186
        $cacheFile = $this->getCachePathPrefix().'/config.php';
187
        $cache = new ConfigCache($cacheFile, $this->debug);
188
        if (!$cache->isFresh() || 'dev' === $this->env) {
189
            $finder = Finder::create()
190
                ->name('*.yaml')
191
                ->name('*.yml')
192
                ->in($configDir)
193
            ;
194
            $configs = array();
195
            $configFiles = array();
196
            /* @var SplFileInfo $file */
197
            foreach ($finder->files() as $file) {
198
                $parsed = Yaml::parseFile($file->getRealPath());
199
                if (is_array($parsed)) {
200
                    $configs = array_merge_recursive($configs, $parsed);
201
                }
202
                $configFiles[] = new FileResource($file->getRealPath());
203
            }
204
            $template = "<?php\n/* ParameterBag Cache File Generated at %s */\nreturn %s;\n";
205
            $time = new \DateTime();
206
            $contents = sprintf(
207
                $template,
208
                $time->format('Y-m-d H:i:s'),
209
                var_export($configs, true)
210
            );
211
            $cache->write($contents, $configFiles);
212
        }
213
214
        return include $cacheFile;
215
    }
216
217
    /**
218
     * Load available plugins directory.
219
     *
220
     * @return array
221
     */
222
    private function loadDirectoryFromAutoloader()
223
    {
224
        $spl = spl_autoload_functions();
225
226
        $dirs = array();
227
        foreach ($spl as $item) {
228
            $object = $item[0];
229
            if (!$object instanceof ClassLoader) {
230
                continue;
231
            }
232
            $temp = array_merge($object->getPrefixes(), $object->getPrefixesPsr4());
233
            foreach ($temp as $name => $dir) {
234
                if (false === strpos($name, 'Dotfiles')) {
235
                    continue;
236
                }
237
                foreach ($dir as $num => $path) {
238
                    $path = realpath($path);
239
                    if ($path && is_dir($path) && !in_array($path, $dirs)) {
240
                        $dirs[] = $path;
241
                    }
242
                }
243
            }
244
        }
245
246
        return $dirs;
247
    }
248
249
    private function loadDotEnv(): void
250
    {
251
        global $argv;
252
        // set temp dir based on OS
253
        putenv('DOTFILES_TEMP_DIR='.sys_get_temp_dir().'/dotfiles');
254
        $dryRun = in_array('--dry-run', $argv) ? true : false;
255
        putenv('DOTFILES_DRY_RUN='.$dryRun);
256
257
        $files = $this->envFiles;
258
        $env = new Dotenv();
259
        if (count($files) > 0) {
260
            call_user_func_array(array($env, 'load'), $files);
261
        }
262
263
        $dev = getenv('DOTFILES_ENV');
264
        if (
265
            'dev' !== $dev && is_file($file = getenv('HOME').'/.dotfiles_profile')) {
266
            $env->load($file);
267
        }
268
269
        $this->debug = (bool) getenv('DOTFILES_DEBUG');
270
        $this->env = getenv('DOTFILES_ENV');
271
272
        $homeDir = getenv('DOTFILES_HOME_DIR');
273
        $backupDir = getenv('DOTFILES_BACKUP_DIR');
274
        if (!getenv('DOTFILES_INSTALL_DIR')) {
275
            putenv('DOTFILES_INSTALL_DIR='.$homeDir.'/.dotfiles');
276
        }
277
278
        if (!getenv('DOTFILES_CONFIG_DIR')) {
279
            putenv('DOTFILES_CONFIG_DIR='.getenv('DOTFILES_BACKUP_DIR').'/config');
280
        }
281
282
        if (!getenv('DOTFILES_CACHE_DIR')) {
283
            putenv('DOTFILES_CACHE_DIR='.$backupDir.'/var/cache');
284
        }
285
        if (!getenv('DOTFILES_LOG_DIR')) {
286
            putenv('DOTFILES_LOG_DIR='.$backupDir.'/var/log');
287
        }
288
    }
289
290
    /**
291
     * Load available plugins.
292
     */
293
    private function loadPlugins(): void
294
    {
295
        $finder = Finder::create();
296
        $finder
297
            ->name('*Plugin.php')
298
        ;
299
        if (is_dir($dir = __DIR__.'/../Plugins')) {
300
            $finder->in(__DIR__.'/../Plugins');
301
        }
302
        $dirs = $this->loadDirectoryFromAutoloader();
303
        $finder->in($dirs);
304
        foreach ($finder->files() as $file) {
305
            $namespace = 'Dotfiles\\Plugins\\'.str_replace('Plugin.php', '', $file->getFileName());
306
            $className = $namespace.'\\'.str_replace('.php', '', $file->getFileName());
307
            if (class_exists($className)) {
308
                /* @var \Dotfiles\Core\Plugin $plugin */
309
                $plugin = new $className();
310
                $this->registerPlugin($plugin);
311
            }
312
        }
313
    }
314
315
    private function processCoreConfig(array $configs, ContainerBuilder $builder): void
316
    {
317
        $dotfileConfig = array_key_exists('dotfiles', $configs) ? $configs['dotfiles'] : array();
318
        $processor = new Processor();
319
        $parameters = $processor->processConfiguration(new Configuration(), $dotfileConfig);
320
        $parameters = array('dotfiles' => $parameters);
321
        Toolkit::flattenArray($parameters);
322
323
        $builder->getParameterBag()->add($parameters);
324
325
        $locator = new FileLocator(__DIR__.'/Resources/config');
326
        $loader = new YamlFileLoader($builder, $locator);
327
        $loader->load('services.yaml');
328
    }
329
330
    /**
331
     * Register plugin.
332
     *
333
     * @param Plugin $plugin
334
     */
335
    private function registerPlugin(Plugin $plugin): void
336
    {
337
        if ($this->hasPlugin($plugin->getName())) {
338
            return;
339
        }
340
341
        $this->plugins[$plugin->getName()] = $plugin;
342
    }
343
}
344