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... (7ea4db)
by Marc
03:05
created

AbstractCommand::getContainer()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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