CloudApiClient   F
last analyzed

Complexity

Total Complexity 61

Size/Duplication

Total Lines 1044
Duplicated Lines 7.28 %

Coupling/Cohesion

Components 4
Dependencies 26

Test Coverage

Coverage 96.02%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 61
c 2
b 0
f 0
lcom 4
cbo 26
dl 76
loc 1044
ccs 314
cts 327
cp 0.9602
rs 0.8139

50 Methods

Rating   Name   Duplication   Size   Complexity  
B factory() 0 26 1
A getBuilderParams() 0 8 1
A sites() 0 5 1
A site() 0 6 1
A environments() 0 6 1
A environment() 9 9 1
A installDistro() 0 11 1
A installDistroByProject() 0 5 1
A installDistroByMakefile() 0 4 1
A servers() 9 9 1
A server() 0 10 1
A sshKeys() 0 6 1
A sshKey() 0 9 1
A addSshKey() 0 16 1
A deleteSshKey() 0 15 3
A svnUsers() 0 6 1
A svnUser() 0 9 1
A addSvnUser() 0 11 1
A deleteSvnUser() 0 9 1
A databases() 0 6 1
A database() 0 9 1
B addDatabase() 0 20 8
A deleteDatabase() 0 10 2
A environmentDatabases() 9 9 1
A environmentDatabase() 0 10 1
A deleteDatabaseBackup() 0 11 1
A restoreDatabaseBackup() 0 11 1
A tasks() 0 8 1
A task() 0 9 1
A domains() 9 9 1
A domain() 10 10 1
A addDomain() 10 10 1
A moveDomain() 0 18 2
A deleteDomain() 10 10 1
A purgeVarnishCache() 10 10 1
A copyDatabase() 0 11 1
A copyFiles() 0 10 1
A liveDev() 0 11 1
A enableLiveDev() 0 4 1
A disableLiveDev() 0 4 1
A deployCode() 0 10 1
A pushCode() 0 10 1
A siteDatabases() 0 4 1
A siteDatabase() 0 4 1
A codeDeploy() 0 4 1
A databaseBackups() 0 10 1
A databaseBackup() 0 11 1
A downloadDatabaseBackup() 0 14 1
A createDatabaseBackup() 0 10 1
A taskInfo() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CloudApiClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CloudApiClient, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Acquia\Cloud\Api;
4
5
use Acquia\Rest\ServiceManagerAware;
6
use Guzzle\Common\Collection;
7
use Acquia\Json\Json;
8
use Guzzle\Service\Client;
9
10
class CloudApiClient extends Client implements ServiceManagerAware
11
{
12
    const BASE_URL         = 'https://cloudapi.acquia.com';
13
    const BASE_PATH        = '/v1';
14
15
    const INSTALL_MAKEFILE = 'make_url';
16
    const INSTALL_PROJECT  = 'distro_url';
17
18
    const LIVEDEV_ENABLE   = 'enable';
19
    const LIVEDEV_DISABLE  = 'disable';
20
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @return \Acquia\Cloud\Api\CloudApiClient
25
     */
26 165
    public static function factory($config = array())
27
    {
28
        $required = array(
29 165
            'base_url',
30 165
            'username',
31 165
            'password',
32 165
        );
33
34
        $defaults = array(
35 165
            'base_url' => self::BASE_URL,
36 165
            'base_path' => self::BASE_PATH,
37 165
        );
38
39
        // Instantiate the Acquia Search plugin.
40 165
        $config = Collection::fromConfig($config, $defaults, $required);
41 159
        $client = new static($config->get('base_url'), $config);
42 159
        $client->setDefaultHeaders(array(
0 ignored issues
show
Deprecated Code introduced by
The method Guzzle\Http\Client::setDefaultHeaders() has been deprecated.

This method has been deprecated.

Loading history...
43 159
            'Content-Type' => 'application/json; charset=utf-8',
44 159
        ));
45
46
        // Attach the Acquia Search plugin to the client.
47 159
        $plugin = new CloudApiAuthPlugin($config->get('username'), $config->get('password'));
48 159
        $client->addSubscriber($plugin);
49
50 159
        return $client;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function getBuilderParams()
57
    {
58
        return array(
59 3
            'base_url' => $this->getConfig('base_url'),
0 ignored issues
show
Documentation introduced by
'base_url' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60 3
            'username' => $this->getConfig('username'),
0 ignored issues
show
Documentation introduced by
'username' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61 3
            'password' => $this->getConfig('password'),
0 ignored issues
show
Documentation introduced by
'password' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62 3
        );
63
    }
64
65
    /**
66
     * @return \Acquia\Cloud\Api\Response\SiteNames
67
     *
68
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
69
     *
70
     * @see http://cloudapi.acquia.com/#GET___sites_-instance_route
71
     */
72 6
    public function sites()
73
    {
74 6
        $request = $this->get('{+base_path}/sites.json');
75 6
        return new Response\SiteNames($request);
76
    }
77
78
    /**
79
     * @param string $site
80
     *
81
     * @return \Acquia\Cloud\Api\Response\Site
82
     *
83
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
84
     *
85
     * @see http://cloudapi.acquia.com/#GET__sites__site-instance_route
86
     */
87 3
    public function site($site)
88
    {
89 3
        $variables = array('site' => $site);
90 3
        $request = $this->get(array('{+base_path}/sites/{site}.json', $variables));
91 3
        return new Response\Site($request);
92
    }
93
94
    /**
95
     * @param string $site
96
     *
97
     * @return \Acquia\Cloud\Api\Response\Environments
98
     *
99
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
100
     *
101
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs-instance_route
102
     */
103 3
    public function environments($site)
104
    {
105 3
        $variables = array('site' => $site);
106 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs.json', $variables));
107 3
        return new Response\Environments($request);
108
    }
109
110
    /**
111
     * @param string $site
112
     * @param string $env
113
     *
114
     * @return \Acquia\Cloud\Api\Response\Environment
115
     *
116
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
117
     *
118
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env-instance_route
119
     */
120 3 View Code Duplication
    public function environment($site, $env)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $variables = array(
123 3
            'site' => $site,
124 3
            'env' => $env,
125 3
        );
126 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}.json', $variables));
127 3
        return new Response\Environment($request);
128
    }
