From fc3aa7826b7c946164446d9252319de6ab3d1e48 Mon Sep 17 00:00:00 2001
From: git <git@787980.no-reply.drupal.org>
Date: Mon, 13 Feb 2017 12:21:22 -0500
Subject: [PATCH] Removes all uses of watchdog_exception in favor of injected
 logger use

Fixes $this use error in RecordType::isAllowed static function
---
 .../src/Entity/MappedObject.php               | 16 +++++---
 .../src/Form/MappedObjectForm.php             | 25 ++++++++++--
 .../SalesforceMappingField/RecordType.php     |  4 +-
 .../RelatedProperties.php                     | 17 +++++++--
 .../tests/src/Unit/MappedObjectTest.php       | 24 +++++++-----
 .../salesforce_push/salesforce_push.module    | 38 +++++++++++++++----
 modules/salesforce_push/src/PushQueue.php     | 27 +++++++++++--
 src/Form/AuthorizeForm.php                    | 27 ++++++++++---
 8 files changed, 138 insertions(+), 40 deletions(-)

diff --git a/modules/salesforce_mapping/src/Entity/MappedObject.php b/modules/salesforce_mapping/src/Entity/MappedObject.php
index d03c5e36..6c1c0622 100644
--- a/modules/salesforce_mapping/src/Entity/MappedObject.php
+++ b/modules/salesforce_mapping/src/Entity/MappedObject.php
@@ -9,15 +9,17 @@ use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\RevisionableContentEntityBase;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Language\LanguageInterface;
-use Drupal\salesforce\Exception as SalesforceException;
-use Drupal\salesforce\SFID;
-use Drupal\salesforce\SObject;
-use Drupal\salesforce\SalesforceEvents;
+use Drupal\Core\Utility\Error;
 use Drupal\salesforce_mapping\MappingConstants;
 use Drupal\salesforce_mapping\PushParams;
 use Drupal\salesforce_mapping\SalesforcePullEntityValueEvent;
 use Drupal\salesforce_mapping\SalesforcePullEvent;
 use Drupal\salesforce_mapping\SalesforcePushEvent;
+use Drupal\salesforce\Exception as SalesforceException;
+use Drupal\salesforce\SalesforceEvents;
+use Drupal\salesforce\SFID;
+use Drupal\salesforce\SObject;
+use Psr\Log\LogLevel;
 
 /**
  * Defines a Salesforce Mapped Object entity class. Mapped Objects are content
@@ -450,7 +452,11 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject
           '@v' => $value,
           '@e' => $e->getMessage(),
         ]);
-        watchdog_exception(__CLASS__, $e);
+        $this->logger(__CLASS__)->log(
+          LogLevel::ERROR,
+          '%type: @message in %function (line %line of %file).',
+          Error::decodeException($e)
+        );
         continue;
       }
     }
diff --git a/modules/salesforce_mapping/src/Form/MappedObjectForm.php b/modules/salesforce_mapping/src/Form/MappedObjectForm.php
index 137748b7..00b2c0f5 100644
--- a/modules/salesforce_mapping/src/Form/MappedObjectForm.php
+++ b/modules/salesforce_mapping/src/Form/MappedObjectForm.php
@@ -5,12 +5,16 @@ namespace Drupal\salesforce_mapping\Form;
 use Drupal\Core\Entity\ContentEntityForm;
 use Drupal\Core\Entity\EntityManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
 use Drupal\Core\Routing\RouteMatchInterface;
+use Drupal\Core\Utility\Error;
+use Drupal\salesforce_mapping\SalesforceMappingStorage;
 use Drupal\salesforce\Exception;
 use Drupal\salesforce\Rest\RestClient;
-use Drupal\salesforce_mapping\SalesforceMappingStorage;
+use Psr\Log\LogLevel;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
+
 /**
  * Salesforce Mapping Form base.
  */
