class ClientService in Sparkpost email 8
Same name and namespace in other branches
- 8.2 src/ClientService.php \Drupal\sparkpost\ClientService
Class ClientService.
@package Drupal\sparkpost
Hierarchy
- class \Drupal\sparkpost\ClientService implements ClientServiceInterface
Expanded class hierarchy of ClientService
1 file declares its use of ClientService
- TestMailForm.php in src/
Form/ TestMailForm.php
1 string reference to 'ClientService'
1 service uses ClientService
File
- src/
ClientService.php, line 15
Namespace
Drupal\sparkpostView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ClientService:: |
protected | property | GuzzleHttp\Client definition. | |
ClientService:: |
protected | property | Drupal\Core\Config\ConfigFactory definition. | |
ClientService:: |
public | function |
Overrides ClientServiceInterface:: |
|
ClientService:: |
public | function |
Overrides ClientServiceInterface:: |
|
ClientService:: |
public | function |
Overrides ClientServiceInterface:: |
|
ClientService:: |
public | function | Constructor. |