Completed
Pull Request — master (#83)
by BENOIT
04:48
created

GreedyCacheStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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
    /**
23
     * @var int
24
     */
25
    protected $ttl;
26
27
    /**
28
     * @var KeyValueHttpHeader
29
     */
30
    private $varyHeaders;
31
32 7
    public function __construct(CacheStorageInterface $cache = null, $ttl, KeyValueHttpHeader $varyHeaders = null)
33
    {
34 7
        $this->ttl = $ttl;
35 7
        $this->varyHeaders = $varyHeaders;
36
37 7
        parent::__construct($cache);
38 7
    }
39
40 6
    protected function getCacheKey(RequestInterface $request, KeyValueHttpHeader $varyHeaders = null)
41
    {
42 6
        if (null === $varyHeaders || $varyHeaders->isEmpty()) {
43 5
            return hash(
44 5
                'sha256',
45 5
                'greedy'.$request->getMethod().$request->getUri()
46 5
            );
47
        }
48 1
        $cacheHeaders = [];
49
50 1
        foreach ($varyHeaders as $key => $value) {
51 1
            if ($request->hasHeader($key)) {
52 1
                $cacheHeaders[$key] = $request->getHeader($key);
53 1
            }
54 1
        }
55
56 1
        return hash(
57 1
            'sha256',
58 1
            'greedy'.$request->getMethod().$request->getUri().json_encode($cacheHeaders)
59 1
        );
60
    }
61
62 6
    public function cache(RequestInterface $request, ResponseInterface $response)
63
    {
64 6
        $warningMessage = sprintf('%d - "%s" "%s"',
65 6
            299,
66 6
            'Cached although the response headers indicate not to do it!',
67 6
            (new \DateTime())->format(\DateTime::RFC1123)
68 6
        );
69
70 6
        $response = $response->withAddedHeader('Warning', $warningMessage);
71
72 6
        if ($cacheObject = $this->getCacheObject($request, $response)) {
73 5
            $varyHeaders = $this->varyHeaders;
74 5
            return $this->storage->save(
75 5
                $this->getCacheKey($request, $varyHeaders),
76
                $cacheObject
77 5
            );
78
        }
79
80 1
        return false;
81
    }
82
83 6
    protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
84
    {
85 6
        if (!array_key_exists($response->getStatusCode(), $this->statusAccepted)) {
86
            // Don't cache it
87 1
            return null;
88
        }
89 5
        $varyHeader = $this->varyHeaders;
90
91 5
        if (null !== $varyHeader && $varyHeader->has('*')) {
92
            // This will never match with a request
93
            return;
94
        }
95
96
97 5
        return new CacheEntry($request, $response, new \DateTime(sprintf('+%d seconds', $this->ttl)));
98
    }
99
100 6
    public function fetch(RequestInterface $request)
101
    {
102 6
        $cache = $this->storage->fetch($this->getCacheKey($request, $this->varyHeaders));
103 6
        return $cache;
104
    }
105
}
106