129
130
    /**
131
     * @param string $site
132
     * @param string $env
133
     * @param string $type
134
     * @param string $source
135
     *
136
     * @return \Acquia\Cloud\Api\Response\Task
137
     *
138
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
139
     *
140
     * @see http://cloudapi.acquia.com/#POST__sites__site_envs__env_install__type-instance_route
141
     */
142
    public function installDistro($site, $env, $type, $source)
143
    {
144
        $variables = array(
145
            'site' => $site,
146
            'env' => $env,
147
            'type' => $type,
148
            'source' => $source,
149
        );
150
        $request = $this->post(array('{+base_path}/sites/{site}/envs/{env}/install/{type}.json?source={source}', $variables));
151
        return new Response\Task($request);
152
    }
153
154
    /**
155
     * Install any publicly accessible, standard Drupal distribution.
156
     *
157
     * @param string $site
158
     * @param string $env
159
     * @param string $projectName
160
     * @param string $version
161
     *
162
     * @return \Acquia\Cloud\Api\Response\Task
163
     *
164
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
165
     */
166
    public function installDistroByProject($site, $env, $projectName, $version)
167
    {
168
        $source = 'http://ftp.drupal.org/files/projects/' . $projectName . '-' . $version . '-core.tar.gz';
169
        return $this->installDistro($site, $env, self::INSTALL_PROJECT, $source);
170
    }
171
172
    /**
173
     * Install a distro by passing a URL to a Drush makefile.
174
     *
175
     * @param string $site
176
     * @param string $env
177
     * @param string $makefileUrl
178
     *
179
     * @return \Acquia\Cloud\Api\Response\Task
180
     *
181
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
182
     */
183
    public function installDistroByMakefile($site, $env, $makefileUrl)
184
    {
185
        return $this->installDistro($site, $env, self::INSTALL_MAKEFILE, $makefileUrl);
186
    }
187
188
    /**
189
     * @param string $site
190
     * @param string $env
191
     *
192
     * @return \Acquia\Cloud\Api\Response\Servers
193
     *
194
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
195
     *
196
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_servers-instance_route
197
     */
198 3 View Code Duplication
    public function servers($site, $env)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    {
200
        $variables = array(
201 3
            'site' => $site,
202 3
            'env' => $env,
203 3
        );
204 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/servers.json', $variables));
205 3
        return new Response\Servers($request);
206
    }
207
208
    /**
209
     * @param string $site
210
     * @param string $env
211
     * @param string $server
212
     *
213
     * @return \Acquia\Cloud\Api\Response\Server
214
     *
215
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
216
     *
217
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_servers__server-instance_route
218
     */
219 3
    public function server($site, $env, $server)
220
    {
221
        $variables = array(
222 3
            'site' => $site,
223 3
            'env' => $env,
224 3
            'server' => $server,
225 3
        );
226 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/servers/{server}.json', $variables));
227 3
        return new Response\Server($request);
228
    }
