Skip to content

Commit 59b1a85

Browse files
committed
feat(interfaces): Add ApplicationInterface and IntegrationInterface
1 parent 3d0ab34 commit 59b1a85

File tree

8 files changed

+282
-2
lines changed

8 files changed

+282
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased](https://github.com/contentful/contentful-core.php/compare/2.1.1...HEAD)
77

88
<!-- PENDING-CHANGES -->
9-
> No meaningful changes since last release.
9+
### Added
10+
11+
* Interface `ApplicationInterface` and `IntegrationInterface` have been added, as well as `ClientInterface::useApplication(ApplicationInterface $application)` and `ClientInterface::useIntegration(IntegrationInterface $integration)`. The `BaseClient` class already provides an implementation of these methods.
1012
<!-- /PENDING-CHANGES -->
1113

1214
## [2.1.1](https://github.com/contentful/contentful-core.php/tree/2.1.1) (2018-11-08)

src/Api/ApplicationInterface.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the contentful/contentful-core package.
5+
*
6+
* @copyright 2015-2018 Contentful GmbH
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Contentful\Core\Api;
13+
14+
interface ApplicationInterface
15+
{
16+
/**
17+
* Returns the name of the current application.
18+
* This value must be the one that is sent as part of
19+
* the "X-Contentful-User-Agent" header to the API.
20+
*
21+
* @return string
22+
*/
23+
public function getApplicationName(): string;
24+
25+
/**
26+
* Returns whether the current application is distributed as a Composer package.
27+
*
28+
* @return bool
29+
*/
30+
public function isPackagedApplication(): bool;
31+
32+
/**
33+
* Returns the package name of the current application.
34+
* If the application is not distributed as a package, this method
35+
* must return an empty string.
36+
*
37+
* @return string
38+
*/
39+
public function getApplicationPackageName(): string;
40+
41+
/**
42+
* Returns the version of the current application.
43+
* This must return an actual version if the application is not distributed
44+
* as a Composer package.
45+
*
46+
* @return string
47+
*/
48+
public function getApplicationVersion(): string;
49+
}

src/Api/BaseClient.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ public function getHost(): string
201201
return $this->host;
202202
}
203203

204+
/**
205+
* {@inheritdoc}
206+
*/
207+
public function useApplication(ApplicationInterface $application)
208+
{
209+
$version = $application->isPackagedApplication()
210+
? self::getVersionForPackage($application->getApplicationPackageName())
211+
: $application->getApplicationVersion();
212+
213+
$this->userAgentGenerator->setApplication(
214+
$application->getApplicationName(),
215+
$version
216+
);
217+
}
218+
204219
/**
205220
* {@inheritdoc}
206221
*/
@@ -211,6 +226,17 @@ public function setApplication(string $name, string $version = '')
211226
return $this;
212227
}
213228

