Completed
Pull Request — master (#42)
by Chad
02:53
created

PredisCache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 10.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 8
loc 75
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B set() 8 28 3
A get() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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