229
230
    /**
231
     * @param string $site
232
     *
233
     * @return \Acquia\Cloud\Api\Response\SshKeys
234
     *
235
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
236
     *
237
     * @see http://cloudapi.acquia.com/#GET__sites__site_sshkeys-instance_route
238
     */
239 3
    public function sshKeys($site)
240
    {
241 3
        $variables = array('site' => $site);
242 3
        $request = $this->get(array('{+base_path}/sites/{site}/sshkeys.json', $variables));
243 3
        return new Response\SshKeys($request);
244
    }
245
246
    /**
247
     * @param string $site
248
     * @param int $keyId
249
     *
250
     * @return \Acquia\Cloud\Api\Response\SshKey
251
     *
252
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
253
     *
254
     * @see http://cloudapi.acquia.com/#GET__sites__site_sshkeys__sshkeyid-instance_route
255
     */
256 3
    public function sshKey($site, $keyId)
257
    {
258
        $variables = array(
259 3
            'site' => $site,
260 3
            'id' => $keyId,
261 3
        );
262 3
        $request = $this->get(array('{+base_path}/sites/{site}/sshkeys/{id}.json', $variables));
263 3
        return new Response\SshKey($request);
264
    }
265
266
    /**
267
     * @param string $site
268
     * @param string $publicKey
269
     * @param string $nickname
270
     * @param bool $shellAccess
271
     * @param bool $vcsAccess
272
     * @param array $blacklist
273
     *
274
     * @return \Acquia\Cloud\Api\Response\Task
275
     *
276
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
277
     *
278
     * @see http://cloudapi.acquia.com/#POST__sites__site_sshkeys-instance_route
279
     */
280 3
    public function addSshKey($site, $publicKey, $nickname, $shellAccess = true, $vcsAccess = true, $blacklist = array())
281
    {
282 3
        $path = '{+base_path}/sites/{site}/sshkeys.json?nickname={nickname}';
283
        $variables = array(
284 3
            'site' => $site,
285 3
            'nickname' => $nickname,
286 3
        );
287 3
        $body = Json::encode(array(
288 3
          'ssh_pub_key' => $publicKey,
289 3
          'shell_access' => $shellAccess,
290 3
          'vcs_access' => $vcsAccess,
291 3
          'blacklist' => $blacklist,
292 3
        ));
293 3
        $request = $this->post(array($path, $variables), null, $body);
294 3
        return new Response\Task($request);
295
    }
296
297
    /**
298
     * @param string $site
299
     * @param int $keyId
300
     *
301
     * @return \Acquia\Cloud\Api\Response\Task
302
     *
303
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException|\InvalidArgumentException
304
     *
305
     * @see http://cloudapi.acquia.com/#DELETE__sites__site_sshkeys__sshkeyid-instance_route
306
     */
307 9
    public function deleteSshKey($site, $keyId)
308
    {
309 9
        if (!is_string($site)) {
310 3
            throw new \InvalidArgumentException('The site parameter must be a string.');
311
        }
312 6
        if (!is_int($keyId)) {
313 3
            throw new \InvalidArgumentException('The keyId parameter must be an integer.');
314
        }
315
        $variables = array(
316 3
            'site' => $site,
317 3
            'id' => $keyId,
318 3
        );
319 3
        $request = $this->delete(array('{+base_path}/sites/{site}/sshkeys/{id}.json', $variables));
320 3
        return new Response\Task($request);
321
    }
322
323
    /**
324
     * @param string $site
325
     *
326
     * @return \Acquia\Cloud\Api\Response\SvnUsers
327
     *
328
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
329
     *
330
     * @see http://cloudapi.acquia.com/#GET__sites__site_svnusers-instance_route
331
     */
332 3
    public function svnUsers($site)
333
    {
334 3
        $variables = array('site' => $site);
335 3
        $request = $this->get(array('{+base_path}/sites/{site}/svnusers.json', $variables));
336 3
        return new Response\SvnUsers($request);
337
    }
338
339
    /**
340
     * @param string $site
341
     * @param int $userId
342
     *
343
     * @return \Acquia\Cloud\Api\Response\SvnUser
344
     *
345
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
346
     *
347
     * @see http://cloudapi.acquia.com/#GET__sites__site_svnusers__svnuserid-instance_route
348
     */
349 3
    public function svnUser($site, $userId)
350
    {
351
        $variables = array(
352 3
            'site' => $site,
353 3
            'id' => $userId,
354 3
        );
355 3
        $request = $this->get(array('{+base_path}/sites/{site}/svnusers/{id}.json', $variables));
356 3
        return new Response\SvnUser($request);
357
    }
