class ClientService in Sparkpost email 8.2
Same name and namespace in other branches
- 8 src/ClientService.php \Drupal\sparkpost\ClientService
Client service that sends the mails.
@package Drupal\sparkpost
Hierarchy
- class \Drupal\sparkpost\ClientService implements ClientServiceInterface
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'
1 service uses ClientService
File
- src/
ClientService.php, line 15
Namespace
Drupal\sparkpostView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ClientService:: |
protected | property | GuzzleHttp\Client definition. | |
ClientService:: |
protected | property | Drupal\Core\Config\ConfigFactory definition. | |
ClientService:: |
constant | Regex for parsing email. | ||
ClientService:: |
public | function |
Gets the client. Overrides ClientServiceInterface:: |
|
ClientService:: |
public | function |
Sends the message. Overrides ClientServiceInterface:: |
|
ClientService:: |
public | function |
Sends the request. Overrides ClientServiceInterface:: |
|
ClientService:: |
public | function | Constructor. |