From 6ba5fc39cc22ad1f084b260c5978a1f32b76e70d Mon Sep 17 00:00:00 2001
From: Aaron Bauman <aaron@messageagency.com>
Date: Tue, 28 Mar 2017 16:14:14 -0400
Subject: [PATCH] Converting Queue and Delete Handlers into services,
 eliminating ::create methods, and updated tests.

---
 .../salesforce_pull/salesforce_pull.module    | 21 +---------
 .../salesforce_pull.services.yml              |  6 +--
 modules/salesforce_pull/src/DeleteHandler.php | 26 ++----------
 modules/salesforce_pull/src/QueueHandler.php  | 42 ++++++-------------
 .../tests/src/Unit/DeleteHandlerTest.php      | 10 ++---
 .../tests/src/Unit/QueueHandlerTest.php       | 35 ++++++++++++++--
 6 files changed, 56 insertions(+), 84 deletions(-)

diff --git a/modules/salesforce_pull/salesforce_pull.module b/modules/salesforce_pull/salesforce_pull.module
index 151012a6..18772426 100644
--- a/modules/salesforce_pull/salesforce_pull.module
+++ b/modules/salesforce_pull/salesforce_pull.module
@@ -5,8 +5,6 @@
  * Pull updates from Salesforce when a Salesforce object is updated.
  */
 
-use Drupal\salesforce_pull\QueueHandler;
-use Drupal\salesforce_pull\DeleteHandler;
 
 /**
  * Implements hook_cron().
@@ -14,22 +12,7 @@ use Drupal\salesforce_pull\DeleteHandler;
 function salesforce_pull_cron() {
   $sfapi = \Drupal::service('salesforce.client');
   if ($sfapi->isAuthorized()) {
-    QueueHandler::create(
-      $sfapi,
-      \Drupal::service('entity.manager')
-        ->getStorage('salesforce_mapping')
-        ->loadMultiple(),
-      \Drupal::queue('cron_salesforce_pull'),
-      \Drupal::state(),
-      \Drupal::service('event_dispatcher'),
-      \Drupal::request()
-    )->getUpdatedRecords();
-    DeleteHandler::create(
-      $sfapi,
-      \Drupal::service('entity_type.manager'),
-      \Drupal::state(),
-      \Drupal::service('event_dispatcher'),
-      \Drupal::request()
-    )->processDeletedRecords();
+    \Drupal::service('salesforce_pull.queue_handler')->getUpdatedRecords();
+    \Drupal::service('salesforce_pull.delete_handler')->processDeletedRecords();
   }
 }
diff --git a/modules/salesforce_pull/salesforce_pull.services.yml b/modules/salesforce_pull/salesforce_pull.services.yml
index 52bac2c3..eb9abff4 100644
--- a/modules/salesforce_pull/salesforce_pull.services.yml
+++ b/modules/salesforce_pull/salesforce_pull.services.yml
@@ -1,10 +1,8 @@
 services:
   salesforce_pull.queue_handler:
     class: Drupal\salesforce_pull\QueueHandler
-    arguments: ['@salesforce.client', '@entity.manager', '@queue.database', '@state', '@event_dispatcher', '@request_stack']
+    arguments: ['@salesforce.client', '@entity_type.manager', '@queue.database', '@state', '@event_dispatcher', '@request_stack']
 
   salesforce_pull.delete_handler:
     class: Drupal\salesforce_pull\DeleteHandler
-    arguments: ['@entity_type.manager']
-    tags:
-      - { name: event_subscriber }
\ No newline at end of file
+    arguments: ['@salesforce.client', '@entity_type.manager', '@state', '@event_dispatcher', '@request_stack']
diff --git a/modules/salesforce_pull/src/DeleteHandler.php b/modules/salesforce_pull/src/DeleteHandler.php
index 9c33b894..f07ce9e8 100644
--- a/modules/salesforce_pull/src/DeleteHandler.php
+++ b/modules/salesforce_pull/src/DeleteHandler.php
@@ -11,7 +11,7 @@ use Drupal\salesforce\SFID;
 use Drupal\salesforce_mapping\MappedObjectStorage;
 use Drupal\salesforce_mapping\MappingConstants;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
 
 /**
  * Handles pull cron deletion of Drupal entities based onSF mapping settings.
@@ -73,35 +73,15 @@ class DeleteHandler {
    *   Entity Manager service.
    * @param \Drupal\Core\State\StateInterface $state
    *   State service.
-   * @param Psr\Log\LoggerInterface $logger
-   *   Logging service.
    */
