/**
  * @param Activity $activity
  * @param OutputInterface $output
  * @param bool $poll
  * @param float|int $interval A refresh interval (in seconds).
  */
 protected function displayLog(Activity $activity, OutputInterface $output, $poll = true, $interval = 1)
 {
     $logger = function ($log) use($output) {
         $output->write($log);
     };
     if (!$poll) {
         $logger($activity['log']);
         return;
     }
     $activity->wait(null, $logger, $interval);
 }
 /**
  * Wait for a single activity to complete, and display the log.
  *
  * @param Activity        $activity
  * @param OutputInterface $output
  * @param string          $success
  * @param string          $failure
  *
  * @return bool
  *   True if the activity succeeded, false otherwise.
  */
 public static function waitAndLog(Activity $activity, OutputInterface $output, $success = null, $failure = null)
 {
     $output->writeln('Waiting for the remote operation to complete...');
     $activity->wait(null, function ($log) use($output) {
         $output->write(preg_replace('/^/m', '    ', $log));
     });
     switch ($activity['result']) {
         case 'success':
             if ($success !== null) {
                 $output->writeln($success);
             }
             return true;
         case 'failure':
             if ($failure !== null) {
                 $output->writeln($failure);
             }
     }
     return false;
 }
 /**
  * Wait for a single activity to complete, and display the log continuously.
  *
  * @param Activity        $activity
  * @param OutputInterface $output
  * @param string          $success
  * @param string          $failure
  *
  * @return bool
  *   True if the activity succeeded, false otherwise.
  */
 public static function waitAndLog(Activity $activity, OutputInterface $output, $success = null, $failure = null)
 {
     $output->writeln('Waiting for the activity <info>' . $activity->id . '</info> (' . $activity->getDescription() . "):");
     // Initialize a progress bar which will show elapsed time and the
     // activity's state.
     $bar = new ProgressBar($output);
     $bar->setPlaceholderFormatterDefinition('state', function () use($activity) {
         return self::formatState($activity->state);
     });
     $bar->setFormat("  [%bar%] %elapsed:6s% (%state%)");
     $bar->start();
     // Wait for the activity to complete.
     $activity->wait(function () use($bar) {
         $bar->advance();
     }, function ($log) use($output, $bar) {
         // Clear the progress bar and ensure the current line is flushed.
         $bar->clear();
         $output->write($output->isDecorated() ? "\n" : "\n");
         // Display the new log output, with an indent.
         $output->write(preg_replace('/^/m', '    ', $log));
         // Display the progress bar again.
         $bar->advance();
     });
     $bar->finish();
     $output->writeln('');
     // Display the success or failure messages.
     switch ($activity['result']) {
         case Activity::RESULT_SUCCESS:
             $output->writeln($success ?: "Activity <info>{$activity->id}</info> succeeded");
             return true;
         case Activity::RESULT_FAILURE:
             $output->writeln($failure ?: "Activity <error>{$activity->id}</error> failed");
             return false;
     }
     return false;
 }
 /**
  * Get a list of environment activities.
  *
  * @param int    $limit
  * @param string $type
  *
  * @return Activity[]
  */
 public function getActivities($limit = 0, $type = null)
 {
     $options = [];
     if ($type !== null) {
         $options['query']['type'] = $type;
     }
     return Activity::getCollection($this->getUri() . '/activities', $limit, $options, $this->client);
 }
 /**
  * Run a long-running operation.
  *
  * @param string $op
  * @param string $method
  * @param array  $body
  *
  * @throws \Exception
  *
  * @return Activity
  */
 protected function runLongOperation($op, $method = 'post', array $body = [])
 {
     $data = $this->runOperation($op, $method, $body);
     if (!isset($data['_embedded']['activities'][0])) {
         throw new \Exception('Expected activity not found');
     }
     return Activity::wrap($data['_embedded']['activities'][0], $this->baseUrl, $this->client);
 }