229+
/**
230+
* {@inheritdoc}
231+
*/
232+
public function useIntegration(IntegrationInterface $integration)
233+
{
234+
$this->userAgentGenerator->setIntegration(
235+
$integration->getIntegrationName(),
236+
self::getVersionForPackage($integration->getIntegrationPackageName())
237+
);
238+
}
239+
214240
/**
215241
* {@inheritdoc}
216242
*/
@@ -225,6 +251,16 @@ public function setIntegration(string $name, string $version = '')
225251
* @return string
226252
*/
227253
public static function getVersion(): string
254+
{
255+
return self::getVersionForPackage(static::getPackageName());
256+
}
257+
258+
/**
259+
* @param string $package
260+
*
261+
* @return string
262+
*/
263+
protected static function getVersionForPackage(string $package): string
228264
{
229265
try {
230266
$shortVersion = PrettyVersions::getVersion(static::getPackageName())

src/Api/ClientInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,41 @@ interface ClientInterface
3232
*/
3333
public function request(string $method, string $uri, array $options = []): ResourceInterface;
3434

35+
/**
36+
* Sets the current application.
37+
* The values are used as part of the X-Contentful-User-Agent header.
38+
*
39+
* @param ApplicationInterface $application
40+
*/
41+
public function useApplication(ApplicationInterface $application);
42+
3543
/**
3644
* Set the application name and version.
3745
* The values are used as part of the X-Contentful-User-Agent header.
3846
*
3947
* @param string $name
4048
* @param string $version
49+
*
50+
* @deprecated 2.2.0 Use useApplication instead
4151
*/
4252
public function setApplication(string $name, string $version = '');
4353

54+
/**
55+
* Sets the current integration.
56+
* The values are used as part of the X-Contentful-User-Agent header.
57+
*
58+
* @param IntegrationInterface $integration
59+
*/
60+
public function useIntegration(IntegrationInterface $integration);
61+
4462
/**
4563
* Set the integration name and version.
4664
* The values are used as part of the X-Contentful-User-Agent header.
4765
*
4866
* @param string $name
4967
* @param string $version
68+
*
69+
* @deprecated 2.2.0 Use useIntegration instead
5070
*/
5171
public function setIntegration(string $name, string $version = '');
5272

src/Api/IntegrationInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the contentful/contentful-core package.
5+
*
6+
* @copyright 2015-2018 Contentful GmbH
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Contentful\Core\Api;
13+
14+
interface IntegrationInterface
15+
{
16+
/**
17+
* Returns the name of the current integration.
18+
* This value must be the one that is sent as part of
19+
* the "X-Contentful-User-Agent" header to the API.
20+
*
21+
* @return string
22+
*/
23+
public function getIntegrationName(): string;
24+
25+
/**
26+
* Returns the package name of the current integration.
27+
* This value must be the one defined in the "composer.json" file.
28+
*
29+
* @return string
30+
*/
31+
public function getIntegrationPackageName(): string;
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the contentful/contentful-core package.
5+
*
6+
* @copyright 2015-2018 Contentful GmbH
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Contentful\Tests\Core\Implementation;
13+
14+
use Contentful\Core\Api\ApplicationInterface;
15+
16+
class Application implements ApplicationInterface
17+
{
18+
/**
19+
* @var bool
20+
*/
21+
private $isPackagedApplication;
22+
23+
public function __construct(bool $isPackagedApplication)
24+
{
25+
$this->isPackagedApplication = $isPackagedApplication;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getApplicationName(): string
32+
{
33+
return 'the-example-app';
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function isPackagedApplication(): bool
40+
{
41+
return $this->isPackagedApplication;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getApplicationPackageName(): string
48+
{
49+
return $this->isPackagedApplication
50+
? 'contentful/the-example-app'
51+
: '';
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getApplicationVersion(): string
58+
{
59+
return $this->isPackagedApplication
60+
? ''
61+
: '1.0.0';
62+
}
63+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the contentful/contentful-core package.
5+
*
6+
* @copyright 2015-2018 Contentful GmbH
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Contentful\Tests\Core\Implementation;
13+
14+
use Contentful\Core\Api\IntegrationInterface;
15+
16+
class Integration implements IntegrationInterface
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function getIntegrationName(): string
22+
{
23+
return 'contentful.symfony';
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getIntegrationPackageName(): string
30+
{
31+
return 'contentful/contentful-bundle';
32+
}
33+
}

tests/Unit/Api/BaseClientTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Contentful\Tests\Core\Unit\Api;
1313

14+
use Contentful\Tests\Core\Implementation\Application;
1415
use Contentful\Tests\Core\Implementation\Client;
1516
use Contentful\Tests\Core\Implementation\ClientCustomException;
1617
use Contentful\Tests\Core\Implementation\Exception\BadRequestException;
18+
use Contentful\Tests\Core\Implementation\Integration;
1719
use Contentful\Tests\Core\Implementation\InvalidPackageNameClient;
1820
use Contentful\Tests\TestCase;
1921
use GuzzleHttp\Client as HttpClient;
@@ -177,10 +179,53 @@ public function testInvalidPackageNameVersion()
177179

178180
$request = $client->getMessages()[0]->getRequest();
179181
// When the current package name is invalid,
180-
//the version will automatically be set to 0.0.0-alpha
182+
// the version will automatically be set to 0.0.0-alpha
181183
$this->assertRegExp(
182184
'/sdk invalid\/0.0.0-alpha; platform PHP\/[0-9\.]*; os (Windows|Linux|macOS);$/',
183185
$request->getHeaderLine('X-Contentful-User-Agent')
184186
);
185187
}
188+
189+
public function testCustomApplication()
190+
{
191+
$httpClient = $this->createHttpClient(function (): ResponseInterface {
192+
return new Response(201);
193+
});
194+
$client = new Client('irrelevant', 'https://cdn.contentful.com', \null, $httpClient);
195+
196+
$client->useApplication(new Application(\false));
197+
$client->callApi('GET', '/');
198+
199+
$request = $client->getMessages()[0]->getRequest();
200+
$this->assertRegExp(
201+
'/^app the-example-app\/1.0.0; sdk contentful-core.php\/[0-9\.]*(-(dev|beta|alpha|RC))?; platform PHP\/[0-9\.]*; os (Windows|Linux|macOS);$/',
202+
$request->getHeaderLine('X-Contentful-User-Agent')
203+
);
204+
205+
$client->useApplication(new Application(\true));
206+
$client->callApi('GET', '/');
207+
208+
$request = $client->getMessages()[0]->getRequest();
209+
$this->assertRegExp(
210+
'/^app the-example-app\/[0-9\.]*(-(dev|beta|alpha|RC))?; sdk contentful-core.php\/[0-9\.]*(-(dev|beta|alpha|RC))?; platform PHP\/[0-9\.]*; os (Windows|Linux|macOS);$/',
211+
$request->getHeaderLine('X-Contentful-User-Agent')
212+
);
213+
}
214+
215+
public function testCustomIntegration()
216+
{
217+
$httpClient = $this->createHttpClient(function (): ResponseInterface {
218+
return new Response(201);
219+
});
220+
$client = new Client('irrelevant', 'https://cdn.contentful.com', \null, $httpClient);
221+
222+
$client->useIntegration(new Integration());
223+
$client->callApi('GET', '/');
224+
225+
$request = $client->getMessages()[0]->getRequest();
226+
$this->assertRegExp(
227+
'/^integration contentful.symfony\/[0-9\.]*(-(dev|beta|alpha|RC))?; sdk contentful-core.php\/[0-9\.]*(-(dev|beta|alpha|RC))?; platform PHP\/[0-9\.]*; os (Windows|Linux|macOS);$/',
228+
$request->getHeaderLine('X-Contentful-User-Agent')
229+
);
230+
}
186231
}

0 commit comments

Comments
 (0)