You are here

class SalesforcePushCommands in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/salesforce_push/src/Commands/SalesforcePushCommands.php \Drupal\salesforce_push\Commands\SalesforcePushCommands
  2. 5.0.x modules/salesforce_push/src/Commands/SalesforcePushCommands.php \Drupal\salesforce_push\Commands\SalesforcePushCommands

A Drush commandfile.

In addition to this file, you need a drush.services.yml in root of your module, and a composer.json file that provides the name of the services file to use.

See these files for an example of injecting Drupal services:

Hierarchy

Expanded class hierarchy of SalesforcePushCommands

1 string reference to 'SalesforcePushCommands'
drush.services.yml in modules/salesforce_push/drush.services.yml
modules/salesforce_push/drush.services.yml
1 service uses SalesforcePushCommands
salesforce_push.commands in modules/salesforce_push/drush.services.yml
\Drupal\salesforce_push\Commands\SalesforcePushCommands

File

modules/salesforce_push/src/Commands/SalesforcePushCommands.php, line 24

Namespace

Drupal\salesforce_push\Commands
View source
class SalesforcePushCommands extends SalesforceMappingCommandsBase {

  /**
   * Database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Push queue service.
   *
   * @var \Drupal\salesforce_push\PushQueue
   */
  protected $pushQueue;

  /**
   * SalesforcePushCommands constructor.
   *
   * @param \Drupal\salesforce\Rest\RestClient $client
   *   Salesforce service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $etm
   *   ETM service.
   * @param \Drupal\salesforce_push\PushQueue $pushQueue
   *   Push queue service.
   * @param \Drupal\Core\Database\Connection $database
   *   Database service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(RestClient $client, EntityTypeManagerInterface $etm, PushQueue $pushQueue, Connection $database) {
    parent::__construct($client, $etm);
    $this->pushQueue = $pushQueue;
    $this->database = $database;
  }

  /**
   * Collect a mapping interactively.
   *
   * @hook interact salesforce_push:push-queue
   */
  public function interactPushQueue(Input $input, Output $output) {
    return $this
      ->interactPushMappings($input, $output, 'Choose a Salesforce mapping', 'Push All');
  }

  /**
   * Collect a mapping interactively.
   *
   * @hook interact salesforce_push:push-unmapped
   */
  public function interactPushUnmapped(Input $input, Output $output) {
    return $this
      ->interactPushMappings($input, $output, 'Choose a Salesforce mapping', 'Push All');
  }

  /**
   * Process push queues for one or all Salesforce Mappings.
   *
   * @param string $name
   *   Mapping name.
   *
   * @usage drush sfpushq
   *   Process all push queue items.
   * @usage drush sfpushq foo
   *   Process push queue items for mapping "foo".
   *
   * @command salesforce_push:push-queue
   * @aliases sfpushq,sfpm,sf-push-queue,salesforce_push:queue
   */
  public function pushQueue($name) {
    $mappings = $this
      ->getPushMappingsFromName($name);
    foreach ($mappings as $mapping) {

      // Process one mapping queue.
      $this->pushQueue
        ->processQueue($mapping);
      $this
        ->logger()
        ->info(dt('Finished pushing !name', [
        '!name' => $mapping
          ->label(),
      ]));
    }
  }

