Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
class Connection
{
public const VERSION = '3.0.1';
public const VERSION = '3.1.0';

public const BASE_URL = 'https://app.sendy.nl';

Expand Down Expand Up @@ -60,6 +60,9 @@ class Connection
/** @var string The appendix for the user agent */
private string $userAgentAppendix = '';

/** @var string|null The URL of the application or website using this SDK, sent as the Referer header */
private ?string $referer = null;

/** @var mixed */
private $state = null;

Expand Down Expand Up @@ -96,6 +99,13 @@ public function setUserAgentAppendix(string $userAgentAppendix): Connection
return $this;
}

public function setReferer(?string $referer): Connection
{
$this->referer = $referer;

return $this;
}

public function setClientId(string $clientId): Connection
{
$this->clientId = $clientId;
Expand Down Expand Up @@ -295,6 +305,10 @@ public function createRequest(
'User-Agent' => trim($userAgent),
]);

if ($this->referer !== null) {
$headers['Referer'] = $this->referer;
}

if (! empty($params)) {
$endpoint .= strpos($endpoint, '?') === false ? '?' : '&';
$endpoint .= http_build_query($params);
Expand Down
13 changes: 13 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public function testUserAgentIsSet(): void
);
}

public function testRefererIsSet(): void
{
$connection = $this->createConnection();
$this->assertArrayNotHasKey('referer', $connection->createRequest('GET', '/')->getHeaders());

$connection = $this->createConnection();
$connection->setReferer('https://example.com');
$this->assertEquals(
'https://example.com',
$connection->createRequest('GET', '/')->getHeaders()['referer'],
);
}

public function testTokenExpires(): void
{
$connection = new Connection();
Expand Down