From 708b5a6098a5148f38c32d0043a416bfed91ac7a Mon Sep 17 00:00:00 2001
From: Alexander Rhodes <ironsizide@787980.no-reply.drupal.org>
Date: Tue, 28 Feb 2017 14:55:02 -0500
Subject: [PATCH] Converts Json service to injected service in RestClient

---
 ROADMAP.txt                      |  9 ++--
 salesforce.services.yml          |  2 +-
 src/Rest/RestClient.php          | 86 ++++++++++++++++++++++----------
 src/Rest/RestClientInterface.php | 14 ++++--
 4 files changed, 75 insertions(+), 36 deletions(-)

diff --git a/ROADMAP.txt b/ROADMAP.txt
index e50976bc..5fdf6a38 100644
--- a/ROADMAP.txt
+++ b/ROADMAP.txt
@@ -26,7 +26,7 @@ Additional details:
 - replace strings with consistent, drupal-wide text
   e.g. "-- Select --", "--" . t('Select') . "--", "Select Object Type", etc.
   Nobody wants to translate all those.
-  
+
 - Replace hooks with interfaces, plugins, event subscribers, etc.
 
 - Automatically pre-add all required Salesforce fields to mappings
@@ -40,17 +40,17 @@ Additional details:
   - easier for other modules to extend, alter behavior
   - standardized, consolidated approach
 
-- Since Drupal Queue Interface is a properly OOP now, we should create a 
+- Since Drupal Queue Interface is a properly OOP now, we should create a
 Salesforce Queue implementation that unlocks the full potential of Salesforce API
 Whether or not this is dependent on SOAP is somewhat irrelevant, as REST and SOAP
-could use the same queue interface, regardless of whether REST can leverage 
+could use the same queue interface, regardless of whether REST can leverage
 the advanced features of SOAP.
 
 - Conversions to do when https://drupal.org/node/1972304 lands
 
 - Migration paths for field mappings
   -- wait for dust to settle on field mapping schema
-  
+
 - Migration paths for mapping object
   -- wait for dust to settle on mapping object schema
 
@@ -203,4 +203,3 @@ salesforce.routing.yml
 
 src/Rest/RestClient.php
 58:   * @TODO: Consider making a test API call.
-149:      // @TODO: convert this into Dependency Injection
diff --git a/salesforce.services.yml b/salesforce.services.yml
index 777d1397..951ca544 100644
--- a/salesforce.services.yml
+++ b/salesforce.services.yml
@@ -1,4 +1,4 @@
 services:
   salesforce.client:
     class: Drupal\salesforce\Rest\RestClient
-    arguments: ['@http_client', '@config.factory', '@state', '@cache.default']
+    arguments: ['@http_client', '@config.factory', '@state', '@cache.default', '@serialization.json']
diff --git a/src/Rest/RestClient.php b/src/Rest/RestClient.php
index 23ba2ebf..e948f083 100644
--- a/src/Rest/RestClient.php
+++ b/src/Rest/RestClient.php
@@ -23,32 +23,82 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  */
 class RestClient implements RestClientInterface {
 
+  /**
+   * Reponse object.
+   *
+   * @var \GuzzleHttp\Psr7\Response
+   */
   public $response;
+
+  /**
+   * GuzzleHttp client.
+   *
+   * @var \GuzzleHttp\ClientInterface
+   */
   protected $httpClient;
+
+  /**
+   * Config factory service.
+   *
+   * @var \Drupal\Core\Config\ConfigFactoryInterface
+   */
   protected $configFactory;
+
+  /**
+   * Salesforce API URL.
+   *
+   * @var Drupal\Core\Url
+   */
   protected $url;
+
+  /**
+   * Salesforce config entity.
+   *
+   * @var \Drupal\Core\Config\ImmutableConfig
+   */
   private $config;
+
+  /**
+   * editable version of config entity.
+   *
+   * @var \Drupal\Core\Config\Config
+   */
   private $configEditable;
+
+  /**
+   * The state service.
+   *
+   * @var \Drupal\Core\State\StateInterface $state
+   */
   private $state;
+
+  /**
+   * The cache service.
+   *
+   * @var Drupal\Core\Cache\CacheBackendInterface cache
+   */
   protected $cache;
 
+  /**
+   * The JSON serializer service.
+   *
+   * @var \Drupal\Component\Serialization\Json $json
+   */
+  protected $json;
+
   const CACHE_LIFETIME = 300;
 
   /**
-   * Constructor which initializes the consumer.
-   *
-   * @param \Drupal\Core\Http\Client $http_client
-   *   The config factory.
-   * @param \Guzzle\Http\ClientInterface $http_client
-   *   The config factory.
+   * {@inheritdoc}
    */
-  public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache) {
+  public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json) {
     $this->configFactory = $config_factory;
     $this->httpClient = $http_client;
     $this->config = $this->configFactory->get('salesforce.settings');
     $this->configEditable = $this->configFactory->getEditable('salesforce.settings');
     $this->state = $state;
     $this->cache = $cache;
+    $this->json = $json;
     return $this;
   }
 
@@ -62,22 +112,7 @@ class RestClient implements RestClientInterface {
   }
 
   /**
-   * Make a call to the Salesforce REST API.
-   *
-   * @param string $path
-   *   Path to resource.
-   * @param array $params
-   *   Parameters to provide.
-   * @param string $method
-   *   Method to initiate the call, such as GET or POST.  Defaults to GET.
-   * @param bool $returnObject
-   *   If true, return a Drupal\salesforce\Rest\RestResponse;
-   *   Otherwise, return json-decoded response body only.
-   *   Defaults to FALSE for backwards compatibility.
-   *
-   * @return mixed
-   *
-   * @throws GuzzleHttp\Exception\RequestException
+   * {@inheritdoc}
    */
   public function apiCall($path, array $params = [], $method = 'GET', $returnObject = FALSE) {
     if (!$this->getAccessToken()) {
@@ -148,8 +183,7 @@ class RestClient implements RestClientInterface {
     ];
     $data = NULL;
     if (!empty($params)) {
-      // @TODO: convert this into Dependency Injection
-      $data = Json::encode($params);
+      $data = $this->json->encode($params);
     }
     return $this->httpRequest($url, $data, $headers, $method);
   }
@@ -188,7 +222,7 @@ class RestClient implements RestClientInterface {
   protected function getErrorData(RequestException $e) {
     $response = $e->getResponse();
     $response_body = $response->getBody()->getContents();
-    $data = Json::decode($response_body);
+    $data = $this->json->decode($response_body);
     if (!empty($data[0])) {
       $data = $data[0];
     }
diff --git a/src/Rest/RestClientInterface.php b/src/Rest/RestClientInterface.php
index 940f90b6..09d81894 100644
--- a/src/Rest/RestClientInterface.php
+++ b/src/Rest/RestClientInterface.php
@@ -27,11 +27,17 @@ interface RestClientInterface {
    * Constructor which initializes the consumer.
    *
    * @param \Drupal\Core\Http\Client $http_client
-   *   The config factory.
+   *   The HTTP Client.
    * @param \Guzzle\Http\ClientInterface $http_client
-   *   The config factory.
-   */
-  public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache);
+   *   The Guzzle Client.
+   * @param \Drupal\Core\State\StateInterface $state
+   *   The state service.
+   * @param \Drupal\Core\Cache\CacheBackendInterface cache
+   *   The cache service.
+   * @param \Drupal\Component\Serialization\Json $json
+   *   The JSON serializer service.
+   */
+  public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json);
 
   /**
    * Determine if this SF instance is fully configured.
-- 
GitLab