You are here

class ClientService in Sparkpost email 8.2

Same name and namespace in other branches
  1. 8 src/ClientService.php \Drupal\sparkpost\ClientService

Client service that sends the mails.

@package Drupal\sparkpost

Hierarchy

Expanded class hierarchy of ClientService

2 files declare their use of ClientService
SparkpostMail.php in src/Plugin/Mail/SparkpostMail.php
TestMailForm.php in src/Form/TestMailForm.php
1 string reference to 'ClientService'
sparkpost.services.yml in ./sparkpost.services.yml
sparkpost.services.yml
1 service uses ClientService
sparkpost.client in ./sparkpost.services.yml
Drupal\sparkpost\ClientService

File

src/ClientService.php, line 15

Namespace

Drupal\sparkpost
View source
class ClientService implements ClientServiceInterface {

  /**
   * Regex for parsing email.
   */
  const EMAIL_REGEX = '/^\\s*(.+?)\\s*<\\s*([^>]+)\\s*>$/';

  /**
   * Drupal\Core\Config\ConfigFactory definition.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * GuzzleHttp\Client definition.
   *
   * @var \GuzzleHttp\Client
   */
  protected $client;

  /**
   * Constructor.
   */
  public function __construct(Client $client, ConfigFactory $configFactory) {
    $this->client = $client;
    $this->configFactory = $configFactory;
  }

  /**
   * {@inheritdoc}
   */
  public function getClient() {
    $config = $this->configFactory
      ->get('sparkpost.settings');
    $httpClient = new GuzzleAdapter($this->client);
    $options = [
      'key' => $config
        ->get('api_key'),
    ];
    if (!empty($config
      ->get('api_hostname'))) {
      $options['host'] = $config
        ->get('api_hostname');
    }
    return new SparkPost($httpClient, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function sendMessage(array $message) {
    $client = $this
      ->getClient();
    try {
      $promise = $client->transmissions
        ->post($message);
      $response = $promise
        ->wait();
      return $response
        ->getBody();
    } catch (\Exception $e) {
      \Drupal::logger('sparkpost')
        ->error($e
        ->getMessage());
      throw $e;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function sendRequest($endpoint, array $data, $method = 'GET') {
    $client = $this
      ->getClient();
    $promise = $client
      ->request($method, $endpoint, $data);
    $response = $promise
      ->wait();
    return $response
      ->getBody();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientService::$client protected property GuzzleHttp\Client definition.
ClientService::$configFactory protected property Drupal\Core\Config\ConfigFactory definition.
ClientService::EMAIL_REGEX constant Regex for parsing email.
ClientService::getClient public function Gets the client. Overrides ClientServiceInterface::getClient
ClientService::sendMessage public function Sends the message. Overrides ClientServiceInterface::sendMessage
ClientService::sendRequest public function Sends the request. Overrides ClientServiceInterface::sendRequest
ClientService::__construct public function Constructor.