Passed
Push — master ( af8133...2361e9 )
by Dmitrij
03:20
created

BaseCommand::config()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace HotRodCli\Commands;
4
5
use HotRodCli\Jobs\Module\ReplaceText;
6
use Symfony\Component\Console\Command\Command;
7
use HotRodCli\AppContainer;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class BaseCommand extends Command
12
{
13
    protected $appContainer;
14
15
    protected $info;
16
17
    protected $jobs = [];
18
19
    protected $processors = [];
20
21
    protected $configs = [
22
        'arguments' => [],
23
        'options' => [],
24
        'description' => '',
25
        'name' => '',
26
        'help' => '',
27
        'info' => ''
28
    ];
29
30
    /** @var  ReplaceText */
31
    protected $replaceTextJob;
32
33
    public function __construct(AppContainer $appContainer)
34
    {
35
        $this->appContainer = $appContainer;
36
        $this->replaceTextJob = $this->appContainer->resolve(ReplaceText::class);
37
38
        parent::__construct();
39
    }
40
41
    public function setJobs()
42
    {
43
        foreach ($this->jobs as $key => $job) {
44
            $this->jobs[$key] = $this->appContainer->resolve($key);
45
        }
46
    }
47
48
    public function setProcessors()
49
    {
50
        foreach ($this->processors as $key => $processor) {
51
            $this->processors[$key] = $this->appContainer->resolve($key);
52
        }
53
    }
54
55
    protected function config()
56
    {
57
        $configs = $this->configs;
58
        $this->setName($configs['name'])->setDescription($configs['description'])->setHelp($configs['help']);
59
60
        $this->setConfigArguments($configs);
61
62
        $this->setConfigOptions($configs);
63
64
        if (isset($configs['info'])) {
65
            $this->setInfo($configs['info']);
66
        }
67
    }
68
69
    public function runProcessors(InputInterface $input, OutputInterface $output)
70
    {
71
        foreach ($this->processors as $processor) {
72
            $processor($input, $output);
73
        }
74
75
        return $this;
76
    }
77
78
    public function replaceTextsSequence(array $sequence, string $destination)
79
    {
80
        foreach ($sequence as $needle => $value) {
81
            $this->replaceTextJob->handle($needle, $value, $destination);
82
        }
83
84
        return $this;
85
    }
86
87
    public function setInfo(string $info)
88
    {
89
        $this->info = $info;
90
91
        return $this;
92
    }
93
94
    public function getInfo()
95
    {
96
        return $this->info;
97
    }
98
99
    protected function setConfigArguments($configs): void
100
    {
101
        foreach ($configs['arguments'] as $argument) {
102
            $this->addArgument(
103
                $argument['name'],
104
                $argument['mode'],
105
                $argument['description']
106
            );
107
        }
108
    }
109
110
    protected function setConfigOptions($configs): void
111
    {
112
        foreach ($configs['options'] as $option) {
113
            $this->addOption(
114
                $option['name'],
115
                $option['shortcut'],
116
                $option['mode'],
117
                $option['description']
118
            );
119
        }
120
    }
121
}
122