Passed
Push — nln-php7 ( 12eaac )
by Nicolas
05:28
created

Command::printHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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