-  private function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher, Request $request) {
+    public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) {
     $this->sfapi = $sfapi;
     $this->etm = $entity_type_manager;
     $this->mappingStorage = $this->etm->getStorage('salesforce_mapping');
     $this->mappedObjectStorage = $this->etm->getStorage('salesforce_mapped_object');
     $this->state = $state;
     $this->eventDispatcher = $event_dispatcher;
-    $this->request = $request;
-  }
-
-  /**
-   * Chainable instantiation method for class.
-   *
-   * @param \Drupal\salesforce\Rest\RestClientInterface $sfapi
-   *   RestClient object.
-   * @param \Drupal\Core\Entity\EntityTyprManagerInterface $entity_type_manager
-   *   Entity Manager service.
-   * @param \Drupal\Core\State\StatInterface $state
-   *   State service.
-   * @param EventDispatcherInterface $event_dispatcher
-   *   Event dispatcher service.
-   * @param Request $request
-   *   Request service.
-   */
-  public static function create(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher, Request $request) {
-    return new DeleteHandler($sfapi, $entity_type_manager, $state, $event_dispatcher, $request);
+    $this->request = $request_stack->getCurrentRequest();
   }
 
   /**
diff --git a/modules/salesforce_pull/src/QueueHandler.php b/modules/salesforce_pull/src/QueueHandler.php
index c76d4621..152a18bc 100644
--- a/modules/salesforce_pull/src/QueueHandler.php
+++ b/modules/salesforce_pull/src/QueueHandler.php
@@ -2,7 +2,8 @@
 
 namespace Drupal\salesforce_pull;
 
-use Drupal\Core\Queue\QueueInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Queue\QueueDatabaseFactory;
 use Drupal\Core\State\StateInterface;
 use Drupal\Core\Utility\Error;
 use Drupal\salesforce\Event\SalesforceErrorEvent;
@@ -14,7 +15,7 @@ use Drupal\salesforce\SelectQueryResult;
 use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
 use Drupal\salesforce_mapping\Event\SalesforceQueryEvent;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
 
 /**
  * Handles pull cron queue set up.
@@ -56,43 +57,26 @@ class QueueHandler {
   protected $pull_fields;
 
   /**
-   * @param RestClientInterface               $sfapi
-   * @param array                    $mappings
-   * @param QueueInterface           $queue
-   * @param StateInterface           $state
-   * @param LoggerInterface          $logger
+   * @param RestClientInterface $sfapi
+   * @param QueueInterface $queue
+   * @param StateInterface $state
    * @param EventDispatcherInterface $event_dispatcher
-   * @param Request                  $request
+   * @param RequestStack $request_stack
    */
 
-  public function __construct(RestClientInterface $sfapi, array $mappings, QueueInterface $queue, StateInterface $state, EventDispatcherInterface $event_dispatcher, Request $request) {
+  public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, QueueDatabaseFactory $queue_factory, StateInterface $state, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) {
     $this->sfapi = $sfapi;
-    $this->queue = $queue;
+    $this->queue = $queue_factory->get('cron_salesforce_pull');
     $this->state = $state;
     $this->eventDispatcher = $event_dispatcher;
-    $this->request = $request;
-    $this->mappings = $mappings;
+    $this->request = $request_stack->getCurrentRequest();
+    $this->mappings = $entity_type_manager
+      ->getStorage('salesforce_mapping')
+      ->loadMultiple();
     $this->pull_fields = [];
     $this->organizeMappings();
   }
 
