diff --git a/ROADMAP.txt b/ROADMAP.txt index e50976bcabe0ca95f348ebfa8191cd044d553ee3..5fdf6a388ec573dc65557ea9e41d40cefa0b6014 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 777d1397eba03892e3f36b982aa2f525fac3a0ba..951ca544bbf9fd44e4f2cf86cf0ad63b4792e9a0 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 23ba2ebf2c55b9c5b8f6f09417c804f6b2a79cf6..e948f083cc6fdc9c1e33da0a92d617404bd7b17f 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 940f90b663b66348a80576a0ff6239354f95b5be..09d818942c74e8e77b54ca993d584f32ea5882cc 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.