Passed
Pull Request — master (#23)
by Dmitrij
02:57
created

BaseCommand::setInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
        foreach ($configs['arguments'] as $argument) {
61
            $this->addArgument(
62
                $argument['name'],
63
                $argument['mode'],
64
                $argument['description']
65
            );
66
        }
67
68
        foreach ($configs['options'] as $option) {
69
            $this->addOption(
70
                $option['name'],
71
                $option['shortcut'],
72
                $option['mode'],
73
                $option['description']
74
            );
75
        }
76
77
        if (isset($configs['info'])) {
78
            $this->setInfo($configs['info']);
79
        }
80
    }
81
82
    public function runProcessors(InputInterface $input, OutputInterface $output)
83
    {
84
        foreach ($this->processors as $processor) {
85
            $processor($input, $output);
86
        }
87
88
        return $this;
89
    }
90
91
    public function replaceTextsSequence(array $sequence, string $destination)
92
    {
93
        foreach ($sequence as $needle => $value) {
94
            $this->replaceTextJob->handle($needle, $value, $destination);
95
        }
96
97
        return $this;
98
    }
99
100
    public function setInfo(string $info)
101
    {
102
        $this->info = $info;
103
104
        return $this;
105
    }
106
107
    public function getInfo()
108
    {
109
        return $this->info;
110
    }
111
}
112