Passed
Push — pac-89--debug-report ( edb11c...f28a35 )
by Tim
03:44
created

AbstractConsolePlugin::setHelperSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Plugins\AbstractConsolePlugin
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 7
13
 *
14
 * @author    Marcus Döllerer <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli\Plugins;
22
23
use Symfony\Component\Console\Application;
24
use Symfony\Component\Console\Exception\LogicException;
25
use Symfony\Component\Console\Helper\HelperInterface;
26
use Symfony\Component\Console\Helper\HelperSet;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use TechDivision\Import\ApplicationInterface;
30
use TechDivision\Import\Plugins\AbstractPlugin;
31
32
/**
33
 * Abstract console plugin implementation containing access to console commands and helpers.
34
 *
35
 * @author    Marcus Döllerer <[email protected]>
36
 * @copyright 2020 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-cli
39
 * @link      http://www.techdivision.com
40
 */
41
abstract class AbstractConsolePlugin extends AbstractPlugin
42
{
43
44
    /**
45
     * The M2IF console application instance.
46
     *
47
     * @var \Symfony\Component\Console\Application
48
     */
49
    protected $cliApplication;
50
51
    /**
52
     * The console input instance.
53
     *
54
     * @var InputInterface
55
     */
56
    protected $input;
57
58
    /**
59
     * The console output instance.
60
     *
61
     * @var OutputInterface
62
     */
63
    protected $output;
64
65
    /**
66
     * The helper set.
67
     *
68
     * @var HelperSet
69
     */
70
    protected $helperSet;
71
72
    /**
73
     * The constructor to initialize the plugin with.
74
     *
75
     * @param \TechDivision\Import\ApplicationInterface $application The application instance
76
     *
77
     * @throws \Exception
78
     */
79
    public function __construct(ApplicationInterface $application)
80
    {
81
82
        // inject the cli application
83
        $cliApplication = $application->getContainer()->get('application');
84
        if (!$cliApplication instanceof Application) {
85
            throw new \Exception('No console application configured, please check your configuration.');
86
        }
87
        $this->setCliApplication($cliApplication);
88
89
        // set the console input
90
        $input = $application->getContainer()->get('input');
91
        if (!$input instanceof InputInterface) {
92
            throw new \Exception('No console input configured, please check your configuration.');
93
        }
94
        $this->setInput($input);
95
96
        // set the console output
97
        $output = $application->getContainer()->get('output');
98
        if (!$output instanceof OutputInterface) {
99
            throw new \Exception('No console output configured, please check your configuration.');
100
        }
101
        $this->setOutput($output);
102
103
        // inject the helper set
104
        $helperSet = $this->getCliApplication()->getHelperSet();
105
        if (!$helperSet instanceof HelperSet) {
0 ignored issues
show
introduced by
$helperSet is always a sub-type of Symfony\Component\Console\Helper\HelperSet.
Loading history...
106
            throw new LogicException('No HelperSet is defined.');
107
        }
108
        $this->setHelperSet($helperSet);
109
110
        parent::__construct($application);
111
    }
112
113
    /**
114
     * Return's the console application instance.
115
     *
116
     * @return \Symfony\Component\Console\Application The console application instance
117
     */
118
    public function getCliApplication()
119
    {
120
        return $this->cliApplication;
121
    }
122
123
    /**
124
     * Set's the console application instance.
125
     *
126
     * @param \Symfony\Component\Console\Application $cliApplication The console application instance
127
     *
128
     * @return void
129
     */
130
    public function setCliApplication(Application $cliApplication)
131
    {
132
        $this->cliApplication = $cliApplication;
133
    }
134
135
    /**
136
     * Return's the console input instance.
137
     *
138
     * @return \Symfony\Component\Console\Input\InputInterface The console input instance
139
     */
140
    public function getInput()
141
    {
142
        return $this->input;
143
    }
144
145
    /**
146
     * Set's the console input instance.
147
     *
148
     * @param \Symfony\Component\Console\Input\InputInterface $input The console input instance
149
     *
150
     * @return void
151
     */
152
    public function setInput(InputInterface $input)
153
    {
154
        $this->input = $input;
155
    }
156
157
    /**
158
     * Return's the console output instance.
159
     *
160
     * @return \Symfony\Component\Console\Output\OutputInterface The console output instance
161
     */
162
    public function getOutput()
163
    {
164
        return $this->output;
165
    }
166
167
    /**
168
     * Set's the console output instance.
169
     *
170
     * @param \Symfony\Component\Console\Output\OutputInterface $output The console output instance
171
     *
172
     * @return void
173
     */
174
    public function setOutput(OutputInterface $output)
175
    {
176
        $this->output = $output;
177
    }
178
179
    /**
180
     * Return's the helper set instance.
181
     *
182
     * @return \Symfony\Component\Console\Helper\HelperSet The helper set instance
183
     */
184
    public function getHelperSet()
185
    {
186
        return $this->helperSet;
187
    }
188
189
    /**
190
     * Set's the helper set instance.
191
     *
192
     * @param \Symfony\Component\Console\Helper\HelperSet $helperSet The helper set instance
193
     *
194
     * @return void
195
     */
196
    public function setHelperSet(HelperSet $helperSet)
197
    {
198
        $this->helperSet = $helperSet;
199
    }
200
201
    /**
202
     * Retrieve a helper by name.
203
     *
204
     * @param string $name The name of the helper to retrieve
205
     *
206
     * @return \Symfony\Component\Console\Helper\HelperInterface The helper instance
207
     */
208
    public function getHelper($name)
209
    {
210
        return $this->getHelperSet()->get($name);
211
    }
212
}
213