Skip to content
Merged
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
9 changes: 9 additions & 0 deletions src/Client/Application/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

class ClientConfiguration
{
const VERSION = '3.0.0';
private string $apiKey;
private array $userAgentComponents = [];
private array $config = [];
Expand Down Expand Up @@ -60,6 +61,7 @@ public function __construct(string $apiKey, ?Environment $environment = null, ar
} catch (InvalidArgumentException $e) {
$this->addError("Invalid configuration: " . $e->getMessage());
}
$this->initUserAgent();
}

/**
Expand Down Expand Up @@ -216,4 +218,11 @@ public function hasError(): bool {
public function getErrors(): array {
return array_values($this->errors);
}

private function initUserAgent()
{
$phpVersion = rtrim(str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION), '-');
$this->addUserAgentComponent('PHP', $phpVersion);
$this->addUserAgentComponent('Alma for PHP', self::VERSION);
}
}
1 change: 0 additions & 1 deletion src/Client/Application/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

class CurlClient implements ClientInterface
{
const VERSION = '3.0.0';

private ClientConfiguration $config;

Expand Down
2 changes: 2 additions & 0 deletions src/Client/Application/Endpoint/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public function __construct(ClientInterface $client)
* @return Request The Request object
*/
private function createRequest(string $method, string $uri, array $body = []): Request {

$headers = [
'User-Agent: ' . $this->client->getConfig()->getUserAgentString(),
'Authorization' => ['Alma-Auth ' . $this->client->getConfig()->getApiKey()]
];
return new Request($method, $uri, $headers, json_encode($body));
Expand Down
9 changes: 6 additions & 3 deletions tests/Client/Unit/Application/ClientConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ClientConfigurationTest extends TestCase
{
/** @var ClientConfiguration */
private ClientConfiguration $clientConfig;

public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -169,6 +169,7 @@ public static function timeoutConfigDataProvider(): array
]
];
}

/**
* @dataProvider timeoutConfigDataProvider
* @return void
Expand Down Expand Up @@ -196,8 +197,10 @@ public function testValidateVerify()

public function testAddUserAgentComponent()
{
$this->clientConfig->addUserAgentComponent('alma-php-sdk', '1.0.0');
$this->assertEquals('alma-php-sdk/1.0.0', $this->clientConfig->getUserAgentString());
$phpVersion = rtrim(str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION), '-');

$this->clientConfig->addUserAgentComponent('wc-version', '1.0.0');
$this->assertEquals(sprintf('wc-version/1.0.0; Alma for PHP/%s; PHP/%s', ClientConfiguration::VERSION, $phpVersion), $this->clientConfig->getUserAgentString());
}

/**
Expand Down
Loading