App::getConfigurations()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 23
rs 9.9
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
        $event->getIO()->write('');
14
15
        try {
16
            $config = self::getConfigurations($event);
17
18
            $fileHandle = new FileHandle();
19
            $processor  = new Processor($fileHandle);
20
21
            $files = glob("{$config['componentsDir']}/**/index.php");
22
23
            foreach ($files as $file) {
24
                $event->getIO()->write("Writing component: <info>{$fileHandle->getComponentName($file)}</info>");
25
26
                $fileHandle->write(
27
                    $file,
28
                    $processor->compile($file),
29
                    "{$config['componentsDir']}/_static"
30
                );
31
            }
32
        } catch (\Exception $e) {
33
            $event->getIO()->write("<error>{$e->getMessage()}</error>");
34
        }
35
    }
36
37
    private static function getConfigurations(Event $event)
38
    {
39
        $baseDir = $event->getComposer()->getConfig()->get('vendor-dir') . '/../';
40
41
        $defaults = [
42
            'componentsDir' => $baseDir . 'components',
43
        ];
44
45
        $file = $baseDir . '.maristela-cli.json';
46
47
        if (!file_exists($file)) {
48
            return $defaults;
49
        }
50
51
        $config = json_decode(file_get_contents($file), true);
52
53
        if (empty($config['componentsDir'])) {
54
            throw new \Exception('The "componentsDir" is not defined on config file.');
55
        }
56
57
        $config['componentsDir'] = "{$baseDir}{$config['componentsDir']}";
58
59
        return array_merge($defaults, $config);
60
    }
61
}
62