You are here

public function SalesforceCommands::interactCreateObject in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 src/Commands/SalesforceCommands.php \Drupal\salesforce\Commands\SalesforceCommands::interactCreateObject()
  2. 5.0.x src/Commands/SalesforceCommands.php \Drupal\salesforce\Commands\SalesforceCommands::interactCreateObject()

Fetch an object type and object data interactively.

@hook interact salesforce:create-object

File

src/Commands/SalesforceCommands.php, line 500

Class

SalesforceCommands
A Drush commandfile.

Namespace

Drupal\salesforce\Commands

Code

public function interactCreateObject(Input $input, Output $output) {
  $format = $input
    ->getOption('encoding');
  if (empty($format)) {
    $input
      ->setOption('encoding', 'query');
    $format = 'query';
  }
  elseif (!in_array($input
    ->getOption('encoding'), [
    'query',
    'json',
  ])) {
    throw new \Exception('Invalid encoding');
  }
  $this
    ->interactObject($input, $output, 'Enter the object type to be created');
  if (!($data = $this
    ->io()
    ->ask('Enter the object data to be created'))) {
    throw new UserAbortException();
  }
  $params = [];
  switch ($format) {
    case 'query':
      parse_str($data, $params);
      if (empty($params)) {
        throw new \Exception(dt('Error when decoding data'));
      }
      break;
    case 'json':
      $params = json_decode($data, TRUE);
      if (json_last_error()) {
        throw new \Exception(dt('Error when decoding data: !error', [
          '!error' => json_last_error_msg(),
        ]));
      }
      break;
  }
  $this
    ->input()
    ->setArgument('data', $params);
}