Completed
Push — master ( 47a956...6a7dae )
by Stéphane
02:49
created

DauxCommand::prepareDaux()   D

Complexity

Conditions 9
Paths 64

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 64
nop 2
dl 0
loc 33
ccs 0
cts 20
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
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'));
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);
81
        }
82
83
        if ($input->getOption('no-cache')) {
84
            $daux->getParams()['cache'] = false;
85
        }
86
87
        return $daux;
88
    }
89
}
90