Completed
Pull Request — 1.x (#56)
by Christian
49:17 queued 48:10
created

ModifyCloudCommandTrait::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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 Symfony\Component\DependencyInjection\ContainerInterface;
21
use Xabbuh\PandaClient\Api\AccountManagerInterface;
22
use Xabbuh\PandaClient\Api\Cloud;
23
use Xabbuh\PandaClient\Api\HttplugClient;
24
use Xabbuh\PandaClient\Transformer\TransformerRegistry;
25
26 1
if (class_exists(ContainerAwareCommand::class)) {
27
    /**
28
     * Modify a panda cloud via command-line.
29
     *
30
     * @author Christian Flothmann <[email protected]>
31
     *
32
     * @final since 1.5
33
     */
34
    class ModifyCloudCommand extends ContainerAwareCommand
35
    {
36
        use ModifyCloudCommandTrait;
37
38
        protected static $defaultName = 'panda:cloud:modify';
39
40
        private $accountManager;
41
        private $transformerRegistry;
42
43
        public function __construct(AccountManagerInterface $accountManager = null, TransformerRegistry $transformerRegistry = null)
44
        {
45
            if (null === $accountManager) {
46
                @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);
47
            }
48
49
            if (null === $transformerRegistry) {
50
                @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);
51
            }
52
53
            parent::__construct();
54
55
            $this->accountManager = $accountManager;
56
            $this->transformerRegistry = $transformerRegistry;
57
        }
58
59
        public function setContainer(ContainerInterface $container = null)
60
        {
61
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
62
63
            parent::setContainer($container);
64
        }
65
66
        public function getContainer()
67
        {
68
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
69
70
            return parent::getContainer();
71
        }
72
    }
73
} else {
74
    /**
75
     * Modify a panda cloud via command-line.
76
     *
77
     * @author Christian Flothmann <[email protected]>
78
     *
79
     * @final since 1.5
80
     */
81
    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 (L34-72) 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...
82
    {
83
        use ModifyCloudCommandTrait;
84
85
        protected static $defaultName = 'panda:cloud:modify';
86
87
        private $accountManager;
88
        private $transformerRegistry;
89
        private $container;
90
91 1
        public function __construct(AccountManagerInterface $accountManager = null, TransformerRegistry $transformerRegistry = null)
92
        {
93 1
            if (null === $accountManager) {
94
                @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);
95
            }
96
97 1
            if (null === $transformerRegistry) {
98
                @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);
99
            }
100
101 1
            parent::__construct();
102
103 1
            $this->accountManager = $accountManager;
104 1
            $this->transformerRegistry = $transformerRegistry;
105 1
        }
106
107
        public function setContainer(ContainerInterface $container = null)
108
        {
109
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
110
111
            $this->container = $container;
112
        }
113
114
        public function getContainer()
115
        {
116
            @trigger_error(sprintf('The %s() method is deprecated since PandaBundle 1.5.', __METHOD__), E_USER_DEPRECATED);
117
118
            return $this->container;
119
        }
120
    }
121
}
122
123
trait ModifyCloudCommandTrait
124
{
125
    /**
126
     * {@inheritDoc}
127
     */
128 1
    protected function configure()
129
    {
130 1
        $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...
131 1
        $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...
132 1
        $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...
133 1
            'account',
134 1
            'a',
135 1
            InputOption::VALUE_REQUIRED,
136 1
            'The account to use to authenticate to the panda service'
137
        );
138 1
        $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...
139 1
        $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...
140 1
        $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...
141 1
        $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...
142 1
        $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...
143 1
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    protected function execute(InputInterface $input, OutputInterface $output)
149
    {
150
        $data = array();
151
152
        // change the cloud name
153
        if ($name = $input->getOption('name')) {
154
            $data['name'] = $name;
155
        }
156
157
        // change the s3 bucket
158
        if ($s3bucket = $input->getOption('s3-bucket')) {
159
            $data['s3_videos_bucket'] = $s3bucket;
160
        }
161
162
        // change the access key
163
        if ($accessKey = $input->getOption('access-key')) {
164
            $data['aws_access_key'] = $accessKey;
165
        }
166
167
        // change the secret key
168
        if ($secretKey = $input->getOption('secret-key')) {
169
            $data['aws_secret_key'] = $secretKey;
170
        }
171
172
        // if anything has changed
173
        if (count($data) > 0) {
174
            $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...
175
            $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...
176
        }
177
178
        return 0;
179
    }
180
181
    private function getCloud(string $cloudId, ?string $accountKey): Cloud
182
    {
183
        if (null === $this->accountManager) {
184
            $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...
185
        }
186
187
        if (null === $this->transformerRegistry) {
188
            $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...
189
        }
190
191
        $account = $this->accountManager->getAccount($accountKey);
192
193
        $httpClient = new HttplugClient();
194
        $httpClient->setCloudId($cloudId);
195
        $httpClient->setAccount($account);
196
197
        $cloud = new Cloud();
198
        $cloud->setHttpClient($httpClient);
199
        $cloud->setTransformers($this->transformerRegistry);
200
201
        return $cloud;
202
    }
203
}
204