-  /**
-   * Chainable instantiation method for class.
-   *
-   * @param  RestClientInterface               $sfapi
-   * @param  array                    $mappings
-   * @param  QueueInterface           $queue
-   * @param  StateInterface           $state
-   * @param  LoggerInterface          $logger
-   * @param  EventDispatcherInterface $event_dispatcher
-   * @param  Request                  $request
-   *
-   * @return QueueHandler
-   */
-  public static function create(RestClientInterface $sfapi, array $mappings, QueueInterface $queue, StateInterface $state, EventDispatcherInterface $event_dispatcher, Request $request) {
-    return new QueueHandler($sfapi, $mappings, $queue, $state, $event_dispatcher, $request);
-  }
-
   /**
    * Pull updated records from Salesforce and place them in the queue.
    *
diff --git a/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php b/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php
index 9fcbc0f1..3b418275 100644
--- a/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php
+++ b/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php
@@ -15,7 +15,7 @@ use Drupal\salesforce_pull\DeleteHandler;
 use Drupal\salesforce\Rest\RestClientInterface;
 use Drupal\Tests\UnitTestCase;
 use Prophecy\Argument;
-use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\HttpFoundation\ServerBag;
 
 /**
@@ -146,16 +146,16 @@ class DeleteHandlerTest extends UnitTestCase {
     $this->server = $prophecy->reveal();
 
     // Mock request.
-    $prophecy = $this->prophesize(Request::CLASS);
+    $prophecy = $this->prophesize(RequestStack::CLASS);
     $prophecy->server = $this->server;
-    $this->request = $prophecy->reveal();
+    $this->request_stack = $prophecy->reveal();
 
-    $this->dh = DeleteHandler::create(
+    $this->dh = new DeleteHandler(
       $this->sfapi,
       $this->etm,
       $this->state,
       $this->ed,
-      $this->request
+      $this->request_stack
     );
   }
 
diff --git a/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php b/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php
index 0c196799..a7a9e557 100644
--- a/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php
+++ b/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php
@@ -2,15 +2,19 @@
 namespace Drupal\Tests\salesforce_pull\Unit;
 
 use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Queue\QueueDatabaseFactory;
 use Drupal\Core\Queue\QueueInterface;
 use Drupal\Core\State\StateInterface;
 use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
+use Drupal\salesforce_mapping\SalesforceMappingStorage;
 use Drupal\salesforce_pull\QueueHandler;
 use Drupal\salesforce\Rest\RestClientInterface;
 use Drupal\salesforce\SelectQueryResult;
 use Drupal\Tests\UnitTestCase;
 use Prophecy\Argument;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\HttpFoundation\ServerBag;
 
 /**
@@ -62,6 +66,21 @@ class QueueHandlerTest extends UnitTestCase {
     $prophecy->numberOfItems()->willReturn(2);
     $this->queue = $prophecy->reveal();
 
+    $prophecy = $this->prophesize(QueueDatabaseFactory::CLASS);
+    $prophecy->get(Argument::any())->willReturn($this->queue);
+    $this->queue_factory = $prophecy->reveal();
+
+    // Mock mapping ConfigEntityStorage object.
+    $prophecy = $this->prophesize(SalesforceMappingStorage::CLASS);
+    $prophecy->loadMultiple(Argument::any())->willReturn([$this->mapping]);
+    $this->mappingStorage = $prophecy->reveal();
+
+    // Mock EntityTypeManagerInterface.
+    $prophecy = $this->prophesize(EntityTypeManagerInterface::CLASS);
+    $prophecy->getStorage('salesforce_mapping')->willReturn($this->mappingStorage);
+    $this->etm = $prophecy->reveal();
+
+
     // mock state
     $prophecy = $this->prophesize(StateInterface::CLASS);
     $prophecy->get('salesforce_pull_last_sync_default', Argument::any())->willReturn('1485787434');
@@ -80,13 +99,17 @@ class QueueHandlerTest extends UnitTestCase {
     $this->server = $prophecy->reveal();
 
     // mock request
-    $prophecy = $this->prophesize(Request::CLASS);
+    $request = $this->prophesize(Request::CLASS);
+
+    // mock request stack
+    $prophecy = $this->prophesize(RequestStack::CLASS);
     $prophecy->server = $this->server;
-    $this->request = $prophecy->reveal();
+    $prophecy->getCurrentRequest()->willReturn($request->reveal());
+    $this->request_stack = $prophecy->reveal();
 
     $this->qh = $this->getMockBuilder(QueueHandler::CLASS)
       ->setMethods(['parseUrl'])
-      ->setConstructorArgs([$this->sfapi, [$this->mapping], $this->queue, $this->state, $this->ed, $this->request])
+      ->setConstructorArgs([$this->sfapi, $this->etm, $this->queue_factory, $this->state, $this->ed, $this->request_stack])
       ->getMock();
     $this->qh->expects($this->any())
       ->method('parseUrl')
@@ -118,9 +141,13 @@ class QueueHandlerTest extends UnitTestCase {
     $prophecy->numberOfItems()->willReturn(100001);
     $this->queue = $prophecy->reveal();
 
+    $prophecy = $this->prophesize(QueueDatabaseFactory::CLASS);
+    $prophecy->get(Argument::any())->willReturn($this->queue);
+    $this->queue_factory = $prophecy->reveal();
+
     $this->qh = $this->getMockBuilder(QueueHandler::CLASS)
       ->setMethods(['parseUrl'])
-      ->setConstructorArgs([$this->sfapi, [$this->mapping], $this->queue, $this->state, $this->ed, $this->request])
+      ->setConstructorArgs([$this->sfapi, $this->etm, $this->queue_factory, $this->state, $this->ed, $this->request_stack])
       ->getMock();
     $this->qh->expects($this->any())
       ->method('parseUrl')
-- 
GitLab