ClientService.php in Sparkpost email 8
File
src/ClientService.php
View source
<?php
namespace Drupal\sparkpost;
use Drupal\Core\Config\ConfigFactory;
use GuzzleHttp\Client;
use SparkPost\SparkPost;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
class ClientService implements ClientServiceInterface {
protected $configFactory;
protected $client;
public function __construct(Client $client, ConfigFactory $configFactory) {
$this->client = $client;
$this->configFactory = $configFactory;
}
public function getClient() {
$config = $this->configFactory
->get('sparkpost.settings');
$httpClient = new GuzzleAdapter($this->client);
return new SparkPost($httpClient, [
'key' => $config
->get('api_key'),
]);
}
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();
}
public function sendRequest($endpoint, array $data, $method = 'GET') {
$client = $this
->getClient();
$promise = $client
->request($method, $endpoint, $data);
$response = $promise
->wait();
return $response
->getBody();
}
}