Completed
Push — master ( 9165ca...fdd023 )
by Marius
02:10
created

resolveGlobalConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
declare(strict_types=1);
3
4
namespace Paysera\PhpStormHelper\Command;
5
6
use Paysera\PhpStormHelper\Entity\GlobalConfiguration;
7
use Paysera\PhpStormHelper\Entity\SuggestedGlobalConfiguration;
8
use Paysera\PhpStormHelper\Service\ExternalToolsConfigurationHelper;
9
use Paysera\PhpStormHelper\Service\PluginDownloader;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use RuntimeException;
15
16
class SetUpGlobalConfigurationCommand extends Command
17
{
18
    private $externalToolsConfigurationHelper;
19
    private $pluginDownloader;
20
    private $defaultGlobalConfiguration;
21
22 8
    public function __construct(
23
        ExternalToolsConfigurationHelper $externalToolsConfigurationHelper,
24
        PluginDownloader $pluginDownloader
25
    ) {
26 8
        parent::__construct();
27
28 8
        $this->externalToolsConfigurationHelper = $externalToolsConfigurationHelper;
29 8
        $this->pluginDownloader = $pluginDownloader;
30 8
        $this->defaultGlobalConfiguration = new SuggestedGlobalConfiguration();
31 8
    }
32
33
    public function setDefaultGlobalConfiguration(GlobalConfiguration $defaultGlobalConfiguration): self
34
    {
35
        $this->defaultGlobalConfiguration = $defaultGlobalConfiguration;
36
        return $this;
37
    }
38
39 8
    protected function configure()
40
    {
41
        $this
42 8
            ->setName('set-up-global-configuration')
43 8
            ->addArgument(
44 8
                'path-to-config-file',
45 8
                InputArgument::OPTIONAL,
46 8
                'Path to php configuration file, which returns GlobalConfiguration object'
47
            )
48
        ;
49 8
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $globalConfiguration = $this->resolveGlobalConfiguration($input);
54
55
        $this->externalToolsConfigurationHelper->configureExternalTools(
56
            $globalConfiguration->getExternalToolConfigurations()
57
        );
58
59
        $this->pluginDownloader->downloadPlugins($globalConfiguration->getPlugins());
60
61
        $output->writeln('Restart all PhpStorm instances for changes to take effect');
62
    }
63
64
    protected function resolveGlobalConfiguration(InputInterface $input): GlobalConfiguration
65
    {
66
        $configurationFilePath = $input->getArgument('path-to-config-file');
67
        if ($configurationFilePath === null) {
68
            return $this->defaultGlobalConfiguration;
69
        }
70
71
        $globalConfiguration = require $configurationFilePath;
72
        if (!$globalConfiguration instanceof GlobalConfiguration) {
73
            throw new RuntimeException('Expected configuration file to return GlobalConfiguration object');
74
        }
75
76
        return $globalConfiguration;
77
    }
78
}
79