358
359
    /**
360
     * @param string $site
361
     * @param string $username
362
     * @param string $password
363
     *
364
     * @return \Acquia\Cloud\Api\Response\Task
365
     *
366
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
367
     *
368
     * @see http://cloudapi.acquia.com/#POST__sites__site_svnusers__username-instance_route
369
     *
370
     * @todo Testing returned a 400 response.
371
     */
372 3
    public function addSvnUser($site, $username, $password)
373
    {
374 3
        $path = '{+base_path}/sites/{site}/svnusers/{username}.json';
375
        $variables = array(
376 3
            'site' => $site,
377 3
            'username' => $username,
378 3
        );
379 3
        $body = Json::encode(array('password' => $password));
380 3
        $request = $this->post(array($path, $variables), null, $body);
381 3
        return new Response\Task($request);
382
    }
383
384
    /**
385
     * @param string $site
386
     * @param int $userId
387
     *
388
     * @return \Acquia\Cloud\Api\Response\Task
389
     *
390
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
391
     *
392
     * @see http://cloudapi.acquia.com/#DELETE__sites__site_svnusers__svnuserid-instance_route
393
     *
394
     * @todo Testing returned a 400 response.
395
     */
396 3
    public function deleteSvnUser($site, $userId)
397
    {
398
        $variables = array(
399 3
            'site' => $site,
400 3
            'id' => $userId,
401 3
        );
402 3
        $request = $this->delete(array('{+base_path}/sites/{site}/svnusers/{id}.json', $variables));
403 3
        return new Response\Task($request);
404
    }
405
406
    /**
407
     * @param string $site
408
     *
409
     * @return \Acquia\Cloud\Api\Response\DatabaseNames
410
     *
411
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
412
     *
413
     * @see http://cloudapi.acquia.com/#GET__sites__site_dbs-instance_route
414
     */
415 6
    public function databases($site)
416
    {
417 6
        $variables = array('site' => $site);
418 6
        $request = $this->get(array('{+base_path}/sites/{site}/dbs.json', $variables));
419 6
        return new Response\DatabaseNames($request);
420
    }
421
422
    /**
423
     * @param string $site
424
     * @param string $db
425
     *
426
     * @return \Acquia\Cloud\Api\Response\DatabaseName
427
     *
428
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
429
     *
430
     * @see http://cloudapi.acquia.com/#GET__sites__site_dbs__db-instance_route
431
     */
432 6
    public function database($site, $db)
433
    {
434
        $variables = array(
435 6
            'site' => $site,
436 6
            'db' => $db,
437 6
        );
438 6
        $request = $this->get(array('{+base_path}/sites/{site}/dbs/{db}.json', $variables));
439 6
        return new Response\DatabaseName($request);
440
    }
441
442
    /**
443
     * @param string $site
444
     * @param string $db
445
     * @param array $cluster_map
446
     *   Optional. A mapping containing all environments and the cluster to which
447
     *   the associated database should be created. Each entry consists of the
448
     *   environment name as the key and the database cluster ID as the value.
449
     *   Note that if more than one cluster is associated with a site group,
450
     *   this map is required.
451
     *
452
     * @return \Acquia\Cloud\Api\Response\Task
453
     *
454
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
455
     *
456
     * @see http://cloudapi.acquia.com/#POST__sites__site_dbs-instance_route
457
     */
458 6
    public function addDatabase($site, $db, $cluster_map = NULL)
459
    {
460 6
        $variables = array('site' => $site);
461 6
        $options = array();
462 6
        if (is_array($cluster_map) && !empty($cluster_map)) {
463 3
            foreach ($cluster_map as $env => $db_cluster) {
464 3
                if (is_string($env) && !empty($env) &&
465 3
                  intval($db_cluster) > 0) {
466 3
                    $options[$env] = array('db_cluster' => (string) $db_cluster);
467 3
                }
468 3
            }
469 3
        }
470 6
        $body = array('db' => $db);
471 6
        if (count($options) > 0) {
472 3
            $body['options'] = $options;
473 3
        }
474 6
        $body = Json::encode($body);
475 6
        $request = $this->post(array('{+base_path}/sites/{site}/dbs.json', $variables), null, $body);
476 6
        return new Response\Task($request);
477
    }
478
479
    /**
480
     * @param string $site
481
     * @param string $db
482
     * @param bool $backup
483
     *   Optional. If TRUE, a final backup of the database instance in each
484
     *   environment is made before deletion.
485
     *
486
     * @return \Acquia\Cloud\Api\Response\Task
487
     *
488
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
489
     *
490
     * @see http://cloudapi.acquia.com/#DELETE__sites__site_dbs__db-instance_route
491
     */
