Completed
Push — master ( a7b3c5...a3697f )
by Christian
04:52 queued 11s
created

ModifyCloudCommand::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 3
crap 12
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\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Xabbuh\PandaClient\Api\AccountManagerInterface;
23
use Xabbuh\PandaClient\Api\Cloud;
24
use Xabbuh\PandaClient\Api\HttplugClient;
25
use Xabbuh\PandaClient\Transformer\TransformerRegistry;
26
27 1
if (class_exists(ContainerAwareCommand::class)) {
28
    /**
29
     * Modify a panda cloud via command-line.
30
     *
31
     * @author Christian Flothmann <[email protected]>
32
     *
33
     * @final
34
     */
35
    class ModifyCloudCommand extends ContainerAwareCommand
36
    {
37
        use ModifyCloudCommandTrait;
38
39
        protected static $defaultName = 'panda:cloud:modify';
40
41
        private $accountManager;
42
        private $transformerRegistry;
43
        private $httpClient;
44
45
        public function __construct(AccountManagerInterface $accountManager = null, TransformerRegistry $transformerRegistry = null, HttpClient $httpClient = null)
46
        {
47
            if (null === $accountManager) {
48
                @trigger_error(sprintf('Not injecting an %s instance into the constructor of the %s class is deprecated since PandaBundle 1.5.', AccountManagerInterface::class, static::class), E_USER_DEPRECATED);
49
            }
50
51
            if (null === $transformerRegistry) {
52
                @trigger_error(sprintf('Not injecting an %s instance into the constructor of the %s class is deprecated since PandaBundle 1.5.', TransformerRegistry::class, static::class), E_USER_DEPRECATED);
53
            }
54
55
            parent::__construct();
56
57
            $this->accountManager = $accountManager;
58
            $this->transformerRegistry = $transformerRegistry;
59
            $this->httpClient = $httpClient;
60
        }
61
62
        public function setContainer(ContainerInterface $container = null)
63
        {
64
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
65
66
            parent::setContainer($container);
67
        }
68
69
        public function getContainer()
70
        {
71
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
72
73
            return parent::getContainer();
74
        }
75
    }
76
} else {
77
    /**
78
     * Modify a panda cloud via command-line.
79
     *
80
     * @author Christian Flothmann <[email protected]>
81
     *
82
     * @final
83
     */
84
    class ModifyCloudCommand extends Command
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Xabbuh\PandaBundle\Command\ModifyCloudCommand has been defined more than once; this definition is ignored, only the first definition in this file (L35-75) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
85
    {
86
        use ModifyCloudCommandTrait;
87
88
        protected static $defaultName = 'panda:cloud:modify';
89
90
        private $accountManager;
91
        private $transformerRegistry;
92
        private $httpClient;
93
        private $container;
94
95 6
        public function __construct(AccountManagerInterface $accountManager = null, TransformerRegistry $transformerRegistry = null, HttpClient $httpClient = null)
96
        {
97 6
            if (null === $accountManager) {
98
                @trigger_error(sprintf('Not injecting an %s instance into the constructor of the %s class is deprecated since PandaBundle 1.5.', AccountManagerInterface::class, static::class), E_USER_DEPRECATED);
99
            }
100
101 6
            if (null === $transformerRegistry) {
102
                @trigger_error(sprintf('Not injecting an %s instance into the constructor of the %s class is deprecated since PandaBundle 1.5.', TransformerRegistry::class, static::class), E_USER_DEPRECATED);
103
            }
104
105 6
            parent::__construct();
106
107 6
            $this->accountManager = $accountManager;
108 6
            $this->transformerRegistry = $transformerRegistry;
109 6
            $this->httpClient = $httpClient;
110 6
        }
111
112
        public function setContainer(ContainerInterface $container = null)
113
        {
114
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
115
116
            $this->container = $container;
117
        }
118
119
        public function getContainer()
120
        {
121
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
122
123
            return $this->container;
124
        }
125
    }
126
}
127
128
/**
129
 * @internal
130
 */
131
trait ModifyCloudCommandTrait
132
{
133
    /**
134
     * {@inheritDoc}
135
     */
136 6
    protected function configure()
137
    {
138 6
        $this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically
0 ignored issues
show
Bug introduced by
It seems like setName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
139 6
        $this->setDescription('Modify a cloud');
0 ignored issues
show
Bug introduced by
It seems like setDescription() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
140 6
        $this->addOption(
0 ignored issues
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
141 6
            'account',
142 6
            'a',
143 6
            InputOption::VALUE_REQUIRED,
144 6
            'The account to use to authenticate to the panda service'
145
        );
146 6
        $this->addOption('name', null, InputOption::VALUE_REQUIRED, 'The new cloud name');
0 ignored issues
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
147 6
        $this->addOption('s3-bucket', null, InputOption::VALUE_REQUIRED, 'The new AWS S3 bucket');
0 ignored issues
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
148 6
        $this->addOption('access-key', null, InputOption::VALUE_REQUIRED, 'The new access key');
0 ignored issues
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
149 6
        $this->addOption('secret-key', null, InputOption::VALUE_REQUIRED, 'The new secret key');
0 ignored issues
show
Bug introduced by
It seems like addOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
150 6
        $this->addArgument('cloud-id', InputArgument::REQUIRED, 'The id of the cloud being modified');
0 ignored issues
show
Bug introduced by
It seems like addArgument() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
151 6
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156 4
    protected function execute(InputInterface $input, OutputInterface $output)
157
    {
158 4
        $data = array();
159
160
        // change the cloud name
161 4
        if ($name = $input->getOption('name')) {
162 1
            $data['name'] = $name;
163
        }
164
165
        // change the s3 bucket
166 4
        if ($s3bucket = $input->getOption('s3-bucket')) {
167 1
            $data['s3_videos_bucket'] = $s3bucket;
168
        }
169
170
        // change the access key
171 4
        if ($accessKey = $input->getOption('access-key')) {
172 1
            $data['aws_access_key'] = $accessKey;
173
        }
174
175
        // change the secret key
176 4
        if ($secretKey = $input->getOption('secret-key')) {
177 1
            $data['aws_secret_key'] = $secretKey;
178
        }
179
180
        // if anything has changed
181 4
        if (count($data) > 0) {
182 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...ommandTrait::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...
183 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...
184
        }
185
186 4
        return 0;
187
    }
188
189 3
    private function getCloud(string $cloudId, ?string $accountKey): Cloud
190
    {
191 3
        if (null === $this->accountManager) {
192
            $this->accountManager = $this->getContainer()->get('xabbuh_panda.account_manager');
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
193
        }
194
195 3
        if (null === $this->transformerRegistry) {
196
            $this->transformerRegistry = $this->getContainer()->get('xabbuh_panda.transformer');
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
197
        }
198
199 3
        $account = $this->accountManager->getAccount($accountKey);
200
201 3
        $httpClient = new HttplugClient($this->httpClient);
202 3
        $httpClient->setCloudId($cloudId);
203 3
        $httpClient->setAccount($account);
204
205 3
        $cloud = new Cloud();
206 3
        $cloud->setHttpClient($httpClient);
207 3
        $cloud->setTransformers($this->transformerRegistry);
208
209 3
        return $cloud;
210
    }
211
}
212