Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Adding drush 9 commands and removing drush 8 ones. #123

Merged
merged 3 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions modules/acm_customer/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
acm_customer.commands:
class: \Drupal\acm_customer\Commands\AcmCustomerDrushCommands
arguments:
- '@database'
- '@logger.factory'
tags:
- { name: drush.command }
188 changes: 188 additions & 0 deletions modules/acm_customer/src/Commands/AcmCustomerDrushCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace Drupal\acm_customer\Commands;

use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;

/**
* Class AcmCustomerDrushCommands.
*
* @package Drupal\acm_customer\Commands
*/
class AcmCustomerDrushCommands extends DrushCommands {

const BATCH_SIZE = 20;

/**
* Database Connection.
*
* @var \Drupal\Core\Database\Connection
*/
private $connection;

/**
* AcmCustomerDrushCommands constructor.
*
* @param \Drupal\Core\Database\Connection $connection
* Database Connection.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* Logger Channel Factory.
*/
public function __construct(Connection $connection,
LoggerChannelFactoryInterface $logger_factory) {
$this->connection = $connection;
$this->setLogger($logger_factory->get('AcmCustomerDrushCommands'));
}

/**
* Push all the customers available in Drupal to upstream system.
*
* @command acm_customer:push-customers
*
* @validate-module-enabled acm_customer
*
* @aliases push-customers
*
* @usage drush push-customers
* Push all the customers available in Drupal to upstream system.
*/
public function pushCustomersData() {
$query = $this->connection->select('users_field_data', 'user');
$query->addField('user', 'mail');
$query->condition('status', 0, '>');
$query->condition('acm_customer_id', 0, '>');
$result = $query->countQuery()->execute()->fetchAssoc();

if ($result['expression'] == 0) {
$this->logger->warning('No customers with active status available to push.');
return;
}

$question = dt('There are @count customers as of now, are you sure you want to push for all?', [
'@count' => $result['expression'],
]);

if (!$this->confirm($question)) {
throw new UserAbortException();
}

$query = $this->connection->select('users_field_data', 'user');
$query->addField('user', 'mail');
$query->condition('status', 0, '>');
$query->condition('acm_customer_id', 0, '>');
$result = $query->execute()->fetchAll(\PDO::FETCH_ASSOC);

$customers = array_column($result, 'mail');

$batch = [
'title' => 'Push Customers Data',
'init_message' => 'Pushing customers data...',
'error_message' => 'Error occurred while pushing customers, please check logs.',
];

foreach (array_chunk($customers, self::BATCH_SIZE) as $customers_batch) {
$batch['operations'][] = [
[__CLASS__, 'pushCustomerDataInBatch'],
[$customers_batch],
];
}

batch_set($batch);
drush_backend_batch_process();

$this->logger()->info('Finished pushing customers data, please check logs for more details.');
}

/**
* Push specific customer available in Drupal to upstream system.
*
* @param string $mail
* E-Mail address of the customer to push.
*
* @command acm_customer:push-customer
*
* @validate-module-enabled acm_customer
*
* @aliases push-customer
*
* @usage drush push-customer abc@xyz.com
* Push specific customer available in Drupal to upstream system.
*/
public function pushCustomerData(string $mail) {
self::pushCustomerDataToUpstream($mail);
$this->logger()->info('Pushed customer data for @mail, please check logs for more details.', [
'@mail' => $mail,
]);
}

/**
* Batch callback to push customers data in batches.
*
* @param array $customers
* Array containing customer mails to push.
* @param mixed $context
* Batch context.
*/
public static function pushCustomerDataInBatch(array $customers, &$context) {
foreach ($customers as $customer) {
self::pushCustomerDataToUpstream($customer);
}
}

/**
* Wrapper function to push data for particular customer.
*
* @param string $mail
* E-Mail address of the customer to push.
*/
public static function pushCustomerDataToUpstream(string $mail) {
$logger = \Drupal::logger('AcmCustomerDrushCommands');

/** @var \Drupal\user\Entity\User $user */
$user = user_load_by_mail($mail);

if (empty($user)) {
$logger->warning('User with @mail not found, skipping.', [
'@mail' => $mail,
]);
}
elseif (empty($user->get('acm_customer_id')->getString())) {
$logger->warning('User with @mail does not have customer id, skipping.', [
'@mail' => $mail,
]);
}

$customer_array = [
'customer_id' => $user->get('acm_customer_id')->getString(),
'firstname' => $user->get('field_first_name')->getString(),
'lastname' => $user->get('field_last_name')->getString(),
'email' => $user->getEmail(),
];

/** @var \Drupal\acm\Connector\APIWrapper $api_wrapper */
$api_wrapper = \Drupal::service('acm.api');

try {
if ($api_wrapper->updateCustomer($customer_array)) {
$logger->info('Successfully pushed for user with @mail.', [
'@mail' => $mail,
]);
}
else {
$logger->warning('Something went wrong while pushing user with @mail.', [
'@mail' => $mail,
]);
}
}
catch (\Exception $e) {
$logger->error('Failed to push for user with @mail, message: @message.', [
'@mail' => $mail,
'@message' => $e->getMessage(),
]);
}
}

}
47 changes: 0 additions & 47 deletions modules/acm_promotion/acm_promotion.drush.inc

This file was deleted.

6 changes: 6 additions & 0 deletions modules/acm_promotion/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
acm_promotion.commands:
class: \Drupal\acm_promotion\Commands\AcmPromotionCommands
arguments: [ '@acm_promotion.promotions_manager', '@logger.factory' ]
tags:
- { name: drush.command }
71 changes: 71 additions & 0 deletions modules/acm_promotion/src/Commands/AcmPromotionCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Drupal\acm_promotion\Commands;

use Drupal\acm_promotion\AcmPromotionsManager;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drush\Commands\DrushCommands;

/**
* Class AcmPromotionCommands.
*
* @package Drupal\acm_promotion\Commands
*/
class AcmPromotionCommands extends DrushCommands {

/**
* ACM Promotions Manager.
*
* @var \Drupal\acm_promotion\AcmPromotionsManager
*/
private $acmPromotionsManager;

/**
* AcmPromotionCommands constructor.
*
* @param \Drupal\acm_promotion\AcmPromotionsManager $acmPromotionsManager
* ACM Promotion Manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerChannelFactory
* Logger Factory.
*/
public function __construct(AcmPromotionsManager $acmPromotionsManager,
LoggerChannelFactoryInterface $loggerChannelFactory) {
$this->acmPromotionsManager = $acmPromotionsManager;
$this->logger = $loggerChannelFactory->get('acm_promotion');
}

/**
* Run a full synchronization of all commerce promotion records.
*
* @param array $options
* Command Options.
*
* @command acm_promotion:sync-promotions
*
* @option types Type of promotions that need to be synced.
*
* @validate-module-enabled acm_promotion
*
* @aliases acspm,sync-commerce-promotions
*
* @usage drush acspm
* Run a full synchronization of all available promotions.
* @usage drush acspm --types=cart
* Run a full synchronization of all available cart promotions.
*/
public function synPromotions(array $options = ['types' => NULL]) {
if ($types = $options['types']) {
$this->logger->notice(dt('Synchronizing all @types commerce promotions, this usually takes some time...', ['@types' => $types]));
$types = explode(',', $types);
$types = array_map('trim', $types);
$this->acmPromotionsManager->syncPromotions($types);
}
else {
$this->logger->notice(dt('Synchronizing all commerce promotions, this usually takes some time...'));
$this->acmPromotionsManager->syncPromotions();
}

$this->logger->notice(dt('Promotion sync completed.'));
}

}
Loading