GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch feature/normalise_config_files... (35a6c3)
by Marc
03:27
created

AbstractCommand::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-21
7
 *
8
 */
9
10
11
namespace Chapi\Commands;
12
13
14
use Chapi\Component\Command\CommandUtils;
15
use Chapi\Component\Config\ChapiConfig;
16
use Chapi\Component\Config\ChapiConfigInterface;
17
use Chapi\Component\DependencyInjection\Loader\ChapiConfigLoader;
18
use Symfony\Component\Config\FileLocator;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
25
use Symfony\Component\Yaml\Parser;
26
27
abstract class AbstractCommand extends Command
28
{
29
    const FOLDER_RESOURCES = '/../../app/Resources/config/';
30
31
    /**
32
     * @var InputInterface
33
     */
34
    protected $oInput;
35
36
    /**
37
     * @var OutputInterface
38
     */
39
    protected $oOutput;
40
41
    /**
42
     * @var ContainerBuilder
43
     */
44
    private $oContainer;
45
46
    /**
47
     * @var string
48
     */
49
    private static $sHomeDir = '';
50
51
    /**
52
     * @inheritdoc
53
     */
54 24
    public function __construct($name = null)
55
    {
56 24
        parent::__construct($name);
57
58
        // setup default --profile option for all commands
59 24
        $this->addOption(
60 24
            'profile',
61 24
            null,
62 24
            InputOption::VALUE_OPTIONAL,
63 24
            'Look for global confguration in .../parameters_<profile>.yml instead of .../parameters.yml',
64 24
            'default'
65
        );
66 24
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 22
    protected function initialize(InputInterface $oInput, OutputInterface $oOutput)
72
    {
73 22
        $this->oInput = $oInput;
74 22
        $this->oOutput = $oOutput;
75
76 22
        parent::initialize($oInput, $oOutput);
77 22
    }
78
79
    /**
80
     * Executes the current command.
81
     *
82
     * This method is not abstract because you can use this class
83
     * as a concrete class. In this case, instead of defining the
84
     * execute() method, you set the code to execute by passing
85
     * a Closure to the setCode() method.
86
     *
87
     * @param InputInterface $oInput An InputInterface instance
88
     * @param OutputInterface $oOutput An OutputInterface instance
89
     *
90
     * @return integer null or 0 if everything went fine, or an error code
91
     *
92
     * @throws \LogicException When this abstract method is not implemented
93
     *
94
     * @see setCode()
95
     */
96 14
    protected function execute(InputInterface $oInput, OutputInterface $oOutput)
97
    {
98 14
        if (!$this->isAppRunable())
99
        {
100
            return 1;
101
        }
102
103
        // set output for verbosity handling
104
        /** @var \Symfony\Bridge\Monolog\Handler\ConsoleHandler $_oConsoleHandler */
105 14
        $_oConsoleHandler = $this->getContainer()->get('ConsoleHandler');
106 14
        $_oConsoleHandler->setOutput($this->oOutput);
107
108 14
        return $this->process();
109
    }
110
111
    /**
112
     * @return int
113
     */
114
    abstract protected function process();
115
116
    /**
117
     * @return ContainerBuilder
118
     */
119
    protected function getContainer()
120
    {
121
        if (is_null($this->oContainer))
122
        {
123
            $_oContainer = $this->loadContainer();
124
125
            // load services
126
            $_oLoader = new YamlFileLoader($_oContainer, new FileLocator(__DIR__ . self::FOLDER_RESOURCES));
127
            $_oLoader->load('services.yml');
128
129
            $this->oContainer = $_oContainer;
130
        }
131
132
        return $this->oContainer;
133
    }
134
135
    /**
136
     * @return string
137
     */
138 5
    protected function getParameterFileName()
139
    {
140 5
        return ChapiConfigInterface::CONFIG_FILE_NAME;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146 3
    protected function isAppRunable()
147
    {
148
        // one file has to exist
149
        if (
150 3
            !file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . ChapiConfigInterface::CONFIG_FILE_NAME)
151 3
            && !file_exists($this->getWorkingDir() . DIRECTORY_SEPARATOR . ChapiConfigInterface::CONFIG_FILE_NAME)
152
        )
153
        {
154
            $this->oOutput->writeln(sprintf(
155
                '<error>%s</error>',
156
                'No parameter file found. Please run "configure" command for initial setup or add a local `.chapiconfig` to your working directory.'
157
            ));
158
            return false;
159
        }
160
161 3
        return true;
162
    }
163
164
    /**
165
     * @return string
166
     */
167 1
    protected function getHomeDir()
168
    {
169 1
        if (!empty(self::$sHomeDir))
170
        {
171 1
            return self::$sHomeDir;
172
        }
173
174 1
        $_sHomeDir = getenv('CHAPI_HOME');
175 1
        if (!$_sHomeDir)
176
        {
177 1
            $_sHomeDir = CommandUtils::getOsHomeDir() . DIRECTORY_SEPARATOR . '.chapi';
178
        }
179
180 1
        CommandUtils::hasCreateDirectoryIfNotExists($_sHomeDir);
181
182 1
        return self::$sHomeDir = $_sHomeDir;
183
    }
184
185
    /**
186
     * @return string
187
     */
188 1
    protected function getCacheDir()
189
    {
190 1
        $_sCacheDir = $this->getHomeDir() . DIRECTORY_SEPARATOR . 'cache';
191 1
        CommandUtils::hasCreateDirectoryIfNotExists($_sCacheDir);
192
193 1
        return $_sCacheDir;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    protected function getWorkingDir()
200
    {
201
        return getcwd();
202
    }
203
204
    /**
205
     * @return string
206
     */
207 4
    protected function getProfileName()
208
    {
209 4
        return $this->oInput->getOption('profile');
210
    }
211
212
    /**
213
     * @return ContainerBuilder
214
     */
215
    private function loadContainer()
216
    {
217
        $_oContainer = new ContainerBuilder();
218
        $_oChapiConfig = new ChapiConfig(
219
            [$this->getHomeDir(), $this->getWorkingDir()],
220
            new Parser(),
221
            $this->getProfileName()
222
        );
223
224
        $_oChapiConfigLoader  = new ChapiConfigLoader($_oContainer, $_oChapiConfig);
225
        $_oChapiConfigLoader->loadProfileParameters();
226
227
        // load basic parameters
228
        $_oContainer->setParameter('chapi_home', $this->getHomeDir());
229
        $_oContainer->setParameter('chapi_work_dir', $this->getWorkingDir());
230
        $_oContainer->setParameter('chapi_profile', $this->getProfileName());
231
232
        return $_oContainer;
233
    }
234
}
235