Failed Conditions
Pull Request — develop (#1)
by
unknown
03:17 queued 01:37
created

App::buildComponents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Maristela\Cli;
4
5
use Composer\Script\Event;
6
use Maristela\Cli\FileHandle;
7
use Maristela\Cli\Processor\Classic as Processor;
8
9
class App
10
{
11
    public static function buildComponents(Event $event)
12
    {
13
        $config = self::getConfigurations($event);
14
15
        $fileHandle = new FileHandle();
16
        $processor  = new Processor($fileHandle);
17
18
        $files =  glob("{$config['componentsDir']}/**/index.php");
19
20
        foreach ($files as $file) {
21
            $fileHandle->write(
22
                $file,
23
                $processor->compile($file),
24
                "{$config['componentsDir']}/_static"
25
            );
26
        }
27
    }
28
29
    private static function getConfigurations(Event $event)
30
    {
31
        $baseDir = $event->getComposer()->getConfig()->get('vendor-dir') . '/../';
32
33
        $defaults = [
34
            'componentsDir' => $baseDir . 'components/',
35
        ];
36
37
        $file = $baseDir . '.maristela-cli.json';
38
39
        if (!file_exists($file)) {
40
            return $defaults;
41
        }
42
43
        $config = json_decode(file_get_contents($file), true);
44
45
        if (empty($config['componentsDir'])) {
46
            throw new Exception('The "componentsDir" is not defined on config file.');
0 ignored issues
show
Bug introduced by
The type Maristela\Cli\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
47
        }
48
49
        $config['componentsDir'] = "{$baseDir}{$config['componentsDir']}";
50
51
        return array_merge($defaults, $config);
52
    }
53
}
54