ModifyCloudCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 95.74%

Importance

Changes 0
Metric Value
wmc 11
cbo 4
dl 0
loc 96
ccs 45
cts 47
cp 0.9574
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 0 16 1
B execute() 0 32 6
A getCloud() 0 22 3
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaBundle package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaBundle\Command;
13
14
use Http\Client\HttpClient;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Xabbuh\PandaClient\Api\AccountManagerInterface;
21
use Xabbuh\PandaClient\Api\Cloud;
22
use Xabbuh\PandaClient\Api\HttplugClient;
23
use Xabbuh\PandaClient\Transformer\TransformerRegistry;
24
25
/**
26
 * Modify a panda cloud via command-line.
27
 *
28
 * @author Christian Flothmann <[email protected]>
29
 */
30
final class ModifyCloudCommand extends Command
31
{
32
    protected static $defaultName = 'panda:cloud:modify';
33
34
    private $accountManager;
35
    private $transformerRegistry;
36
    private $httpClient;
37
38 6
    public function __construct(AccountManagerInterface $accountManager, TransformerRegistry $transformerRegistry, HttpClient $httpClient = null)
39
    {
40 6
        parent::__construct();
41
42 6
        $this->accountManager = $accountManager;
43 6
        $this->transformerRegistry = $transformerRegistry;
44 6
        $this->httpClient = $httpClient;
45 6
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 6
    protected function configure()
51
    {
52 6
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
53 6
        $this->setDescription('Modify a cloud');
54 6
        $this->addOption(
55 6
            'account',
56 6
            'a',
57 6
            InputOption::VALUE_REQUIRED,
58 6
            'The account to use to authenticate to the panda service'
59
        );
60 6
        $this->addOption('name', null, InputOption::VALUE_REQUIRED, 'The new cloud name');
61 6
        $this->addOption('s3-bucket', null, InputOption::VALUE_REQUIRED, 'The new AWS S3 bucket');
62 6
        $this->addOption('access-key', null, InputOption::VALUE_REQUIRED, 'The new access key');
63 6
        $this->addOption('secret-key', null, InputOption::VALUE_REQUIRED, 'The new secret key');
64 6
        $this->addArgument('cloud-id', InputArgument::REQUIRED, 'The id of the cloud being modified');
65 6
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 4
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72 4
        $data = array();
73
74
        // change the cloud name
75 4
        if ($name = $input->getOption('name')) {
76 1
            $data['name'] = $name;
77
        }
78
79
        // change the s3 bucket
80 4
        if ($s3bucket = $input->getOption('s3-bucket')) {
81 1
            $data['s3_videos_bucket'] = $s3bucket;
82
        }
83
84
        // change the access key
85 4
        if ($accessKey = $input->getOption('access-key')) {
86 1
            $data['aws_access_key'] = $accessKey;
87
        }
88
89
        // change the secret key
90 4
        if ($secretKey = $input->getOption('secret-key')) {
91 1
            $data['aws_secret_key'] = $secretKey;
92
        }
93
94
        // if anything has changed
95 4
        if (count($data) > 0) {
96 3
            $cloud = $this->getCloud($input->getArgument('cloud-id'), $input->getOption('account'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('cloud-id') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Xabbuh\PandaBundle\Comma...loudCommand::getCloud() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97 3
            $cloud->setCloud($data, $input->getArgument('cloud-id'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('cloud-id') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string>; however, Xabbuh\PandaClient\Api\Cloud::setCloud() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
98
        }
99
100 4
        return 0;
101
    }
102
103 3
    private function getCloud(string $cloudId, ?string $accountKey): Cloud
104
    {
105 3
        if (null === $this->accountManager) {
106
            $this->accountManager = $this->getContainer()->get('xabbuh_panda.account_manager');
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Xabbuh\PandaBundl...and\ModifyCloudCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        }
108
109 3
        if (null === $this->transformerRegistry) {
110
            $this->transformerRegistry = $this->getContainer()->get('xabbuh_panda.transformer');
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Xabbuh\PandaBundl...and\ModifyCloudCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        }
112
113 3
        $account = $this->accountManager->getAccount($accountKey);
114
115 3
        $httpClient = new HttplugClient($this->httpClient);
116 3
        $httpClient->setCloudId($cloudId);
117 3
        $httpClient->setAccount($account);
118
119 3
        $cloud = new Cloud();
120 3
        $cloud->setHttpClient($httpClient);
121 3
        $cloud->setTransformers($this->transformerRegistry);
122
123 3
        return $cloud;
124
    }
125
}
126