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.

AbstractCommand::getWorkingDir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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