492 6
    public function deleteDatabase($site, $db, $backup = TRUE)
493
    {
494
        $variables = array(
495 6
            'site' => $site,
496 6
            'db' => $db,
497 6
            'backup' => $backup ? 1 : 0,
498 6
        );
499 6
        $request = $this->delete(array('{+base_path}/sites/{site}/dbs/{db}.json?backup={backup}', $variables));
500 6
        return new Response\Task($request);
501
    }
502
503
    /**
504
     * @param string $site
505
     * @param string $env
506
     *
507
     * @return \Acquia\Cloud\Api\Response\Databases
508
     *
509
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
510
     *
511
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_dbs-instance_route
512
     */
513 3 View Code Duplication
    public function environmentDatabases($site, $env)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
514
    {
515
        $variables = array(
516 3
            'site' => $site,
517 3
            'env' => $env,
518 3
        );
519 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/dbs.json', $variables));
520 3
        return new Response\Databases($request);
521
    }
522
523
    /**
524
     * @param string $site
525
     * @param string $env
526
     * @param string $db
527
     *
528
     * @return \Acquia\Cloud\Api\Response\Database
529
     *
530
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
531
     *
532
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_dbs__db-instance_route
533
     */
534 3
    public function environmentDatabase($site, $env, $db)
535
    {
536
        $variables = array(
537 3
            'site' => $site,
538 3
            'env' => $env,
539 3
            'db' => $db,
540 3
        );
541 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}.json', $variables));
542 3
        return new Response\Database($request);
543
    }
544
545
    /**
546
     * @param string $site
547
     * @param string $env
548
     * @param string $db
549
     *
550
     * @return \Acquia\Cloud\Api\Response\DatabaseBackups
551
     *
552
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
553
     *
554
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_dbs__db_backups-instance_route
555
     */
556 3
    public function databaseBackups($site, $env, $db)
557
    {
558
        $variables = array(
559 3
            'site' => $site,
560 3
            'env' => $env,
561 3
            'db' => $db,
562 3
        );
563 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}/backups.json', $variables));
564 3
        return new Response\DatabaseBackups($request);
565
    }
566
567
    /**
568
     * @param string $site
569
     * @param string $env
570
     * @param string $db
571
     * @param int $backupId
572
     *
573
     * @return \Acquia\Cloud\Api\Response\Task
574
     *
575
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
576
     *
577
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_dbs__db_backups__backup-instance_route
578
     */
579 3
    public function databaseBackup($site, $env, $db, $backupId)
580
    {
581
        $variables = array(
582 3
            'site' => $site,
583 3
            'env' => $env,
584 3
            'db' => $db,
585 3
            'id' => $backupId,
586 3
        );
587 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}/backups/{id}.json', $variables));
588 3
        return new Response\DatabaseBackup($request);
589
    }
590
591
    /**
592
     * @param string $site
593
     * @param string $env
594
     * @param string $db
595
     * @param int $backupId
596
     *
597
     * @return \Acquia\Cloud\Api\Response\Task
598
     *
599
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
600
     *
601
     * @see http://cloudapi.acquia.com/#DELETE__sites__site_envs__env_dbs__db_backups__backup-instance_route
602
     */
603 3
    public function deleteDatabaseBackup($site, $env, $db, $backupId)
604
    {
605
      $variables = array(
606 3
        'site' => $site,
607 3
        'env' => $env,
608 3
        'db' => $db,
609 3
        'backup' => $backupId,
610 3
      );
611 3
      $request = $this->delete(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}/backups/{backup}.json', $variables));
612 3
      return new Response\Task($request);
613
    }
614
615
    /**
616
     * @param string $site
617
     * @param string $env
618
     * @param string $db
619
     * @param int $backupId
620
     * @param string $outfile
621
     *
622
     * @return \Guzzle\Http\Message\Response
623
     *
624
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
625
     *
626
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_dbs__db_backups__backup_download-instance_route
627
     */
628 3
    public function downloadDatabaseBackup($site, $env, $db, $backupId, $outfile)
629
    {
630
        $variables = array(
631 3
            'site' => $site,
632 3
            'env' => $env,
633 3
            'db' => $db,
634 3
            'id' => $backupId,
635 3
        );
636 3
        return $this
637 3
            ->get(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}/backups/{id}/download.json', $variables))
638 3
            ->setResponseBody($outfile)
639 3
            ->send()
640 3
        ;
641
    }
