Skip to content

Commit

Permalink
Merge pull request #159 from tdclaritum/master
Browse files Browse the repository at this point in the history
Add an option to use only API key - header authentication
  • Loading branch information
roelvanduijnhoven authored May 10, 2024
2 parents 4ff677e + bff5b51 commit 730acb3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"laminas/laminas-servicemanager": "^3.11"
},
"require-dev": {
"ext-json": "*",
"psr/container": "^1.0 || ^2.0",
"guzzlehttp/guzzle": "^7.4",
"laminas/laminas-modulemanager": "^2.8",
Expand Down
5 changes: 0 additions & 5 deletions config/slm_mail.send_grid.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
return array(
'slm_mail' => array(
'send_grid' => array(
/**
* Set your Send Grid username
*/
// 'username' => '',

/**
* Set your Send Grid API key
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/SendGridServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}

$config = $config['slm_mail']['send_grid'];
$service = new SendGridService($config['username'], $config['key']);
$service = new SendGridService($config['key']);

$client = $container->get('SlmMail\Http\Client');
$service->setClient($client);
Expand Down
27 changes: 11 additions & 16 deletions src/Service/SendGridService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ class SendGridService extends AbstractMailService
*/
protected const API_ENDPOINT = 'https://sendgrid.com/api';

/**
* SendGrid username
*
* @var string
*/
protected $username;

/**
* SendGrid API key
*
Expand All @@ -70,12 +63,10 @@ class SendGridService extends AbstractMailService


/**
* @param string $username
* @param string $apiKey
*/
public function __construct(string $username, string $apiKey)
public function __construct(string $apiKey)
{
$this->username = $username;
$this->apiKey = $apiKey;
}

Expand Down Expand Up @@ -344,13 +335,17 @@ public function deleteBlock(string $email): array
*/
private function prepareHttpClient(string $uri, array $parameters = []): HttpClient
{
$parameters = array_merge(['api_user' => $this->username, 'api_key' => $this->apiKey], $parameters);
$client = $this->getClient()
->resetParameters()
->setMethod(HttpRequest::METHOD_GET)
->setUri(self::API_ENDPOINT . $uri)
->setParameterGet($this->filterParameters($parameters));

$client->setHeaders([
'Authorization' => sprintf('Bearer %s', $this->apiKey),
]);

return $this->getClient()
->resetParameters()
->setMethod(HttpRequest::METHOD_GET)
->setUri(self::API_ENDPOINT . $uri)
->setParameterGet($this->filterParameters($parameters));
return $client;
}

/**
Expand Down
34 changes: 33 additions & 1 deletion tests/SlmMailTest/Service/SendGridServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

namespace SlmMailTest\Service;

use Laminas\Http\Client as HttpClient;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use SlmMail\Service\SendGridService;
Expand All @@ -49,14 +50,16 @@

class SendGridServiceTest extends TestCase
{
private const API_KEY = 'my-secret-key';

/**
* @var SendGridService
*/
protected $service;

protected function setUp(): void
{
$this->service = new SendGridService('my-username', 'my-secret-key');
$this->service = new SendGridService(self::API_KEY);
}

public function testCreateFromFactory()
Expand All @@ -80,6 +83,35 @@ public function testResultIsProperlyParsed()
$this->assertEquals($payload, $actual);
}


public function testPrepareHttpClientWithUsername(): void
{
$httpClient = $this->createMock(HttpClient::class);
$httpClient->method('resetParameters')
->willReturn($httpClient);
$httpClient->method('setMethod')
->willReturn($httpClient);
$httpClient->method('setUri')
->willReturn($httpClient);
$httpClient->method('setParameterGet')
->willReturn($httpClient);

$httpClient->expects(self::once())
->method('setHeaders')
->with([
'Authorization' => sprintf(
'Bearer %s',
self::API_KEY
),
]);

$this->service->setClient($httpClient);
$method = new ReflectionMethod($this->service, 'prepareHttpClient');
$method->setAccessible(true);

$method->invoke($this->service, '/mail.send.json', []);
}

/**
* @dataProvider exceptionDataProvider
*/
Expand Down

0 comments on commit 730acb3

Please sign in to comment.