Passed
Push — master ( df6fea...e34f76 )
by Kevin
01:16
created

GreedyCacheStrategy::getCacheObject()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 22
ccs 8
cts 12
cp 0.6667
rs 8.6737
cc 5
eloc 11
nc 4
nop 2
crap 5.9256
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Strategy;
4
5
use Kevinrob\GuzzleCache\CacheEntry;
6
use Kevinrob\GuzzleCache\KeyValueHttpHeader;
7
use Kevinrob\GuzzleCache\Storage\CacheStorageInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * This strategy represents a "greedy" HTTP client.
13
 *
14
 * It can be used to cache responses in spite of any cache related response headers,
15
 * but it SHOULDN'T be used unless absolutely necessary, e.g. when accessing
16
 * badly designed APIs without Cache control.
17
 *
18
 * Obviously, this follows no RFC :(.
19
 */
20
class GreedyCacheStrategy extends PrivateCacheStrategy
21
{
22
    const HEADER_TTL = 'X-Kevinrob-GuzzleCache-TTL';
23
24
    /**
25
     * @var int
26
     */
27
    protected $defaultTtl;
28
29
    /**
30
     * @var KeyValueHttpHeader
31
     */
32
    private $varyHeaders;
33
34 8
    public function __construct(CacheStorageInterface $cache = null, $defaultTtl, KeyValueHttpHeader $varyHeaders = null)
35
    {
36 8
        $this->defaultTtl = $defaultTtl;
37 8
        $this->varyHeaders = $varyHeaders;
38 8
        parent::__construct($cache);
39 8
    }
40
41 7
    protected function getCacheKey(RequestInterface $request, KeyValueHttpHeader $varyHeaders = null)
42
    {
43 7
        if (null === $varyHeaders || $varyHeaders->isEmpty()) {
44 6
            return hash(
45 6
                'sha256',
46 6
                'greedy'.$request->getMethod().$request->getUri()
47 6
            );
48
        }
49
50 1
        $cacheHeaders = [];
51 1
        foreach ($varyHeaders as $key => $value) {
52 1
            if ($request->hasHeader($key)) {
53 1
                $cacheHeaders[$key] = $request->getHeader($key);
54 1
            }
55 1
        }
56
57 1
        return hash(
58 1
            'sha256',
59 1
            'greedy'.$request->getMethod().$request->getUri().json_encode($cacheHeaders)
60 1
        );
61
    }
62
63 7
    public function cache(RequestInterface $request, ResponseInterface $response)
64
    {
65 7
        $warningMessage = sprintf('%d - "%s" "%s"',
66 7
            299,
67 7
            'Cached although the response headers indicate not to do it!',
68 7
            (new \DateTime())->format(\DateTime::RFC1123)
69 7
        );
70
71 7
        $response = $response->withAddedHeader('Warning', $warningMessage);
72
73 7
        if ($cacheObject = $this->getCacheObject($request, $response)) {
74 6
            return $this->storage->save(
75 6
                $this->getCacheKey($request, $this->varyHeaders),
76
                $cacheObject
77 6
            );
78
        }
79
80 1
        return false;
81
    }
82
83 7
    protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
84
    {
85 7
        if (!array_key_exists($response->getStatusCode(), $this->statusAccepted)) {
86
            // Don't cache it
87 1
            return null;
88
        }
89
90 6
        if (null !== $this->varyHeaders && $this->varyHeaders->has('*')) {
91
            // This will never match with a request
92
            return;
93
        }
94
95 6
        $response = $response->withoutHeader('Etag')->withoutHeader('Last-Modified');
96
97 6
        $ttl = $this->defaultTtl;
98 6
        if ($request->hasHeader(self::HEADER_TTL)) {
99
            $ttlHeaderValues = $request->getHeader(self::HEADER_TTL);
100
            $ttl = (int)reset($ttlHeaderValues);
101
        }
102
103 6
        return new CacheEntry($request->withoutHeader(self::HEADER_TTL), $response, new \DateTime(sprintf('+%d seconds', $ttl)));
104
    }
105
106 7
    public function fetch(RequestInterface $request)
107
    {
108 7
        $cache = $this->storage->fetch($this->getCacheKey($request, $this->varyHeaders));
109 7
        return $cache;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function delete(RequestInterface $request)
116
    {
117
        return $this->storage->delete($this->getCacheKey($request));
118
    }
119
}
120