642
643
    /**
644
     * @param string $site
645
     * @param string $env
646
     * @param string $db
647
     *
648
     * @return \Acquia\Cloud\Api\Response\Task
649
     *
650
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
651
     *
652
     * @see http://cloudapi.acquia.com/#POST__sites__site_envs__env_dbs__db_backups-instance_route
653
     */
654 3
    public function createDatabaseBackup($site, $env, $db)
655
    {
656
        $variables = array(
657 3
            'site' => $site,
658 3
            'env' => $env,
659 3
            'db' => $db,
660 3
        );
661 3
        $request = $this->post(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}/backups.json', $variables));
662 3
        return new Response\Task($request);
663
    }
664
665
    /**
666
     * @param string $site
667
     * @param string $env
668
     * @param string $db
669
     * @param string $backupId
670
     *
671
     * @return \Acquia\Cloud\Api\Response\Task
672
     *
673
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
674
     *
675
     * @see http://cloudapi.acquia.com/#POST__sites__site_envs__env_dbs__db_backups__backup_restore-instance_route
676
     */
677 3
    public function restoreDatabaseBackup($site, $env, $db, $backupId)
678
    {
679
        $variables = array(
680 3
            'site' => $site,
681 3
            'env' => $env,
682 3
            'db' => $db,
683 3
            'id' => $backupId,
684 3
        );
685 3
        $request = $this->post(array('{+base_path}/sites/{site}/envs/{env}/dbs/{db}/backups/{id}/restore.json', $variables));
686 3
        return new Response\Task($request);
687
    }
688
689
    /**
690
     * @param string $site
691
     *
692
     * @return \Acquia\Cloud\Api\Response\Tasks
693
     *
694
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
695
     *
696
     * @see http://cloudapi.acquia.com/#GET__sites__site_tasks-instance_route
697
     */
698 3
    public function tasks($site)
699
    {
700
        $variables = array(
701 3
            'site' => $site,
702 3
        );
703 3
        $request = $this->get(array('{+base_path}/sites/{site}/tasks.json', $variables));
704 3
        return new Response\Tasks($request);
705
    }
706
707
    /**
708
     * @param string $site
709
     * @param int $taskId
710
     *
711
     * @return \Acquia\Cloud\Api\Response\Task
712
     *
713
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
714
     *
715
     * @see http://cloudapi.acquia.com/#GET__sites__site_tasks__task-instance_route
716
     */
717 6
    public function task($site, $taskId)
718
    {
719
        $variables = array(
720 6
            'site' => $site,
721 6
            'task' => $taskId,
722 6
        );
723 6
        $request = $this->get(array('{+base_path}/sites/{site}/tasks/{task}.json', $variables));
724 6
        return new Response\Task($request);
725
    }
726
727
    /**
728
     * @param string $site
729
     * @param string $env
730
     *
731
     * @return \Acquia\Cloud\Api\Response\Domains
732
     *
733
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
734
     *
735
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_domains-instance_route
736
     */
737 3 View Code Duplication
    public function domains($site, $env)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
738
    {
739
        $variables = array(
740 3
            'site' => $site,
741 3
            'env' => $env,
742 3
        );
743 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/domains.json', $variables));
744 3
        return new Response\Domains($request);
745
    }
746
747
    /**
748
     * @param string $site
749
     * @param string $env
750
     * @param string $domain
751
     *
752
     * @return \Acquia\Cloud\Api\Response\Domain
753
     *
754
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
755
     *
756
     * @see http://cloudapi.acquia.com/#GET__sites__site_envs__env_domains__domain-instance_route
757
     */
758 3 View Code Duplication
    public function domain($site, $env, $domain)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
759
    {
760
        $variables = array(
761 3
            'site' => $site,
762 3
            'env' => $env,
763 3
            'domain' => $domain,
764 3
        );
765 3
        $request = $this->get(array('{+base_path}/sites/{site}/envs/{env}/domains/{domain}.json', $variables));
766 3
        return new Response\Domain($request);
767
    }
768
769
    /**
770
     * @param string $site
771
     * @param string $env
772
     * @param string $domain
773
     *
774
     * @return \Acquia\Cloud\Api\Response\Task
775
     *
776
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
777
     *
778
     * @see http://cloudapi.acquia.com/#POST__sites__site_envs__env_domains__domain-instance_route
779
     */
780 3 View Code Duplication
    public function addDomain($site, $env, $domain)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
781
    {
782
      $variables = array(
783 3
        'site' => $site,
784 3
        'env' => $env,
785 3
        'domain' => $domain,
786 3
      );
787 3
      $request = $this->post(array('{+base_path}/sites/{site}/envs/{env}/domains/{domain}.json', $variables));
788 3
      return new Response\Task($request);
789
    }
