DauxCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 79
ccs 0
cts 60
cp 0
rs 10
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 15 5
A configure() 0 11 1
A applyConfiguration() 0 11 2
B prepareDaux() 0 33 9
1
<?php namespace Todaymade\Daux\Console;
2
3
use Symfony\Component\Console\Command\Command as SymfonyCommand;
4
use Symfony\Component\Console\Input\InputInterface;
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Todaymade\Daux\Daux;
8
9
class DauxCommand extends SymfonyCommand
10
{
11
    protected function configure() 
12
    {
13
        $this
14
            ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Configuration file')
15
            ->addOption('value', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Set different configuration values')
16
            ->addOption('source', 's', InputOption::VALUE_REQUIRED, 'Where to take the documentation from')
17
            ->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree')
18
            ->addOption('no-cache', null, InputOption::VALUE_NONE, 'Disable Cache');
19
20
        // HTML Format only
21
        $this->addOption('themes', 't', InputOption::VALUE_REQUIRED, 'Set a different themes directory');
22
    }
23
24
    private function setValue(&$array, $key, $value)
25
    {
26
        if (is_null($key)) {
27
            return $array = $value;
28
        }
29
        $keys = explode('.', $key);
30
        while (count($keys) > 1) {
31
            $key = array_shift($keys);
32
            if (!isset($array[$key]) || !is_array($array[$key])) {
33
                $array[$key] = [];
34
            }
35
            $array = &$array[$key];
36
        }
37
        $array[array_shift($keys)] = $value;
38
        return $array;
39
    }
40
41
    private function applyConfiguration(array $options, Daux $daux)
42
    {
43
        $values = array_map(
44
            function($value) {
45
                return array_map("trim", explode('=', $value));
46
            },
47
            $options
48
        );
49
50
        foreach ($values as $value) {
51
            $this->setValue($daux->options, $value[0], $value[1]);
52
        }
53
    }
54
55
    protected function prepareDaux(InputInterface $input, OutputInterface $output)
56
    {
57
        $daux = new Daux(Daux::STATIC_MODE, $output);
58
59
        // Set the format if requested
60
        if ($input->hasOption('format') && $input->getOption('format')) {
61
            $daux->getParams()->setFormat($input->getOption('format'));
62
        }
63
64
        // Set the source directory
65
        if ($input->getOption('source')) {
66
            $daux->getParams()->setDocumentationDirectory($input->getOption('source'));
67
        }
68
69
        if ($input->getOption('themes')) {
70
            $daux->getParams()->setThemesDirectory($input->getOption('themes'));
71
        }
72
73
        $daux->initializeConfiguration($input->getOption('configuration'));
0 ignored issues
show
Bug introduced by Stéphane Goetz
It seems like $input->getOption('configuration') can also be of type string[]; however, parameter $override_file of Todaymade\Daux\Daux::initializeConfiguration() 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

73
        $daux->initializeConfiguration(/** @scrutinizer ignore-type */ $input->getOption('configuration'));
Loading history...
74
75
        if ($input->hasOption('delete') && $input->getOption('delete')) {
76
            $daux->getParams()['confluence']['delete'] = true;
77
        }
78
79
        if ($input->hasOption('value')) {
80
            $this->applyConfiguration($input->getOption('value'), $daux);
0 ignored issues
show
Bug introduced by Stéphane Goetz
It seems like $input->getOption('value') can also be of type boolean and null and string; however, parameter $options of Todaymade\Daux\Console\D...d::applyConfiguration() does only seem to accept array, 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

80
            $this->applyConfiguration(/** @scrutinizer ignore-type */ $input->getOption('value'), $daux);
Loading history...
81
        }
82
83
        if ($input->getOption('no-cache')) {
84
            $daux->getParams()['cache'] = false;
85
        }
86
87
        return $daux;
88
    }
89
}
90