Skip to content

Commit e72b02b

Browse files
committed
Added options resolver
1 parent e31a287 commit e72b02b

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"php": ">=5.4",
1515
"php-http/httplug": "1.0.0-alpha3",
1616
"php-http/client-tools": "^0.1@dev",
17-
"php-http/message-factory": "^1.0"
17+
"php-http/message-factory": "^1.0",
18+
"symfony/options-resolver": "^2.3|^3.0"
1819
},
1920
"require-dev": {
2021
"phpspec/phpspec": "^2.4-alpha",

src/CachePlugin.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Cache\CacheItemPoolInterface;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
1011

1112
/**
1213
* Allow for caching a response.
@@ -25,32 +26,22 @@ class CachePlugin implements Plugin
2526
*/
2627
private $streamFactory;
2728

28-
/**
29-
* Default time to store object in cache. This value is used if CachePlugin::respectCacheHeaders is false or
30-
* if cache headers are missing.
31-
*
32-
* @var int
33-
*/
34-
private $defaultTtl;
35-
36-
/**
37-
* Look at the cache headers to know how long this response is going to be cached.
38-
*
39-
* @var bool
40-
*/
41-
private $respectCacheHeaders;
29+
private $config = [
30+
'default_ttl' => null,
31+
'respect_cache_headers' => true
32+
];
4233

4334
/**
4435
* @param CacheItemPoolInterface $pool
4536
* @param StreamFactory $streamFactory
46-
* @param array $options
37+
* @param array $config
4738
*/
48-
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $options = [])
39+
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
4940
{
5041
$this->pool = $pool;
5142
$this->streamFactory = $streamFactory;
52-
$this->defaultTtl = isset($options['default_ttl']) ? $options['default_ttl'] : null;
53-
$this->respectCacheHeaders = isset($options['respect_cache_headers']) ? $options['respect_cache_headers'] : true;
43+
44+
$this->config = $this->configure($config);
5445
}
5546

5647
/**
@@ -151,8 +142,8 @@ private function createCacheKey(RequestInterface $request)
151142
*/
152143
private function getMaxAge(ResponseInterface $response)
153144
{
154-
if (!$this->respectCacheHeaders) {
155-
return $this->defaultTtl;
145+
if (!$this->config['respect_cache_headers']) {
146+
return $this->config['default_ttl'];
156147
}
157148

158149
// check for max age in the Cache-Control header
@@ -172,6 +163,22 @@ private function getMaxAge(ResponseInterface $response)
172163
return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp();
173164
}
174165

175-
return $this->defaultTtl;
166+
return $this->config['default_ttl'];
167+
}
168+
169+
/**
170+
* @param array $config
171+
*
172+
* @return array
173+
*/
174+
protected function configure(array $config = [])
175+
{
176+
$resolver = new OptionsResolver();
177+
$resolver->setDefaults($this->config);
178+
179+
$resolver->setAllowedTypes('default_ttl', 'int');
180+
$resolver->setAllowedTypes('respect_cache_headers', 'bool');
181+
182+
return $resolver->resolve($config);
176183
}
177184
}

0 commit comments

Comments
 (0)