You are here

class ClientService in Sparkpost email 8

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

Class ClientService.

@package Drupal\sparkpost

Hierarchy

Expanded class hierarchy of ClientService

1 file declares its use of ClientService
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 {

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

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

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

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

  /**
   * @param array $message
   * @return array
   * @throws \SparkPost\SparkPostException
   */
  public function sendMessage(array $message) {
    $config = $this->configFactory
      ->get('sparkpost.settings');
    $client = $this
      ->getClient();
    $message['content']['from'] = [
      'name' => $config
        ->get('sender_name'),
      'email' => $config
        ->get('sender'),
    ];
    $promise = $client->transmissions
      ->post($message);
    $response = $promise
      ->wait();
    return $response
      ->getBody();
  }

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

}

Members