@@ -28,16 +32,26 @@ class MappedObjectForm extends ContentEntityForm {
   protected $pushPluginManager;
 
   protected $mapping_storage;
+
+  protected $logger;
+
+  protected $entityManager;
+
+  protected $rest;
+
+  protected $route_match;
+
   /**
    * Constructs a ContentEntityForm object.
    *
    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
    *   The entity manager.
    */
-  public function __construct(EntityManagerInterface $entity_manager, RestClient $rest, RouteMatchInterface $route_match) {
+  public function __construct(EntityManagerInterface $entity_manager, RestClient $rest, LoggerChannelFactoryInterface $logger_factory, RouteMatchInterface $route_match) {
     $this->entityManager = $entity_manager;
     $this->mapping_storage = $entity_manager->getStorage('salesforce_mapping');
     $this->rest = $rest;
+    $this->logger = $logger_factory->get(__CLASS__);
     $this->route_match = $route_match;
   }
 
@@ -48,6 +62,7 @@ class MappedObjectForm extends ContentEntityForm {
     return new static(
       $container->get('entity.manager'),
       $container->get('salesforce.client'),
+      $container->get('logger.factory'),
       $container->get('current_route_match')
     );
   }
@@ -116,7 +131,11 @@ class MappedObjectForm extends ContentEntityForm {
       $mapped_object->push();
     }
     catch (\Exception $e) {
-      watchdog_exception(__CLASS__, $e);
+      $this->logger->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
       drupal_set_message(t('Push failed with an exception: %exception', array('%exception' => $e->getMessage())), 'error');
       return;
     }
diff --git a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RecordType.php b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RecordType.php
index 44ce5f5e..868aa56d 100644
--- a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RecordType.php
+++ b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RecordType.php
@@ -75,7 +75,7 @@ class RecordType extends SalesforceMappingFieldPluginBase {
   public static function isAllowed(SalesforceMappingInterface $mapping) {
     try {
       $record_types =
-        $this->client()->getRecordTypes($mapping->getSalesforceObjectType());
+        self::client()->getRecordTypes($mapping->getSalesforceObjectType());
       return count($record_types) > 1;
     }
     catch (\Exception $e) {
@@ -86,7 +86,7 @@ class RecordType extends SalesforceMappingFieldPluginBase {
   /**
    * Wrapper for Salesforce client service.
    */
-  public function client() {
+  public static function client() {
     return \Drupal::service('salesforce.client');
   }
 
diff --git a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php
index 44f8e82d..97d998a2 100644
--- a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php
+++ b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php
@@ -4,9 +4,11 @@ namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
 
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Utility\Error;
 use Drupal\field\Field;
-use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
 use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
+use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
+use Psr\Log\LogLevel;
 
 /**
  * Adapter for entity Reference and fields.
@@ -81,8 +83,11 @@ $mapping) {
         ->load($field->value);
     }
     catch (\Exception $e) {
-      // @TODO something about this exception
-      watchdog_exception(__CLASS__, $e);
+      $this->logger(__CLASS__)->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
       return;
     }
 
@@ -163,4 +168,10 @@ $mapping) {
     return $options;
   }
 
+  /**
+   * Wrapper for Drupal core logger service.
+   */
+  public function logger($log) {
+    return \Drupal::logger($log);
+  }
 }
diff --git a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php
index 7d9e6676..b0baeb8b 100644
--- a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php
+++ b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php
@@ -7,10 +7,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Language\LanguageInterface;
-use Drupal\Tests\UnitTestCase;
-use Drupal\salesforce\Rest\RestClientInterface;
-use Drupal\salesforce\SFID;
-use Drupal\salesforce\SObject;
+use Drupal\Core\Utility\Error;
 use Drupal\salesforce_mapping\Entity\MappedObject;
 use Drupal\salesforce_mapping\Entity\MappedObjectInterface;
 use Drupal\salesforce_mapping\Entity\SalesforceMapping;
@@ -19,7 +16,12 @@ use Drupal\salesforce_mapping\MappingConstants;
 use Drupal\salesforce_mapping\Plugin\SalesforceMappingField\Properties;
 use Drupal\salesforce_mapping\SalesforceMappingFieldPluginInterface;
 use Drupal\salesforce_mapping\SalesforceMappingFieldPluginManager;
+use Drupal\salesforce\Rest\RestClientInterface;
+use Drupal\salesforce\SFID;
+use Drupal\salesforce\SObject;
+use Drupal\Tests\UnitTestCase;
 use Prophecy\Argument;
+use Psr\Log\LogLevel;
 
 /**
  * Test Mapped Object instantitation
@@ -126,7 +128,7 @@ class MappedObjectTest extends UnitTestCase {
       ->expects($this->any())
       ->method('id')
       ->willReturn($this->entity_id);
-  
+
     $this->entity
       ->expects($this->any())
       ->method('isTranslatable')
@@ -211,7 +213,7 @@ class MappedObjectTest extends UnitTestCase {
       ->willReturn(NULL);
     $this->assertNull($this->mapped_object->push());
   }
-  
+
   /**
    * @covers ::push
    */
@@ -320,7 +322,11 @@ class MappedObjectTest extends UnitTestCase {
             '@v' => $value,
             '@e' => $e->getMessage(),
           ]);
-          watchdog_exception(__CLASS__, $e);
+          $this->logger(__CLASS__)->log(
+            LogLevel::ERROR,
+            '%type: @message in %function (line %line of %file).',
+            Error::decodeException($e)
+          );
           continue;
         }
       }
@@ -345,7 +351,7 @@ class MappedObjectTest extends UnitTestCase {
         ->save();
 
       return $this;
-    
+
   }
 
-}
\ No newline at end of file
+}
diff --git a/modules/salesforce_push/salesforce_push.module b/modules/salesforce_push/salesforce_push.module
index 2c18b6a1..ed68708a 100644
--- a/modules/salesforce_push/salesforce_push.module
+++ b/modules/salesforce_push/salesforce_push.module
@@ -5,16 +5,18 @@
  * Push updates to Salesforce when a Drupal entity is updated.
  */
 
-use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Utility\Error;
 use Drupal\salesforce_mapping\Entity\MappedObject;
 use Drupal\salesforce_mapping\Entity\MappedObjectInterface;
 use Drupal\salesforce_mapping\Entity\SalesforceMapping;
 use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
-use GuzzleHttp\Exception\RequestException;
 use Drupal\salesforce_mapping\MappingConstants;
-use Drupal\salesforce\SalesforceEvents;
 use Drupal\salesforce_mapping\SalesforcePushEvent;
+use Drupal\salesforce\SalesforceEvents;
+use GuzzleHttp\Exception\RequestException;
+use Psr\Log\LogLevel;
 
 /**
  * Implements hook_entity_insert().
@@ -74,7 +76,11 @@ function salesforce_push_entity_crud(EntityInterface $entity, $op) {
     }
     catch (\Exception $e) {
       // Do not allow any exception to prevent entity CRUD.
-      watchdog_exception('Salesforce Push', $e);
+      \Drupal::logger('Salesforce Push')->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
     }
   }
 }
@@ -130,7 +136,11 @@ function salesforce_push_entity_crud_mapping(EntityInterface $entity, $op, Sales
       salesforce_push_enqueue_async($entity, $mapping, $mapped_object, $op);
     }
     catch (\Exception $e) {
-      watchdog_exception('Salesforce Push', $e);
+      \Drupal::logger('Salesforce Push')->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
     }
     return;
   }
@@ -163,12 +173,20 @@ function salesforce_push_entity_crud_mapping(EntityInterface $entity, $op, Sales
       new SalesforcePushEvent($mapped_object, NULL, $op)
     );
 
-    watchdog_exception('Salesforce Push', $e);
+    \Drupal::logger('Salesforce Push')->log(
+      LogLevel::ERROR,
+      '%type: @message in %function (line %line of %file).',
+      Error::decodeException($e)
+    );
     try {
       salesforce_push_enqueue_async($entity, $mapping, $mapped_object, $op);
     }
     catch (\Exception $e) {
-      watchdog_exception('Salesforce Push', $e);
+      \Drupal::logger('Salesforce Push')->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
     }
 
     $mapped_object
@@ -213,6 +231,10 @@ function salesforce_push_cron() {
     $queue->processQueues();
   }
   catch (\Exception $e) {
-    watchdog_exception('Salesforce Push', $e);
+    \Drupal::logger('Salesforce Push')->log(
+      LogLevel::ERROR,
+      '%type: @message in %function (line %line of %file).',
+      Error::decodeException($e)
+    );
   }
 }
diff --git a/modules/salesforce_push/src/PushQueue.php b/modules/salesforce_push/src/PushQueue.php
index b309de91..a52c50ca 100644
--- a/modules/salesforce_push/src/PushQueue.php
+++ b/modules/salesforce_push/src/PushQueue.php
@@ -13,9 +13,11 @@ use Drupal\Core\Queue\DatabaseQueue;
 use Drupal\Core\Queue\RequeueException;
 use Drupal\Core\Queue\SuspendQueueException;
 use Drupal\Core\State\State;
+use Drupal\Core\Utility\Error;
 use Drupal\salesforce_mapping\MappedObjectStorage;
 use Drupal\salesforce_mapping\SalesforceMappingStorage;
 use Drupal\salesforce\EntityNotFoundException;
+use Psr\Log\LogLevel;
 
 /**
  * Salesforce push queue.
@@ -38,6 +40,7 @@ class PushQueue extends DatabaseQueue {
   protected $limit;
   protected $connection;
   protected $state;
+  protected $logger;
   protected $queueManager;
   protected $max_fails;
 
@@ -309,14 +312,22 @@ class PushQueue extends DatabaseQueue {
           // Getting a Requeue here is weird for a group of items, but we'll
           // deal with it.
           $this->releaseItems($items);
-          watchdog_exception('Salesforce Push', $e);
+          $this->logger->log(
+            LogLevel::ERROR,
+            '%type: @message in %function (line %line of %file).',
+            Error::decodeException($e)
+          );
         }
         catch (SuspendQueueException $e) {
           // Getting a SuspendQueue is more likely, e.g. because of a network
           // or authorization error. Release items and move on to the next
           // mapping in this case.
           $this->releaseItems($items);
-          watchdog_exception('Salesforce Push', $e);
+          $this->logger->log(
+            LogLevel::ERROR,
+            '%type: @message in %function (line %line of %file).',
+            Error::decodeException($e)
+          );
 
           continue 2;
         }
@@ -324,7 +335,11 @@ class PushQueue extends DatabaseQueue {
           // In case of any other kind of exception, log it and leave the item
           // in the queue to be processed again later.
           // @TODO: this is how Cron.php queue works, but I don't really understand why it doesn't get re-queued.
-          watchdog_exception('Salesforce Push', $e);
+          $this->logger->log(
+            LogLevel::ERROR,
+            '%type: @message in %function (line %line of %file).',
+            Error::decodeException($e)
+          );
         }
         finally {
           // If we've reached our limit, we're done. Otherwise, continue to next items.
@@ -404,7 +419,11 @@ class PushQueue extends DatabaseQueue {
       return $update->execute();
     }
     catch (\Exception $e) {
-      watchdog_exception('Salesforce Push', $e);
+      $this->logger->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
       $this->catchException($e);
       // If the table doesn't exist we should consider the item released.
       return TRUE;
diff --git a/src/Form/AuthorizeForm.php b/src/Form/AuthorizeForm.php
index ec996899..dc8e2158 100644
--- a/src/Form/AuthorizeForm.php
+++ b/src/Form/AuthorizeForm.php
@@ -2,15 +2,18 @@
 
 namespace Drupal\salesforce\Form;
 
-use GuzzleHttp\Exception\RequestException;
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Form\ConfigFormBase;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
 use Drupal\Core\State\StateInterface;
+use Drupal\Core\Url;
+use Drupal\Core\Utility\Error;
 use Drupal\salesforce\Exception;
 use Drupal\salesforce\Rest\RestClient;
 use Drupal\salesforce\SalesforceClient;
-use Drupal\Core\Url;
+use GuzzleHttp\Exception\RequestException;
+use Psr\Log\LogLevel;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 
@@ -28,6 +31,8 @@ class AuthorizeForm extends ConfigFormBase {
    */
   protected $state;
 
+  protected $logger;
+
   /**
    * Constructs a \Drupal\system\ConfigFormBase object.
    *
@@ -40,10 +45,11 @@ class AuthorizeForm extends ConfigFormBase {
    * @param \Drupal\Core\State\StateInterface $state
    *   The state keyvalue collection to use.
    */
-  public function __construct(ConfigFactoryInterface $config_factory, RestClient $salesforce_client, StateInterface $state) {
+  public function __construct(ConfigFactoryInterface $config_factory, RestClient $salesforce_client, StateInterface $state, LoggerChannelFactoryInterface $logger_factory) {
     parent::__construct($config_factory);
     $this->sf_client = $salesforce_client;
     $this->state = $state;
+    $this->logger = $logger_factory->get(__CLASS__);
   }
 
   /**
@@ -53,7 +59,8 @@ class AuthorizeForm extends ConfigFormBase {
     return new static(
       $container->get('config.factory'),
       $container->get('salesforce.client'),
-      $container->get('state')
+      $container->get('state'),
+      $container->get('logger.factory')
     );
   }
 
@@ -120,7 +127,11 @@ class AuthorizeForm extends ConfigFormBase {
         ];
       }
       catch (RequestException $e) {
-        watchdog_exception(__CLASS__, $e);
+        $this->logger->log(
+          LogLevel::ERROR,
+          '%type: @message in %function (line %line of %file).',
+          Error::decodeException($e)
+        );
         salesforce_set_message($e->getMessage(), 'warning');
       }
     }
@@ -156,7 +167,11 @@ class AuthorizeForm extends ConfigFormBase {
     }
     catch (RequestException $e) {
       drupal_set_message(t("Error during authorization: %message", $e->getMessage()), 'error');
-      watchdog_exception(__CLASS__, $e);
+      $this->logger->log(
+        LogLevel::ERROR,
+        '%type: @message in %function (line %line of %file).',
+        Error::decodeException($e)
+      );
     }
 
     parent::submitForm($form, $form_state);
-- 
GitLab