Skip to content
Snippets Groups Projects
Commit 21e69b44 authored by Colin Corrigan's avatar Colin Corrigan Committed by Aaron Bauman
Browse files

Issue #3018851 by cwcorrigan: Move mapping-related methods out of salesforce...

Issue #3018851 by cwcorrigan: Move mapping-related methods out of salesforce core module, and into salesforce_mapping module
parent 5dd5bc46
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,6 @@ namespace Drupal\salesforce_mapping\Commands; ...@@ -5,7 +5,6 @@ namespace Drupal\salesforce_mapping\Commands;
use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\salesforce\Commands\SalesforceCommandsBase;
use Drupal\salesforce\Rest\RestClient; use Drupal\salesforce\Rest\RestClient;
use Drupal\salesforce\SelectQuery; use Drupal\salesforce\SelectQuery;
use Drush\Exceptions\UserAbortException; use Drush\Exceptions\UserAbortException;
...@@ -23,7 +22,7 @@ use Symfony\Component\Console\Output\Output; ...@@ -23,7 +22,7 @@ use Symfony\Component\Console\Output\Output;
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php * - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml * - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/ */
class SalesforceMappingCommands extends SalesforceCommandsBase { class SalesforceMappingCommands extends SalesforceMappingCommandsBase {
protected $salesforceConfig; protected $salesforceConfig;
protected $database; protected $database;
......
<?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;
/**
* Shared command base for Salesforce Drush commands.
*/
abstract class SalesforceMappingCommandsBase extends SalesforceCommandsBase {
/**
* Salesforce Mapping storage handler.
*
* @var \Drupal\salesforce_mapping\SalesforceMappingStorage
*/
protected $mappingStorage;
/**
* Mapped Object storage handler.
*
* @var \Drupal\salesforce_mapping\MappedObjectStorage
*/
protected $mappedObjectStorage;
/**
* SalesforceMappingCommandsBase constructor.
*
* @param \Drupal\salesforce\Rest\RestClient $client
* SF client.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $etm
* Entity type manager.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(RestClient $client, EntityTypeManagerInterface $etm) {
parent::__construct($client, $etm);
$this->mappingStorage = $etm->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $etm->getStorage('salesforce_mapped_object');
}
/**
* Collect a salesforce mapping interactively.
*/
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;
}
/** @var \Drupal\salesforce_mapping\Entity\SalesforceMapping $mapping */
$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);
}
/**
* Collect a salesforce mapping name, and set it to a "name" argument.
*/
protected function interactPushMappings(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE) {
return $this->interactMapping($input, $output, $message, $allOption, 'push');
}
/**
* Collect a salesforce mapping name, and set it to a "name" argument.
*/
protected function interactPullMappings(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE) {
return $this->interactMapping($input, $output, $message, $allOption, 'pull');
}
/**
* Helper method to collect the choice from user, given a set of options.
*/
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);
}
/**
* Given a mapping name (and optional direction), get an array of mappings.
*
* @param string $name
* 'ALL' to load all mappings, or a mapping id.
* @param string $dir
* 'push'|'pull'|NULL to load limit mappings by push or pull types.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface[]
* The mappings.
*/
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;
}
/**
* Given a mapping name, get an array of matching push mappings.
*
* @param string $name
* The mapping name.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface[]
* The matching mappings.
*
* @throws \Exception
*/
protected function getPushMappingsFromName($name) {
return $this->getMappingsFromName($name, 'push');
}
/**
* Given a mappin gname, get an array of matching pull mappings.
*
* @param string $name
* The mapping name.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface[]
* The pull mappings.
*
* @throws \Exception
*/
protected function getPullMappingsFromName($name) {
return $this->getMappingsFromName($name, 'pull');
}
/**
* Pass-through helper to add appropriate formatters for a query result.
*
* @param \Drupal\salesforce\Commands\QueryResult $query
* The query result.
*
* @return \Drupal\salesforce\Commands\QueryResult
* The same, unchanged query result.
*/
protected function returnQueryResult(QueryResult $query) {
$formatter = new QueryResultTableFormatter();
$formatterManager = Drush::getContainer()->get('formatterManager');
$formatterManager->addFormatter('table', $formatter);
return $query;
}
}
...@@ -4,7 +4,7 @@ namespace Drupal\salesforce_pull\Commands; ...@@ -4,7 +4,7 @@ namespace Drupal\salesforce_pull\Commands;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\salesforce\Commands\SalesforceCommandsBase; use Drupal\salesforce_mapping\Commands\SalesforceMappingCommandsBase;
use Drupal\salesforce\Event\SalesforceEvents; use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce\Rest\RestClient; use Drupal\salesforce\Rest\RestClient;
use Drupal\salesforce\SFID; use Drupal\salesforce\SFID;
...@@ -24,7 +24,7 @@ use Symfony\Component\Console\Output\Output; ...@@ -24,7 +24,7 @@ use Symfony\Component\Console\Output\Output;
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php * - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml * - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/ */
class SalesforcePullCommands extends SalesforceCommandsBase { class SalesforcePullCommands extends SalesforceMappingCommandsBase {
/** /**
* Pull queue handler service. * Pull queue handler service.
......
...@@ -4,7 +4,7 @@ namespace Drupal\salesforce_push\Commands; ...@@ -4,7 +4,7 @@ namespace Drupal\salesforce_push\Commands;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\salesforce\Commands\SalesforceCommandsBase; use Drupal\salesforce_mapping\Commands\SalesforceMappingCommandsBase;
use Drupal\salesforce\Rest\RestClient; use Drupal\salesforce\Rest\RestClient;
use Drupal\salesforce_push\PushQueue; use Drupal\salesforce_push\PushQueue;
use Symfony\Component\Console\Input\Input; use Symfony\Component\Console\Input\Input;
...@@ -21,7 +21,7 @@ use Symfony\Component\Console\Output\Output; ...@@ -21,7 +21,7 @@ use Symfony\Component\Console\Output\Output;
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php * - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml * - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/ */
class SalesforcePushCommands extends SalesforceCommandsBase { class SalesforcePushCommands extends SalesforceMappingCommandsBase {
/** /**
* Database service. * Database service.
......
...@@ -595,7 +595,7 @@ class SalesforceCommands extends SalesforceCommandsBase { ...@@ -595,7 +595,7 @@ class SalesforceCommands extends SalesforceCommandsBase {
* @return \Drupal\salesforce\Commands\QueryResult * @return \Drupal\salesforce\Commands\QueryResult
* The query result. * The query result.
* *
* @command salesforcef:query-object * @command salesforce:query-object
* @aliases sfqo,sf-query-object * @aliases sfqo,sf-query-object
*/ */
public function queryObject($object, array $options = [ public function queryObject($object, array $options = [
......
...@@ -29,20 +29,6 @@ abstract class SalesforceCommandsBase extends DrushCommands { ...@@ -29,20 +29,6 @@ abstract class SalesforceCommandsBase extends DrushCommands {
*/ */
protected $etm; protected $etm;
/**
* Salesforce Mapping storage handler.
*
* @var \Drupal\salesforce_mapping\SalesforceMappingStorage
*/
protected $mappingStorage;
/**
* Mapped Object storage handler.
*
* @var \Drupal\salesforce_mapping\MappedObjectStorage
*/
protected $mappedObjectStorage;
/** /**
* SalesforceCommandsBase constructor. * SalesforceCommandsBase constructor.
* *
...@@ -57,8 +43,6 @@ abstract class SalesforceCommandsBase extends DrushCommands { ...@@ -57,8 +43,6 @@ abstract class SalesforceCommandsBase extends DrushCommands {
public function __construct(RestClient $client, EntityTypeManagerInterface $etm) { public function __construct(RestClient $client, EntityTypeManagerInterface $etm) {
$this->client = $client; $this->client = $client;
$this->etm = $etm; $this->etm = $etm;
$this->mappingStorage = $etm->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $etm->getStorage('salesforce_mapped_object');
} }
/** /**
...@@ -77,142 +61,6 @@ abstract class SalesforceCommandsBase extends DrushCommands { ...@@ -77,142 +61,6 @@ abstract class SalesforceCommandsBase extends DrushCommands {
} }
} }
/**
* Collect a salesforce mapping interactively.
*/
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;
}
/** @var \Drupal\salesforce_mapping\Entity\SalesforceMapping $mapping */
$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);
}
/**
* Collect a salesforce mapping name, and set it to a "name" argument.
*/
protected function interactPushMappings(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE) {
return $this->interactMapping($input, $output, $message, $allOption, 'push');
}
/**
* Collect a salesforce mapping name, and set it to a "name" argument.
*/
protected function interactPullMappings(Input $input, Output $output, $message = 'Choose a Salesforce mapping', $allOption = FALSE) {
return $this->interactMapping($input, $output, $message, $allOption, 'pull');
}
/**
* Helper method to collect the choice from user, given a set of options.
*/
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);
}
/**
* Given a mapping name (and optional direction), get an array of mappings.
*
* @param string $name
* 'ALL' to load all mappings, or a mapping id.
* @param string $dir
* 'push'|'pull'|NULL to load limit mappings by push or pull types.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface[]
* The mappings.
*/
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)) {
throw new \Exception(dt('No push mappings loaded'));
}
return $mappings;
}
/**
* Given a mapping name, get an array of matching push mappings.
*
* @param string $name
* The mapping name.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMapping[]
* The matching mappings.
*
* @throws \Exception
*/
protected function getPushMappingsFromName($name) {
return $this->getMappingsFromName($name, 'push');
}
/**
* Given a mappin gname, get an array of matching pull mappings.
*
* @param string $name
* The mapping name.
*
* @return \Drupal\salesforce_mapping\Entity\SalesforceMapping[]
* The pull mappings.
*
* @throws \Exception
*/
protected function getPullMappingsFromName($name) {
return $this->getMappingsFromName($name, 'pull');
}
/** /**
* Pass-through helper to add appropriate formatters for a query result. * Pass-through helper to add appropriate formatters for a query result.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment