protected function deleteMultiple(array $environments, InputInterface $input, OutputInterface $output)
{
// Confirm which environments the user wishes to be deleted.
$delete = [];
$deactivate = [];
$error = false;
$questionHelper = $this->getHelper('question');
foreach ($environments as $environment) {
$environmentId = $environment->id;
if ($environmentId == 'master') {
$output->writeln("The <error>master</error> environment cannot be deleted.");
$error = true;
continue;
}
// Check that the environment does not have children.
// @todo remove this check when Platform's behavior is fixed
foreach ($this->api()->getEnvironments($this->getSelectedProject()) as $potentialChild) {
if ($potentialChild->parent == $environment->id) {
$output->writeln("The environment <error>{$environmentId}</error> has children and therefore can't be deleted.");
$output->writeln("Please delete the environment's children first.");
$error = true;
continue 2;
}
}
if ($environment->isActive()) {
$output->writeln("The environment <comment>{$environmentId}</comment> is currently active: deleting it will delete all associated data.");
if ($questionHelper->confirm("Are you sure you want to delete the environment <comment>{$environmentId}</comment>?")) {
$deactivate[$environmentId] = $environment;
if (!$input->getOption('no-delete-branch')) {
if ($input->getOption('delete-branch') || $input->isInteractive() && $questionHelper->confirm("Delete the remote Git branch too?")) {
$delete[$environmentId] = $environment;
}
}
}
} elseif ($environment->status === 'inactive') {
if ($questionHelper->confirm("Are you sure you want to delete the remote Git branch <comment>{$environmentId}</comment>?")) {
$delete[$environmentId] = $environment;
}
} elseif ($environment->status === 'dirty') {
$output->writeln("The environment <error>{$environmentId}</error> is currently building, and therefore can't be deleted. Please wait.");
$error = true;
continue;
}
}
$deactivateActivities = [];
$deactivated = 0;
/** @var Environment $environment */
foreach ($deactivate as $environmentId => $environment) {
try {
$output->writeln("Deleting environment <info>{$environmentId}</info>");
$deactivateActivities[] = $environment->deactivate();
$deactivated++;
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}
if (!$input->getOption('no-wait')) {
if (!ActivityUtil::waitMultiple($deactivateActivities, $output, $this->getSelectedProject())) {
$error = true;
}
}
$deleted = 0;
foreach ($delete as $environmentId => $environment) {
try {
if ($environment->status !== 'inactive') {
$environment->refresh();
if ($environment->status !== 'inactive') {
$output->writeln("Cannot delete branch <error>{$environmentId}</error>: it is not (yet) inactive.");
continue;
}
}
$environment->delete();
$output->writeln("Deleted remote Git branch <info>{$environmentId}</info>");
$deleted++;
} catch (\Exception $e) {
$output->writeln($e->getMessage());
}
}
if ($deleted < count($delete) || $deactivated < count($deactivate)) {
$error = true;
}
if (($deleted || $deactivated || $error) && isset($environment)) {
$this->api()->clearEnvironmentsCache($environment->project);
}
return !$error;
}