Skip to content

Commit 9143957

Browse files
authored
feat: add user-agent with SDK version string (#69)
1 parent 59922b6 commit 9143957

File tree

7 files changed

+83
-44
lines changed

7 files changed

+83
-44
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ composer.phar
22
composer.lock
33
/vendor/
44
**/coverage
5-
6-
# PHP CS Fixer
75
.php_cs
86
.php_cs.cache
97
.php-cs-fixer.cache
8+
.phpunit.result.cache

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/SumUp/Application/ApplicationConfiguration.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SumUp\Application;
44

55
use SumUp\Exceptions\SumUpConfigurationException;
6+
use SumUp\SdkInfo;
67

78
/**
89
* Class ApplicationConfiguration
@@ -16,6 +17,11 @@ class ApplicationConfiguration implements ApplicationConfigurationInterface
1617
*/
1718
const DEFAULT_SCOPES = ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'];
1819

20+
/**
21+
* Header name for the SDK's User-Agent.
22+
*/
23+
const USER_AGENT_HEADER = 'User-Agent';
24+
1925
/**
2026
* The possible values for grant type.
2127
*/
@@ -393,7 +399,10 @@ protected function setForceGuzzle($forceGuzzle)
393399
*/
394400
public function setCustomHeaders($customHeaders)
395401
{
396-
$this->customHeaders = is_array($customHeaders) ? $customHeaders : [];
402+
$headers = is_array($customHeaders) ? $customHeaders : [];
403+
$headers[self::USER_AGENT_HEADER] = SdkInfo::getUserAgent();
404+
405+
$this->customHeaders = $headers;
397406
}
398407

399408
/**

src/SumUp/SdkInfo.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace SumUp;
4+
5+
/**
6+
* Provides metadata about the SDK.
7+
*/
8+
class SdkInfo
9+
{
10+
const USER_AGENT_TEMPLATE = 'sumup-php/v%s';
11+
12+
/**
13+
* Returns the SDK version embedded in the build.
14+
*
15+
* @return string
16+
*/
17+
public static function getVersion()
18+
{
19+
return Version::CURRENT;
20+
}
21+
22+
/**
23+
* Returns the formatted User-Agent string used in outbound requests.
24+
*
25+
* @return string
26+
*/
27+
public static function getUserAgent()
28+
{
29+
return sprintf(self::USER_AGENT_TEMPLATE, self::getVersion());
30+
}
31+
}

src/SumUp/Utils/Headers.php

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SumUp\Utils;
44

55
use SumUp\Authentication\AccessToken;
6+
use SumUp\SdkInfo;
67

78
/**
89
* Class Headers
@@ -11,12 +12,6 @@
1112
*/
1213
class Headers
1314
{
14-
/**
15-
* Cached value of the project's version.
16-
*
17-
* @var string $cacheVersion
18-
*/
19-
protected static $cacheVersion;
2015
/**
2116
* Get the common header for Content-Type: application/json.
2217
*
@@ -49,39 +44,6 @@ public static function getAuth(AccessToken $accessToken)
4944
return ['Authorization' => 'Bearer ' . $accessToken->getValue()];
5045
}
5146

52-
/**
53-
* Get custom array.
54-
*
55-
* @return array
56-
*/
57-
public static function getTrk()
58-
{
59-
return ['X-SDK' => 'PHP-SDK/v' . self::getProjectVersion() . ' PHP/v' . phpversion()];
60-
}
61-
62-
/**
63-
* Get the version of the project accroding to the composer.json
64-
*
65-
* @return string
66-
*/
67-
public static function getProjectVersion()
68-
{
69-
if (is_null(self::$cacheVersion)) {
70-
self::$cacheVersion = 'unknown';
71-
$pathToComposer = dirname(__FILE__) . '/../../../composer.json';
72-
73-
if (is_readable($pathToComposer)) {
74-
$content = file_get_contents($pathToComposer);
75-
$content = json_decode($content, true);
76-
if (is_array($content) && !empty($content['version'])) {
77-
self::$cacheVersion = $content['version'];
78-
}
79-
}
80-
}
81-
82-
return self::$cacheVersion;
83-
}
84-
8547
/**
8648
* Get standard headers needed for every request.
8749
*
@@ -90,7 +52,7 @@ public static function getProjectVersion()
9052
public static function getStandardHeaders()
9153
{
9254
$headers = self::getCTJson();
93-
$headers += self::getTrk();
55+
$headers['User-Agent'] = SdkInfo::getUserAgent();
9456
return $headers;
9557
}
9658
}

src/SumUp/Version.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// File generated from our OpenAPI spec
4+
5+
namespace SumUp;
6+
7+
class Version
8+
{
9+
const CURRENT = '1.3.0'; // x-release-please-version
10+
}

tests/ApplicationConfigurationTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use SumUp\Application\ApplicationConfiguration;
77
use SumUp\Exceptions\SumUpConfigurationException;
8+
use SumUp\SdkInfo;
89

910
class ApplicationConfigurationTest extends TestCase
1011
{
@@ -34,4 +35,32 @@ public function testInvalidGrantTypeThrowsException()
3435
'grant_type' => 'invalid',
3536
]);
3637
}
38+
39+
public function testUserAgentHeaderIsAlwaysAdded()
40+
{
41+
$config = new ApplicationConfiguration([
42+
'api_key' => 'test-api-key',
43+
]);
44+
45+
$headers = $config->getCustomHeaders();
46+
47+
$this->assertArrayHasKey(ApplicationConfiguration::USER_AGENT_HEADER, $headers);
48+
$this->assertSame(SdkInfo::getUserAgent(), $headers[ApplicationConfiguration::USER_AGENT_HEADER]);
49+
}
50+
51+
public function testCustomUserAgentHeaderIsOverridden()
52+
{
53+
$config = new ApplicationConfiguration([
54+
'api_key' => 'test-api-key',
55+
'custom_headers' => [
56+
'User-Agent' => 'custom-agent',
57+
'X-Custom' => 'value',
58+
],
59+
]);
60+
61+
$headers = $config->getCustomHeaders();
62+
63+
$this->assertSame(SdkInfo::getUserAgent(), $headers[ApplicationConfiguration::USER_AGENT_HEADER]);
64+
$this->assertSame('value', $headers['X-Custom']);
65+
}
3766
}

0 commit comments

Comments
 (0)