View source
<?php
namespace Drupal\salesforce_mapping\Commands;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\salesforce\Rest\RestClient;
use Drush\Drush;
use Drush\Exceptions\UserAbortException;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Output\Output;
use Drupal\salesforce\Commands\SalesforceCommandsBase;
use Drupal\salesforce\Commands\QueryResult;
use Drupal\salesforce\Commands\QueryResultTableFormatter;
abstract class SalesforceMappingCommandsBase extends SalesforceCommandsBase {
protected $mappingStorage;
protected $mappedObjectStorage;
public function __construct(RestClient $client, EntityTypeManagerInterface $etm) {
parent::__construct($client, $etm);
$this->mappingStorage = $etm
->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $etm
->getStorage('salesforce_mapped_object');
}
protected function interactMapping(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE, $dir = NULL) {
if ($name = $input
->getArgument('name')) {
if (strtoupper($name) == 'ALL') {
$input
->setArgument('name', 'ALL');
return;
}
$mapping = $this->mappingStorage
->load($name);
if (!$mapping) {
$this
->logger()
->error(dt('Mapping %name does not exist.', [
'%name' => $name,
]));
}
elseif ($dir == 'push' && !$mapping
->doesPush()) {
$this
->logger()
->error(dt('Mapping %name does not push.', [
'%name' => $name,
]));
}
elseif ($dir == 'pull' && !$mapping
->doesPull()) {
$this
->logger()
->error(dt('Mapping %name does not push.', [
'%name' => $name,
]));
}
else {
return;
}
}
if ($dir == 'pull') {
$options = $this->mappingStorage
->loadPullMappings();
}
elseif ($dir == 'push') {
$options = $this->mappingStorage
->loadPushMappings();
}
else {
$options = $this->mappingStorage
->loadMultiple();
}
$this
->doMappingNameOptions($input, array_keys($options), $message, $allOption);
}
protected function interactPushMappings(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE) {
return $this
->interactMapping($input, $output, $message, $allOption, 'push');
}
protected function interactPullMappings(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE) {
return $this
->interactMapping($input, $output, $message, $allOption, 'pull');
}
protected function doMappingNameOptions(Input $input, array $options, $message, $allOption = FALSE) {
$options = array_combine($options, $options);
if ($allOption) {
$options['ALL'] = $allOption;
}
if (!($answer = $this
->io()
->choice($message, $options))) {
throw new UserAbortException();
}
$input
->setArgument('name', $answer);
}
protected function getMappingsFromName($name, $dir = NULL) {
$mappings = [];
if ($name == 'ALL') {
if ($dir == 'pull') {
$mappings = $this->mappingStorage
->loadPullMappings();
}
elseif ($dir == 'push') {
$mappings = $this->mappingStorage
->loadPushMappings();
}
else {
$mappings = $this->mappingStorage
->loadMultiple();
}
}
else {
$mapping = $this->mappingStorage
->load($name);
if ($dir == 'push' && !$mapping
->doesPush()) {
throw new \Exception(dt("Mapping !name does not push.", [
'!name' => $name,
]));
}
elseif ($dir == 'pull' && !$mapping
->doesPull()) {
throw new \Exception(dt("Mapping !name does not pull.", [
'!name' => $name,
]));
}
$mappings = [
$mapping,
];
}
$mappings = array_filter($mappings);
if (empty($mappings)) {
if ($dir == 'push') {
throw new \Exception(dt('No push mappings loaded'));
}
if ($dir == 'pull') {
throw new \Exception(dt('No pull mappings loaded'));
}
}
return $mappings;
}
protected function getPushMappingsFromName($name) {
return $this
->getMappingsFromName($name, 'push');
}
protected function getPullMappingsFromName($name) {
return $this
->getMappingsFromName($name, 'pull');
}
protected function returnQueryResult(QueryResult $query) {
$formatter = new QueryResultTableFormatter();
$formatterManager = Drush::getContainer()
->get('formatterManager');
$formatterManager
->addFormatter('table', $formatter);
return $query;
}
}