Completed
Push — master ( 3d56ef...bb0d18 )
by Tim
59:19 queued 51:22
created

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