Completed
Push — 2.0 ( 9cfd8f...0c0b7d )
by Peter
03:41
created

CacheClient::setProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service\Client;
10
11
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Client\Cache\ExpireResolver;
12
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Client\Cache\Storage\StorageInterface;
13
14
class CacheClient implements ClientInterface
15
{
16
    /**
17
     * @var ClientInterface
18
     */
19
    protected $client;
20
21
    /**
22
     * @var ExpireResolver
23
     */
24
    protected $resolver;
25
26
    /**
27
     * @var StorageInterface
28
     */
29
    protected $storage;
30
31
    /**
32
     * @param ClientInterface $client
33
     * @param ExpireResolver $resolver
34
     * @param StorageInterface $storage
35
     */
36 9
    public function __construct(ClientInterface $client, ExpireResolver $resolver, StorageInterface $storage)
37
    {
38 9
        $this->client = $client;
39 9
        $this->resolver = $resolver;
40 9
        $this->storage = $storage;
41 9
    }
42
43
    /**
44
     * @param int $timeout
45
     *
46
     * @return CacheClient
47
     */
48 1
    public function setTimeout($timeout)
49
    {
50 1
        $this->client->setTimeout($timeout);
51
52 1
        return $this;
53
    }
54
55
    /**
56
     * @param string $proxy
57
     *
58
     * @return CacheClient
59
     */
60 1
    public function setProxy($proxy)
61
    {
62 1
        $this->client->setProxy($proxy);
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * @param string $request
69
     * @param array $params
70
     *
71
     * @return string
72
     */
73 7
    public function get($request, array $params = [])
74
    {
75 7
        $expires = $this->resolver->getExpire($request, new \DateTime());
76 7
        if (!$expires) {
77 1
            return $this->client->get($request, $params);
78
        }
79
80 6
        $key = http_build_query(['request' => $request] + $params);
81 6
        if (!($response = $this->storage->get($key))) {
82 3
            $response = $this->client->get($request, $params);
83 3
            $this->storage->set($key, $response, $expires);
84 3
        }
85
86 6
        return $response;
87
    }
88
}
89