790
791
    /**
792
     * Moves domains atomically from one environment to another.
793
     *
794
     * @param string $site
795
     *   The site.
796
     * @param string|array $domains
797
     *   The domain name(s) as an array of strings, or the string '*' to move all
798
     *   domains.
799
     * @param string $sourceEnv
800
     *   The environment which currently has this domain.
801
     * @param string $targetEnv
802
     *   The destination environment for the domain.
803
     * @param bool $skipSiteUpdate
804
     *   Optional. If set to TRUE this will inhibit running
805
     *   fields-config-web.php for this domain move.
806
     *
807
     * @return \Acquia\Cloud\Api\Response\Task
808
     *
809
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
810
     *
811
     * @see http://cloudapi.acquia.com/#POST__sites__site_domain_move__source__target-instance_route
812
     */
813 6
    public function moveDomain($site, $domains, $sourceEnv, $targetEnv, $skipSiteUpdate = FALSE)
814
    {
815 6
        $paths = '{+base_path}/sites/{site}/domain-move/{source}/{target}.json';
816 6
        $update_site = '';
817 6
        if ($skipSiteUpdate) {
818 3
            $update_site = '1';
819 3
            $paths .= '?skip_site_update={update_site}';
820 3
        }
821
        $variables = array(
822 6
          'site' => $site,
823 6
          'source' => $sourceEnv,
824 6
          'target' => $targetEnv,
825 6
          'update_site' => $update_site,
826 6
        );
827 6
        $body = Json::encode(array('domains' => (array) $domains));
828 6
        $request = $this->post(array($paths, $variables), null, $body);
829 6
        return new Response\Task($request);
830
    }
831
832
    /**
833
     * @param string $site
834
     * @param string $env
835
     * @param string $domain
836
     *
837
     * @return \Acquia\Cloud\Api\Response\Task
838
     *
839
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
840
     *
841
     * @see http://cloudapi.acquia.com/#DELETE__sites__site_envs__env_domains__domain-instance_route
842
     */
843 3 View Code Duplication
    public function deleteDomain($site, $env, $domain)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
844
    {
845
      $variables = array(
846 3
        'site' => $site,
847 3
        'env' => $env,
848 3
        'domain' => $domain,
849 3
      );
850 3
      $request = $this->delete(array('{+base_path}/sites/{site}/envs/{env}/domains/{domain}.json', $variables));
851 3
      return new Response\Task($request);
852
    }
853
854
    /**
855
     * @param string $site
856
     * @param string $env
857
     * @param string $domain
858
     *
859
     * @return \Acquia\Cloud\Api\Response\Task
860
     *
861
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
862
     *
863
     * @see http://cloudapi.acquia.com/#DELETE__sites__site_envs__env_domains__domain_cache-instance_route
864
     */
865 3 View Code Duplication
    public function purgeVarnishCache($site, $env, $domain)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
866
    {
867
        $variables = array(
868 3
            'site' => $site,
869 3
            'env' => $env,
870 3
            'domain' => $domain,
871 3
        );
872 3
        $request = $this->delete(array('{+base_path}/sites/{site}/envs/{env}/domains/{domain}/cache.json', $variables));
873 3
        return new Response\Task($request);
874
    }
875
876
    /**
877
     * @param string $site
878
     * @param string $db
879
     * @param string $sourceEnv
880
     * @param string $targetEnv
881
     *
882
     * @return \Acquia\Cloud\Api\Response\Task
883
     *
884
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
885
     *
886
     * @see http://cloudapi.acquia.com/#POST__sites__site_dbs__db_db_copy__source__target-instance_route
887
     */
888 3
    public function copyDatabase($site, $db, $sourceEnv, $targetEnv)
889
    {
890
        $variables = array(
891 3
            'site' => $site,
892 3
            'db' => $db,
893 3
            'source' => $sourceEnv,
894 3
            'target' => $targetEnv,
895 3
        );
896 3
        $request = $this->post(array('{+base_path}/sites/{site}/dbs/{db}/db-copy/{source}/{target}.json', $variables));
897 3
        return new Response\Task($request);
898
    }
899
900
    /**
901
     * @param string $site
902
     * @param string $sourceEnv
903
     * @param string $targetEnv
904
     *
905
     * @return \Acquia\Cloud\Api\Response\Task
906
     *
907
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
908
     *
909
     * @see http://cloudapi.acquia.com/#POST__sites__site_files_copy__source__target-instance_route
910
     */
911 3
    public function copyFiles($site, $sourceEnv, $targetEnv)
