Skip to content

Commit e5b0ea1

Browse files
committed
Refactor structure and add PSR-16 simple cache as required by bookboon/api > 4.0
Signed-off-by: Lasse Mammen <lkm@bookboon.com>
1 parent bb6a7b2 commit e5b0ea1

8 files changed

Lines changed: 28 additions & 24 deletions

File tree

src/Bookboon/ApiBundle/DependencyInjection/BookboonApiExtension.php renamed to DependencyInjection/BookboonApiExtension.php

File renamed without changes.

src/Bookboon/ApiBundle/DependencyInjection/Configuration.php renamed to DependencyInjection/Configuration.php

File renamed without changes.

src/Bookboon/ApiBundle/Helper/ConfigurationHolder.php renamed to Helper/ConfigurationHolder.php

File renamed without changes.

Resources/config/services.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
bookboon.cache:
3+
public: true
4+
class: Symfony\Component\Cache\Simple\RedisCache
5+
arguments:
6+
- '@default_redis_provider'
7+
- 'api'
8+
- 1800
9+
10+
Bookboon\Api\Bookboon:
11+
public: true
12+
factory: ['Bookboon\ApiBundle\Service\ApiFactory', create]
13+
arguments:
14+
- '@Bookboon\ApiBundle\Helper\ConfigurationHolder'
15+
- '@bookboon.cache'

src/Bookboon/ApiBundle/Service/ApiFactory.php renamed to Service/ApiFactory.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
namespace Bookboon\ApiBundle\Service;
44

5-
65
use Bookboon\Api\Bookboon;
7-
use Bookboon\Api\Cache\Cache;
86
use Bookboon\Api\Client\Headers;
97
use Bookboon\Api\Client\Oauth\OauthGrants;
108
use Bookboon\Api\Client\OauthClient;
119
use Bookboon\ApiBundle\Helper\ConfigurationHolder;
10+
use Psr\SimpleCache\CacheInterface;
1211

1312
class ApiFactory
1413
{
1514
/**
1615
* @param ConfigurationHolder $config
17-
* @param Cache $cache
16+
* @param CacheInterface $cache
1817
* @return Bookboon
1918
*/
20-
public static function create(ConfigurationHolder $config, Cache $cache)
19+
public static function create(ConfigurationHolder $config, CacheInterface $cache)
2120
{
2221
$bookboon = new Bookboon(
2322
new OauthClient(
@@ -26,7 +25,7 @@ public static function create(ConfigurationHolder $config, Cache $cache)
2625
self::headersFromConfig($config),
2726
$config->getScopes(),
2827
$cache,
29-
"",
28+
'',
3029
null,
3130
$config->getOverrideAuthUri(),
3231
$config->getOverrideApiUri()
@@ -38,15 +37,15 @@ public static function create(ConfigurationHolder $config, Cache $cache)
3837
return $bookboon;
3938
}
4039

41-
public static function credentialFactory(Bookboon $bookboon, Cache $cache, ConfigurationHolder $config)
40+
public static function credentialFactory(Bookboon $bookboon, CacheInterface $cache, ConfigurationHolder $config)
4241
{
4342
$token = $cache->get("bookboonapi.{$config->getId()}");
4443

45-
if ($token === false) {
44+
if ($token === null) {
4645
$token = $bookboon->getClient()->requestAccessToken([], OauthGrants::CLIENT_CREDENTIALS);
4746

4847
$ttl = $token->getExpires() - time();
49-
$cache->save("bookboonapi.{$config->getId()}", $token, $ttl);
48+
$cache->set("bookboonapi.{$config->getId()}", $token, $ttl);
5049
}
5150

5251
return $token;
@@ -87,10 +86,10 @@ private static function headersFromConfig(ConfigurationHolder $config)
8786
*/
8887
private static function createAcceptLanguageString($languages)
8988
{
90-
$acceptLanguage = "";
91-
for ($i=0; count($languages) > $i; $i++) {
89+
$acceptLanguage = '';
90+
for ($i=0, $iMax = count($languages); $iMax > $i; $i++) {
9291
/* TODO: logic might need to be updated if $i > 10 */
93-
$acceptLanguage .= $i == 0 ? $languages[$i] . ',' : $languages[$i] . ';q=' . (1 - $i/10) . ',';
92+
$acceptLanguage .= $i === 0 ? $languages[$i] . ',' : $languages[$i] . ';q=' . (1 - $i/10) . ',';
9493
}
9594
return rtrim($acceptLanguage,',');
9695
}

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
"php": ">=5.6",
1818
"symfony/dependency-injection": "~4.0",
1919
"symfony/config": "~4.0",
20-
"bookboon/api": "~3.14"
20+
"bookboon/api": "~4.0",
21+
"symfony/cache": "^4.2"
2122
},
2223
"autoload": {
2324
"psr-4": {
24-
"": "src"
25+
"Bookboon\\ApiBundle\\": ""
2526
}
2627
}
2728
}

src/Bookboon/ApiBundle/Resources/config/services.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)