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

SetUpGlobalConfigurationCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 41.94%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 63
ccs 13
cts 31
cp 0.4194
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setDefaultGlobalConfiguration() 0 5 1
A configure() 0 11 1
A execute() 0 12 1
A resolveGlobalConfiguration() 0 14 3
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