912
    {
913
        $variables = array(
914 3
            'site' => $site,
915 3
            'source' => $sourceEnv,
916 3
            'target' => $targetEnv,
917 3
        );
918 3
        $request = $this->post(array('{+base_path}/sites/{site}/files-copy/{source}/{target}.json', $variables));
919 3
        return new Response\Task($request);
920
    }
921
922
    /**
923
     * @param string $site
924
     * @param string $env
925
     * @param string $action
926
     * @param bool $discard
927
     *
928
     * @return \Acquia\Cloud\Api\Response\Task
929
     *
930
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
931
     *
932
     * @see http://cloudapi.acquia.com/#POST__sites__site_envs__env_livedev__action-instance_route
933
     */
934 6
    public function liveDev($site, $env, $action, $discard = false)
935
    {
936
        $variables = array(
937 6
            'site' => $site,
938 6
            'env' => $env,
939 6
            'action' => $action,
940 6
            'discard' => (int) $discard,
941 6
        );
942 6
        $request = $this->post(array('{+base_path}/sites/{site}/envs/{env}/livedev/{action}.json?discard={discard}', $variables));
943 6
        return new Response\Task($request);
944
    }
945
946
    /**
947
     * @param string $site
948
     * @param string $env
949
     * @param bool $discard
950
     *
951
     * @return \Acquia\Cloud\Api\Response\Task
952
     *
953
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
954
     */
955 3
    public function enableLiveDev($site, $env, $discard = false)
956
    {
957 3
        return $this->liveDev($site, $env, self::LIVEDEV_ENABLE, $discard);
958
    }
959
960
    /**
961
     * @param string $site
962
     * @param string $env
963
     * @param bool $discard
964
     *
965
     * @return \Acquia\Cloud\Api\Response\Task
966
     *
967
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
968
     */
969 3
    public function disableLiveDev($site, $env, $discard = false)
970
    {
971 3
        return $this->liveDev($site, $env, self::LIVEDEV_DISABLE, $discard);
972
    }
973
974
    /**
975
     * Deploy code from on environment to another.
976
     *
977
     * @param string $site
978
     * @param string $sourceEnv
979
     * @param string $targetEnv
980
     *
981
     * @return \Acquia\Cloud\Api\Response\Task
982
     *
983
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
984
     *
985
     * @see http://cloudapi.acquia.com/#POST__sites__site_code_deploy__source__target-instance_route
986
     */
987 6
    public function deployCode($site, $sourceEnv, $targetEnv)
988
    {
989
        $variables = array(
990 6
            'site' => $site,
991 6
            'source' => $sourceEnv,
992 6
            'target' => $targetEnv,
993 6
        );
994 6
        $request = $this->post(array('{+base_path}/sites/{site}/code-deploy/{source}/{target}.json', $variables));
995 6
        return new Response\Task($request);
996
    }
997
998
    /**
999
     * Deploy a tag or branch to an environment.
1000
     *
1001
     * @param string $site
1002
     * @param string $env
1003
     * @param string $vcsPath
1004
     *
1005
     * @return \Acquia\Cloud\Api\Response\Task
1006
     *
1007
     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
1008
     *
1009
     * @see http://cloudapi.acquia.com/#POST__sites__site_envs__env_code_deploy-instance_route
1010
     */
1011 3
    public function pushCode($site, $env, $vcsPath)
1012
    {
1013
        $variables = array(
1014 3
            'site' => $site,
1015 3
            'env' => $env,
1016 3
            'path' => $vcsPath,
1017 3
        );
1018 3
        $request = $this->post(array('{+base_path}/sites/{site}/envs/{env}/code-deploy.json?path={path}', $variables));
1019 3
        return new Response\Task($request);
1020
    }
1021
1022
    /**
1023
     * @deprecated since version 0.5.0
1024
     */
1025 3
    public function taskInfo($site, $taskId)
1026
    {
1027 3
        return $this->task($site, $taskId);
1028
    }
1029
1030
    /**
1031
     * @deprecated since version 0.5.0
1032
     */
1033 3
    public function siteDatabases($site)
1034
    {
1035 3
        return $this->databases($site);
1036
    }
1037
1038
    /**
1039
     * @deprecated since version 0.5.0
1040
     */
1041 3
    public function siteDatabase($site, $db)
1042
    {
1043 3
        return $this->database($site, $db);
1044
    }
1045
1046
    /**
1047
     * @deprecated since version 0.5.0
1048
     */
1049 3
    public function codeDeploy($site, $source, $target)
1050
    {
1051 3
        return $this->deployCode($site, $source, $target);
1052
    }
1053
}
1054