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
Pull Request — master (#68)
by Marc
08:42
created

AbstractCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2.0078
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 Symfony\Component\Config\FileLocator;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21
22
abstract class AbstractCommand extends Command
23
{
24
    const FOLDER_RESOURCES = '/../../app/Resources/config/';
25
26
    /**
27
     * @var InputInterface
28
     */
29
    protected $oInput;
30
31
    /**
32
     * @var OutputInterface
33
     */
34
    protected $oOutput;
35
36
    /**
37
     * @var ContainerBuilder
38
     */
39
    private static $oContainer;
40
41
    /**
42
     * @var string
43
     */
44
    private static $sHomeDir = '';
45
46
    /**
47
     * Executes the current command.
48
     *
49
     * This method is not abstract because you can use this class
50
     * as a concrete class. In this case, instead of defining the
51
     * execute() method, you set the code to execute by passing
52
     * a Closure to the setCode() method.
53
     *
54
     * @param InputInterface $oInput An InputInterface instance
55
     * @param OutputInterface $oOutput An OutputInterface instance
56
     *
57
     * @return integer null or 0 if everything went fine, or an error code
58
     *
59
     * @throws \LogicException When this abstract method is not implemented
60
     *
61
     * @see setCode()
62
     */
63 14
    protected function execute(InputInterface $oInput, OutputInterface $oOutput)
64
    {
65 14
        $this->oInput = $oInput;
66 14
        $this->oOutput = $oOutput;
67
68 14
        if (!$this->isAppRunable())
69
        {
70
            return 1;
71
        }
72
73
        // set output for verbosity handling
74
        /** @var \Symfony\Bridge\Monolog\Handler\ConsoleHandler $_oConsoleHandler */
75 14
        $_oConsoleHandler = $this->getContainer()->get('ConsoleHandler');
76 14
        $_oConsoleHandler->setOutput($this->oOutput);
77
78 14
        return $this->process();
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    abstract protected function process();
85
86
    /**
87
     * @return ContainerBuilder
88
     */
89
    protected function getContainer()
90
    {
91
        if (is_null(self::$oContainer))
92
        {
93
            $_oContainer = new ContainerBuilder();
94
95
            // load local parameters
96
            $this->loadParameterConfig($this->getHomeDir(), 'parameters.yml', $_oContainer);
97
98
            // load optional parameter in the current working directory
99
            $this->loadParameterConfig($this->getWorkingDir(), '.chapiconfig', $_oContainer);
100
101
            // load services
102
            $_oLoader = new YamlFileLoader($_oContainer, new FileLocator(__DIR__ . self::FOLDER_RESOURCES));
103
            $_oLoader->load('services.yml');
104
105
            self::$oContainer = $_oContainer;
106
        }
107
108
        return self::$oContainer;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 3
    protected function isAppRunable()
115
    {
116
        if (
117 3
            !file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . 'parameters.yml')
118 3
            && !file_exists($this->getWorkingDir() . DIRECTORY_SEPARATOR . '.chapiconfig')
119
        ) // one file have to exist
120
        {
121
            $this->oOutput->writeln(sprintf(
122
                '<error>%s</error>',
123
                'No parameter file found. Please run "configure" command for initial setup or add a local `.chapiconfig` to your working directory.'
124
            ));
125
            return false;
126
        }
127
128 3
        return true;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 1
    protected function getHomeDir()
135
    {
136 1
        if (!empty(self::$sHomeDir))
137
        {
138 1
            return self::$sHomeDir;
139
        }
140
141 1
        $_sHomeDir = getenv('CHAPI_HOME');
142 1
        if (!$_sHomeDir)
143
        {
144 1
            $_sHomeDir = CommandUtils::getOsHomeDir() . DIRECTORY_SEPARATOR . '.chapi';
145
        }
146
147 1
        CommandUtils::hasCreateDirectoryIfNotExists($_sHomeDir);
148
149 1
        return self::$sHomeDir = $_sHomeDir;
150
    }
151
152
    /**
153
     * @return string
154
     */
155 1
    protected function getCacheDir()
156
    {
157 1
        $_sCacheDir = $this->getHomeDir() . DIRECTORY_SEPARATOR . 'cache';
158 1
        CommandUtils::hasCreateDirectoryIfNotExists($_sCacheDir);
159
160 1
        return $_sCacheDir;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    protected function getWorkingDir()
167
    {
168
        return getcwd();
169
    }
170
171
    /**
172
     * @param string $sPath
173
     * @param string $sFile
174
     * @param ContainerBuilder $oContainer
175
     */
176
    private function loadParameterConfig($sPath, $sFile, $oContainer)
177
    {
178
        // load local parameters
179
        if (file_exists($sPath . DIRECTORY_SEPARATOR . $sFile))
180
        {
181
            $_oLoader = new YamlFileLoader($oContainer, new FileLocator($sPath));
182
            $_oLoader->load($sFile);
183
        }
184
    }
185
}