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 since 1.5 |
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 since 1.5 |
83
|
|
|
*/ |
84
|
|
|
class ModifyCloudCommand extends Command |
|
|
|
|
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
|
5 |
|
public function __construct(AccountManagerInterface $accountManager = null, TransformerRegistry $transformerRegistry = null, HttpClient $httpClient = null) |
96
|
|
|
{ |
97
|
5 |
|
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
|
5 |
|
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
|
5 |
|
parent::__construct(); |
106
|
|
|
|
107
|
5 |
|
$this->accountManager = $accountManager; |
108
|
5 |
|
$this->transformerRegistry = $transformerRegistry; |
109
|
5 |
|
$this->httpClient = $httpClient; |
110
|
5 |
|
} |
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
|
|
|
trait ModifyCloudCommandTrait |
129
|
|
|
{ |
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
5 |
|
protected function configure() |
134
|
|
|
{ |
135
|
5 |
|
$this->setName(static::$defaultName); // BC with Symfony Console 3.3 and older not handling the property automatically |
|
|
|
|
136
|
5 |
|
$this->setDescription('Modify a cloud'); |
|
|
|
|
137
|
5 |
|
$this->addOption( |
|
|
|
|
138
|
5 |
|
'account', |
139
|
5 |
|
'a', |
140
|
5 |
|
InputOption::VALUE_REQUIRED, |
141
|
5 |
|
'The account to use to authenticate to the panda service' |
142
|
|
|
); |
143
|
5 |
|
$this->addOption('name', null, InputOption::VALUE_REQUIRED, 'The new cloud name'); |
|
|
|
|
144
|
5 |
|
$this->addOption('s3-bucket', null, InputOption::VALUE_REQUIRED, 'The new AWS S3 bucket'); |
|
|
|
|
145
|
5 |
|
$this->addOption('access-key', null, InputOption::VALUE_REQUIRED, 'The new access key'); |
|
|
|
|
146
|
5 |
|
$this->addOption('secret-key', null, InputOption::VALUE_REQUIRED, 'The new secret key'); |
|
|
|
|
147
|
5 |
|
$this->addArgument('cloud-id', InputArgument::REQUIRED, 'The id of the cloud being modified'); |
|
|
|
|
148
|
5 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
154
|
|
|
{ |
155
|
4 |
|
$data = array(); |
156
|
|
|
|
157
|
|
|
// change the cloud name |
158
|
4 |
|
if ($name = $input->getOption('name')) { |
159
|
1 |
|
$data['name'] = $name; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// change the s3 bucket |
163
|
4 |
|
if ($s3bucket = $input->getOption('s3-bucket')) { |
164
|
1 |
|
$data['s3_videos_bucket'] = $s3bucket; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// change the access key |
168
|
4 |
|
if ($accessKey = $input->getOption('access-key')) { |
169
|
1 |
|
$data['aws_access_key'] = $accessKey; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// change the secret key |
173
|
4 |
|
if ($secretKey = $input->getOption('secret-key')) { |
174
|
1 |
|
$data['aws_secret_key'] = $secretKey; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// if anything has changed |
178
|
4 |
|
if (count($data) > 0) { |
179
|
3 |
|
$cloud = $this->getCloud($input->getArgument('cloud-id'), $input->getOption('account')); |
|
|
|
|
180
|
3 |
|
$cloud->setCloud($data, $input->getArgument('cloud-id')); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
return 0; |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
private function getCloud(string $cloudId, ?string $accountKey): Cloud |
187
|
|
|
{ |
188
|
3 |
|
if (null === $this->accountManager) { |
189
|
|
|
$this->accountManager = $this->getContainer()->get('xabbuh_panda.account_manager'); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
3 |
|
if (null === $this->transformerRegistry) { |
193
|
|
|
$this->transformerRegistry = $this->getContainer()->get('xabbuh_panda.transformer'); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
3 |
|
$account = $this->accountManager->getAccount($accountKey); |
197
|
|
|
|
198
|
3 |
|
$httpClient = new HttplugClient($this->httpClient); |
199
|
3 |
|
$httpClient->setCloudId($cloudId); |
200
|
3 |
|
$httpClient->setAccount($account); |
201
|
|
|
|
202
|
3 |
|
$cloud = new Cloud(); |
203
|
3 |
|
$cloud->setHttpClient($httpClient); |
204
|
3 |
|
$cloud->setTransformers($this->transformerRegistry); |
205
|
|
|
|
206
|
3 |
|
return $cloud; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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.