Completed
Push — master ( d82965...ddb300 )
by Christian
13s queued 11s
created

ModifyCloudCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
31
 */
32
class ModifyCloudCommand extends Command
33
{
34
    protected static $defaultName = 'panda:cloud:modify';
35
36
    private $accountManager;
37
    private $transformerRegistry;
38
    private $httpClient;
39
40 6
    public function __construct(AccountManagerInterface $accountManager, TransformerRegistry $transformerRegistry, HttpClient $httpClient = null)
41
    {
42 6
        parent::__construct();
43
44 6
        $this->accountManager = $accountManager;
45 6
        $this->transformerRegistry = $transformerRegistry;
46 6
        $this->httpClient = $httpClient;
47 6
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 6
    protected function configure()
53
    {
54 6
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
55 6
        $this->setDescription('Modify a cloud');
56 6
        $this->addOption(
57 6
            'account',
58 6
            'a',
59 6
            InputOption::VALUE_REQUIRED,
60 6
            'The account to use to authenticate to the panda service'
61
        );
62 6
        $this->addOption('name', null, InputOption::VALUE_REQUIRED, 'The new cloud name');
63 6
        $this->addOption('s3-bucket', null, InputOption::VALUE_REQUIRED, 'The new AWS S3 bucket');
64 6
        $this->addOption('access-key', null, InputOption::VALUE_REQUIRED, 'The new access key');
65 6
        $this->addOption('secret-key', null, InputOption::VALUE_REQUIRED, 'The new secret key');
66 6
        $this->addArgument('cloud-id', InputArgument::REQUIRED, 'The id of the cloud being modified');
67 6
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 4
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74 4
        $data = array();
75
76
        // change the cloud name
77 4
        if ($name = $input->getOption('name')) {
78 1
            $data['name'] = $name;
79
        }
80
81
        // change the s3 bucket
82 4
        if ($s3bucket = $input->getOption('s3-bucket')) {
83 1
            $data['s3_videos_bucket'] = $s3bucket;
84
        }
85
86
        // change the access key
87 4
        if ($accessKey = $input->getOption('access-key')) {
88 1
            $data['aws_access_key'] = $accessKey;
89
        }
90
91
        // change the secret key
92 4
        if ($secretKey = $input->getOption('secret-key')) {
93 1
            $data['aws_secret_key'] = $secretKey;
94
        }
95
96
        // if anything has changed
97 4
        if (count($data) > 0) {
98 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...
99 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...
100
        }
101
102 4
        return 0;
103
    }
104
105 3
    private function getCloud(string $cloudId, ?string $accountKey): Cloud
106
    {
107 3
        if (null === $this->accountManager) {
108
            $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...
109
        }
110
111 3
        if (null === $this->transformerRegistry) {
112
            $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...
113
        }
114
115 3
        $account = $this->accountManager->getAccount($accountKey);
116
117 3
        $httpClient = new HttplugClient($this->httpClient);
118 3
        $httpClient->setCloudId($cloudId);
119 3
        $httpClient->setAccount($account);
120
121 3
        $cloud = new Cloud();
122 3
        $cloud->setHttpClient($httpClient);
123 3
        $cloud->setTransformers($this->transformerRegistry);
124
125 3
        return $cloud;
126
    }
127
}
128