Completed
Pull Request — master (#42)
by Chad
03:06
created

PredisCache::set()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 8
loc 24
c 0
b 0
f 0
rs 8.9713
cc 3
eloc 14
nc 3
nop 3
1
<?php
2
3
namespace TraderInteractive\Api;
4
5
use DominionEnterprises\Util;
6
use DominionEnterprises\Util\Arrays;
7
8
/**
9
 * Class to cache API results in a Redis store.
10
 */
11
final class PredisCache implements Cache
12
{
13
    /**
14
     * Predis client for storing cache.
15
     *
16
     * @var \Predis\Client
17
     */
18
    private $client;
19
20
    /**
21
     * Construct a cache instance.
22
     *
23
     * @param \Predis\Client $client The predis client to send data to.
24
     */
25
    public function __construct(\Predis\Client $client)
26
    {
27
        $this->client = $client;
28
    }
29
30
    /**
31
     * @see Cache::set()
32
     */
33
    public function set(Request $request, Response $response, $expires = null)
34
    {
35 View Code Duplication
        if ($expires === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            $expiresHeader = null;
37
            if (!Arrays::tryGet($response->getResponseHeaders(), 'Expires', $expiresHeader)) {
38
                return;
39
            }
40
41
            $expires = Util::ensureNot(
42
                false,
43
                strtotime($expiresHeader[0]),
44
                "Unable to parse Expires value of '{$expiresHeader[0]}'"
45
            );
46
        }
47
48
        $key = self::getKey($request);
49
        $this->client->set(
50
            $key,
51
            json_encode(
52
                [
53
                    'httpCode' => $response->getHttpCode(),
54
                    'headers' => $response->getResponseHeaders(),
55
                    'body' => $response->getResponse(),
56
                ]
57
            )
58
        );
59
        $this->client->expireat($key, $expires);
60
    }
61
62
    /**
63
     * @see Cache::get()
64
     */
65
    public function get(Request $request)
66
    {
67
        $cached = $this->client->get(self::getKey($request));
68
        if ($cached !== null) {
69
            $data = json_decode($cached, true);
70
            return new Response($data['httpCode'], $data['headers'], $data['body']);
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * Helper method to get a unique key for an API request.
78
     *
79
     * This generator does not use the request headers so there is a chance for conflicts
80
     *
81
     * @param Request $request The request from which to generate an unique key
82
     *
83
     * @return string the unique identifier
84
     */
85
    private static function getKey(Request $request)
86
    {
87
        return "{$request->getUrl()}:{$request->getBody()}";
88
    }
89
}
90