Skip to content

Commit

Permalink
Refines db commands and runs phpmd over codebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 21, 2020
1 parent 665ebe8 commit 2431ef1
Show file tree
Hide file tree
Showing 24 changed files with 640 additions and 159 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"AcquiaCli\\":"src"
}
},
"autoload-dev": {
"psr-4":{
"AcquiaCli\\Tests\\": "tests/"
}
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0.0",
"squizlabs/php_codesniffer": "^3.1",
Expand Down
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Acquia CLI">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="tests/logs/clover.xml"/>
</logging>
<php>
<const name="PHPUNIT_ACQUIACLI_TESTSUITE" value="true"/>
</php>
</phpunit>

1 change: 1 addition & 0 deletions src/Cli/AcquiaCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public function injectParameters($container)
$parameterInjection->register('AcquiaCloudApi\Endpoints\Insights', new AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\LogForwardingDestinations', new AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\SslCertificates', new AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Organizations', new AcquiaCliInjector);
}

/**
Expand Down
31 changes: 6 additions & 25 deletions src/Commands/AcquiaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,6 @@ public function __construct()
$this->setTableStyles();
}

public function getCloudApi()
{
$cloudapi = Robo::service('cloudApi')->getCloudApi();
return $cloudapi;
}

public function getEnvironments()
{
$environments = Robo::service('cloudApi')->getEnvironments();
return $environments;
}

public function getApplications()
{
$applications = Robo::service('cloudApi')->getApplications();
return $applications;
}

/**
* Override the confirm method from consolidation/Robo to allow automatic
* confirmation.
Expand Down Expand Up @@ -196,7 +178,7 @@ protected function waitForNotification($response)
$progress->setMessage('Looking up notification');
$progress->start();

$notificationAdapter = new Notifications($this->getCloudApi());
$notificationAdapter = new Notifications($this->cloudapi);

while (true) {
$progress->advance($sleep);
Expand All @@ -210,7 +192,7 @@ protected function waitForNotification($response)
case self::TASKFAILED:
// If there's one failure we should throw an exception
throw new \Exception('Acquia task failed.');
break(2);
break(2);
// If tasks are started or in progress, we should continue back
// to the top of the loop and wait until tasks are complete.
case self::TASKSTARTED:
Expand All @@ -221,7 +203,7 @@ protected function waitForNotification($response)
break(2);
default:
throw new \Exception('Unknown notification status.');
break(2);
break(2);
}

// Timeout if the command exceeds the configured timeout threshold.
Expand All @@ -246,7 +228,6 @@ protected function waitForNotification($response)
*/
protected function backupAndMoveDbs($uuid, $environmentFrom, $environmentTo, $dbName = null)
{

if (null !== $dbName) {
$this->cloudapi->addQuery('filter', "name=${dbName}");
}
Expand All @@ -268,7 +249,7 @@ protected function backupAndMoveDbs($uuid, $environmentFrom, $environmentTo, $db
)
);

$databaseAdapter = new Databases($this->getCloudApi());
$databaseAdapter = new Databases($this->cloudapi);
$response = $databaseAdapter->copy($environmentFrom->uuid, $database->name, $environmentTo->uuid);
$this->waitForNotification($response);
}
Expand All @@ -280,7 +261,7 @@ protected function backupAndMoveDbs($uuid, $environmentFrom, $environmentTo, $db
*/
protected function backupAllEnvironmentDbs($uuid, $environment)
{
$dbAdapter = new Databases($this->getCloudApi());
$dbAdapter = new Databases($this->cloudapi);
$databases = $dbAdapter->getAll($uuid);
foreach ($databases as $database) {
$this->backupDb($uuid, $environment, $database);
Expand All @@ -296,7 +277,7 @@ protected function backupDb($uuid, $environment, $database)
{
// Run database backups.
$this->say(sprintf('Backing up DB (%s) on %s', $database->name, $environment->label));
$dbAdapter = new DatabaseBackups($this->getCloudApi());
$dbAdapter = new DatabaseBackups($this->cloudapi);
$response = $dbAdapter->create($environment->uuid, $database->name);
$this->waitForNotification($response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ApplicationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function applicationTagDelete(Applications $applicationsAdapter, $uuid, $
* @param string $name
*
* @command application:rename
* @alias app:rename,a:rename
* @aliases app:rename,a:rename
*/
public function applicationRename(Applications $applicationsAdapter, $uuid, $name)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/CodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function code(Client $client, Code $codeAdapter, $uuid, $match = null)
/**
* Deploys code from one environment to another.
*
* @param string $uuid
* @param string $environmentFrom
* @param string $environmentTo
* @param string $uuid
* @param string $environmentFrom
* @param string $environmentTo
*
* @command code:deploy
* @aliases c:d
Expand Down
38 changes: 19 additions & 19 deletions src/Commands/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class CronCommand extends AcquiaCommand
/**
* Shows all cron tasks associated with an environment.
*
* @param string $uuid
* @param string $environment
* @param string $uuid
* @param string $environment
*
* @command cron:list
*/
Expand Down Expand Up @@ -55,11 +55,11 @@ public function crons(Crons $cronAdapter, $uuid, $environment)
/**
* Adds a new cron task for an environment.
*
* @param string $uuid
* @param string $environment
* @param string $commandString The command to be run on cron wrapped in quotes.
* @param string $frequency The crontab format frequency wrapped in quotes
* @param string $label An optional label for the cron command wrapped in quotes.
* @param string $uuid
* @param string $environment
* @param string $commandString The command to be run on cron wrapped in quotes.
* @param string $frequency The crontab format frequency wrapped in quotes
* @param string $label An optional label for the cron command wrapped in quotes.
*
* @command cron:create
* @aliases cron:add
Expand All @@ -75,9 +75,9 @@ public function cronAdd(Crons $cronAdapter, $uuid, $environment, $commandString,
/**
* Removes a cron task for an environment.
*
* @param string $uuid
* @param string $environment
* @param int $cronId
* @param string $uuid
* @param string $environment
* @param int $cronId
*
* @command cron:delete
* @aliases cron:remove
Expand All @@ -95,9 +95,9 @@ public function cronDelete(Crons $cronAdapter, $uuid, $environment, $cronId)
/**
* Enables a disabled cron entry.
*
* @param string $uuid
* @param string $environment
* @param int $cronId
* @param string $uuid
* @param string $environment
* @param int $cronId
*
* @command cron:enable
*/
Expand All @@ -112,9 +112,9 @@ public function cronEnable(Crons $cronAdapter, $uuid, $environment, $cronId)
/**
* Disables an enabled cron entry.
*
* @param string $uuid
* @param string $environment
* @param int $cronId
* @param string $uuid
* @param string $environment
* @param int $cronId
*
* @command cron:disable
*/
Expand All @@ -131,9 +131,9 @@ public function cronDisable(Crons $cronAdapter, $uuid, $environment, $cronId)
/**
* Shows detailed information about a single cron command.
*
* @param string $uuid
* @param string $environment
* @param int $cronId
* @param string $uuid
* @param string $environment
* @param int $cronId
*
* @command cron:info
*/
Expand Down
64 changes: 44 additions & 20 deletions src/Commands/DbBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,55 @@ class DbBackupCommand extends AcquiaCommand
/**
* Backs up all DBs in an environment.
*
* @param string $uuid
* @param string $environment
* @param string $uuid
* @param string $environment
*
* @command database:backup:all
* @aliases db:backup:all
*/
public function dbBackupAll($uuid, $environment)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
$this->backupAllEnvironmentDbs($uuid, $environment);
}

/**
* Backs up a DB in an environment.
*
* @param string $uuid
* @param string $environment
* @param string $dbName
*
* @command database:backup
* @aliases db:backup
*/
public function dbBackup($uuid, $environment)
public function dbBackup(Databases $databaseAdapter, $uuid, $environment, $dbName)
{
$environment = $this->cloudapiService->getEnvironment($uuid, $environment);
$this->backupAllEnvironmentDbs($uuid, $environment);

// @TODO replace this call with a specific API call.
// https://cloudapi-docs.acquia.com/#/Environments/getEnvironmentsDatabase
$databases = $databaseAdapter->getAll($uuid);
foreach ($databases as $database) {
if ($database->name === $dbName) {
$this->backupDb($uuid, $environment, $database);
}
}
}

/**
* Shows a list of database backups for all databases in an environment.
*
* @param string $uuid
* @param string $environment
* @param string $dbName
* @param string $uuid
* @param string $environment
* @param string $dbName
*
* @command database:backup:list
* @aliases db:backup:list
*/
public function dbBackupList(
Client $client,
Databases $databaseAdapter,
DatabaseBackups $databaseBackupsAdapter,
$uuid,
$environment,
Expand All @@ -61,8 +86,7 @@ public function dbBackupList(
if (null !== $dbName) {
$client->addQuery('filter', "name=${dbName}");
}
$dbAdapter = new Databases($this->cloudapi);
$databases = $dbAdapter->getAll($uuid);
$databases = $databaseAdapter->getAll($uuid);
$client->clearQuery();

$table = new Table($this->output());
Expand Down Expand Up @@ -97,10 +121,10 @@ public function dbBackupList(
/**
* Restores a database from a saved backup.
*
* @param string $uuid
* @param string $environment
* @param string $dbName
* @param int $backupId
* @param string $uuid
* @param string $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:restore
* @aliases db:backup:restore
Expand All @@ -122,10 +146,10 @@ public function dbBackupRestore(DatabaseBackups $databaseBackupsAdapter, $uuid,
/**
* Provides a database backup link.
*
* @param string $uuid
* @param string $environment
* @param string $dbName
* @param int $backupId
* @param string $uuid
* @param string $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:link
* @aliases db:backup:link
Expand All @@ -148,9 +172,9 @@ public function dbBackupLink($uuid, $environment, $dbName, $backupId)
/**
* Downloads a database backup.
*
* @param string $uuid
* @param string $environment
* @param string $dbName
* @param string $uuid
* @param string $environment
* @param string $dbName
*
* @throws \Exception
*
Expand Down
Loading

0 comments on commit 2431ef1

Please sign in to comment.