Completed
Push — nln-php7 ( 12eaac...5c4f20 )
by Nicolas
05:37
created

Command   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 85.92%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 147
ccs 61
cts 71
cp 0.8592
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 5 1
A execute() 0 36 5
A configureOutputInterface() 0 7 1
A enableFinderCache() 0 4 1
B formatValue() 0 29 6
A printHeader() 0 11 2
A getLogo() 0 34 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma;
6
7
use Karma\Logging\OutputAware;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Karma\Logging\OutputInterfaceAdapter;
12
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
13
14
class Command extends \Symfony\Component\Console\Command\Command
15
{
16
    use OutputAware;
17
18
    protected
19
        $app;
20
21 36
    public function __construct(Application $app)
22
    {
23 36
        parent::__construct();
24
25 36
        $this->app = $app;
26 36
    }
27
28 36
    protected function configure()
29
    {
30 36
        $this->addOption('cache', null, InputOption::VALUE_NONE, 'Cache the dist files list');
31 36
        $this->addOption('no-title', null, InputOption::VALUE_NONE, 'Do not display logo title');
32 36
    }
33
34 36
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36 36
        $this->configureOutputInterface($output);
37 36
        $this->printHeader($input->getOption('no-title'));
38
39 36
        $profile = $this->app['profile'];
40
41 36
        $confDir = Application::DEFAULT_CONF_DIRECTORY;
42 36
        if($profile->hasConfigurationDirectory())
43
        {
44 1
            $confDir = $profile->getConfigurationDirectory();
45
        }
46
47 36
        $masterFile = Application::DEFAULT_MASTER_FILE;
48 36
        if($profile->hasMasterFilename())
49
        {
50 1
            $masterFile = $profile->getMasterFilename();
51
        }
52
53 36
        $suffix = Application::DEFAULT_DISTFILE_SUFFIX;
54 36
        if($profile->hasTemplatesSuffix())
55
        {
56 1
            $suffix = $profile->getTemplatesSuffix();
57
        }
58
59 36
        $this->app['configuration.path']       = $confDir;
60 36
        $this->app['configuration.masterFile'] = $masterFile;
61 36
        $this->app['distFiles.suffix'] = $suffix;
62
63 36
        $this->app['logger'] = new OutputInterfaceAdapter($output);
64
65 36
        if($input->getOption('cache'))
66
        {
67 2
            $this->enableFinderCache();
68
        }
69 36
    }
70
71 36
    private function configureOutputInterface(OutputInterface $output): void
72
    {
73 36
        $style = new OutputFormatterStyle('cyan', null, array('bold'));
74 36
        $output->getFormatter()->setStyle('important', $style);
75
76 36
        $this->setOutput($output);
77 36
    }
78
79 2
    private function enableFinderCache(): void
80
    {
81 2
        $this->app['sources.fileSystem.finder'] = $this->app['sources.fileSystem.cached'];
82 2
    }
83
84 5
    protected function formatValue($value)
85
    {
86 5
        if($value === false)
87
        {
88
            $value = 'false';
89
        }
90 5
        elseif($value === true)
91
        {
92
            $value = 'true';
93
        }
94 5
        elseif($value === null)
95
        {
96
            $value = '<fg=white;options=bold>NULL</fg=white;options=bold>';
97
        }
98 5
        elseif($value === Configuration::NOT_FOUND)
99
        {
100
            $value = '<error>NOT FOUND</error>';
101
        }
102 5
        elseif(is_array($value))
103
        {
104
            array_walk($value, function(& $item) {
105
                $item = $this->formatValue($item);
106
            });
107
108
            $value = sprintf('[%s]', implode(', ', $value));
109
        }
110
111 5
        return $value;
112
    }
113
114 36
    private function printHeader($noTitle = true)
115
    {
116 36
        if($noTitle === true)
117
        {
118
            return $this->output->writeln('Karma ' . Application::VERSION);
119
        }
120
121 36
        $this->output->writeln(
122 36
           $this->getLogo($this->output->isDecorated())
123
        );
124 36
    }
125
126 36
    private function getLogo($outputDecorated = true)
127
    {
128
        $logo = <<<ASCIIART
129 36
.@@@@...@@..
130
...@@..@....
131
...@@.@@....    %K A R M A*
132
...@@.@@@...               %VERSION*
133
...@@...@@..
134
...@@....@@.
135
136
ASCIIART;
137
138 36
        $background = 'fg=magenta';
139 36
        $text = 'fg=white';
140
141 36
        $toBackground = "</$text><$background>";
142 36
        $toText = "</$background><$text>";
143
144
        // insert style tags
145 36
        if($outputDecorated === true)
146
        {
147
            $logo = str_replace('@', "$toText@$toBackground", $logo);
148
            $logo = str_replace('.', '@', $logo);
149
        }
150
151 36
        $logo = str_replace(['%', '*'], [$toText, $toBackground], $logo);
152
153 36
        return sprintf(
154 36
            '<%s>%s</%s>',
155 36
            $background,
156 36
            str_replace('VERSION', Application::VERSION, $logo),
157 36
            $background
158
        );
159
    }
160
}
161