Completed
Push — develop ( c60de3...4fc294 )
by Susi
05:38
created

BaseCommand::initialize()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 18
nc 8
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace T3G\Elasticorn\Commands;
5
6
use Psr\Log\LogLevel;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
use T3G\Elasticorn\Service\ConfigurationService;
13
use T3G\Elasticorn\Service\IndexService;
14
15
class BaseCommand extends Command
16
{
17
18
    /**
19
     * @var IndexService
20
     */
21
    protected $indexService;
22
23
    /**
24
     * @var ConfigurationService
25
     */
26
    protected $configurationService;
27
28
    /**
29
     * @var \T3G\Elasticorn\Service\DocumentTypeService
30
     */
31
    protected $documentTypeService;
32
33
    protected function initialize(InputInterface $input, OutputInterface $output)
34
    {
35
        global $container;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
36
        $verbosityLevelMap = [
37
            LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
38
            LogLevel::INFO   => OutputInterface::VERBOSITY_NORMAL,
39
        ];
40
        $container->setParameter('logger.output', $output);
41
        $container->setParameter('logger.verbosityMap', $verbosityLevelMap);
42
        if ($input->hasOption('config-path') && !empty($input->getOption('config-path'))) {
43
            putenv('configurationPath=' . $input->getOption('config-path'));
44
        }
45
        if ($input->hasArgument('indexName')) {
46
            $container->setParameter('index.name', $input->getArgument('indexName'));
47
        } else {
48
            $container->setParameter('index.name', null);
49
        }
50
        if ($input->hasArgument('documentType')) {
51
            $container->setParameter('type.name', $input->getArgument('documentType'));
52
            $this->documentTypeService = $container->get('documentTypeService');
53
        }
54
        $this->indexService = $container->get('indexService');
55
        $this->configurationService = $container->get('configurationService');
56
    }
57
58
    protected function configure()
59
    {
60
        $this->addOption(
61
            'config-path',
62
            'c',
63
            InputArgument::OPTIONAL,
64
            'The full path to the configuration directory (may be relative)'
65
        );
66
    }
67
68
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70
        while (!(getenv('configurationPath') && file_exists(getenv('configurationPath')))) {
71
            $this->askForConfigDir($input, $output);
72
        }
73
        $path = rtrim(getenv('configurationPath'), DIRECTORY_SEPARATOR) . '/';
74
        putenv('configurationPath=' . $path);
75
    }
76
77
    private function askForConfigDir(InputInterface $input, OutputInterface $output)
78
    {
79
        $helper = $this->getHelper('question');
80
        $question = new Question('Please enter a valid path to your configuration directory:' . "\n");
81
82
        $answer = $helper->ask($input, $output, $question);
83
        putenv('configurationPath=' . $answer);
84
    }
85
}