Completed
Push — master ( 5c84c5...c7f8f4 )
by Marius
01:51
created

ConfigureInstallationCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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