  /**
   * Push entities of a mapped type that are not linked to Salesforce Objects.
   *
   * @param string $name
   *   The Drupal machine name of the mapping for the entities.
   * @param array $options
   *   An associative array of options whose values come from cli, aliases,
   *   config, etc.
   *
   * @option count
   *   The number of entities to try to sync. (Default is 50).
   * @usage drush sfpu foo
   *   Push 50 drupal entities without mapped objects for mapping "foo"
   * @usage drush sfpu foo --count=42
   *   Push 42 unmapped drupal entities without mapped objects for mapping "foo"
   *
   * @command salesforce_push:push-unmapped
   * @aliases sfpu,salesforce-push-unmapped,salesforce_push:unmapped
   */
  public function pushUnmapped($name, array $options = [
    'count' => NULL,
  ]) {
    $mappings = $this
      ->getPushMappingsFromName($name);
    foreach ($mappings as $mapping) {
      $entity_type = $mapping
        ->get('drupal_entity_type');
      $entity_storage = $this->etm
        ->getStorage($entity_type);
      $entity_keys = $this->etm
        ->getDefinition($entity_type)
        ->getKeys();
      $id_key = $entity_keys['id'];
      $bundle_key = empty($entity_keys['bundle']) ? FALSE : $entity_keys['bundle'];
      $query = $this->database
        ->select($entity_storage
        ->getBaseTable(), 'b');
      $query
        ->leftJoin('salesforce_mapped_object', 'm', "b.{$id_key} = m.drupal_entity__target_id AND m.drupal_entity__target_type = '{$entity_type}'");
      if ($bundle_key) {
        $query
          ->condition("b.{$bundle_key}", $mapping
          ->get('drupal_bundle'));
      }
      $query
        ->fields('b', [
        $id_key,
      ]);
      $query
        ->isNull('m.drupal_entity__target_id');
      $results = $query
        ->range(0, drush_get_option('count', 50))
        ->execute()
        ->fetchAllAssoc($id_key);
      $entities = $entity_storage
        ->loadMultiple(array_keys($results));
      $log = [];
      print_r($entities);
      foreach ($entities as $entity) {
        salesforce_push_entity_crud($entity, 'push_create');
        $log[] = $entity
          ->id();
      }
      $this->logger
        ->info(dt("!mapping: !count unmapped entities found and push to Salesforce attempted. See logs for more details.", [
        '!count' => count($log),
        '!mapping' => $mapping
          ->label(),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SalesforceCommandsBase::$client protected property The Salesforce client.
SalesforceCommandsBase::$etm protected property The entity type manager.
SalesforceCommandsBase::interactObject protected function Collect a salesforce object name, and set it to "object" argument.
SalesforceMappingCommandsBase::$mappedObjectStorage protected property Mapped Object storage handler.
SalesforceMappingCommandsBase::$mappingStorage protected property Salesforce Mapping storage handler.
SalesforceMappingCommandsBase::doMappingNameOptions protected function Helper method to collect the choice from user, given a set of options.
SalesforceMappingCommandsBase::getMappingsFromName protected function Given a mapping name (and optional direction), get an array of mappings.
SalesforceMappingCommandsBase::getPullMappingsFromName protected function Given a mappin gname, get an array of matching pull mappings.
SalesforceMappingCommandsBase::getPushMappingsFromName protected function Given a mapping name, get an array of matching push mappings.
SalesforceMappingCommandsBase::interactMapping protected function Collect a salesforce mapping interactively.
SalesforceMappingCommandsBase::interactPullMappings protected function Collect a salesforce mapping name, and set it to a "name" argument.
SalesforceMappingCommandsBase::interactPushMappings protected function Collect a salesforce mapping name, and set it to a "name" argument.
SalesforceMappingCommandsBase::returnQueryResult protected function Pass-through helper to add appropriate formatters for a query result. Overrides SalesforceCommandsBase::returnQueryResult
SalesforcePushCommands::$database protected property Database service.
SalesforcePushCommands::$pushQueue protected property Push queue service.
SalesforcePushCommands::interactPushQueue public function Collect a mapping interactively.
SalesforcePushCommands::interactPushUnmapped public function Collect a mapping interactively.
SalesforcePushCommands::pushQueue public function Process push queues for one or all Salesforce Mappings.
SalesforcePushCommands::pushUnmapped public function Push entities of a mapped type that are not linked to Salesforce Objects.
SalesforcePushCommands::__construct public function SalesforcePushCommands constructor. Overrides SalesforceMappingCommandsBase::__construct