CachedRemoteData   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 97
ccs 31
cts 31
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getCachePool() 0 4 1
A getUrl() 0 4 1
A getKey() 0 4 1
A get() 0 13 2
A load() 0 21 2
1
<?php
2
3
namespace CL\CurrencyConvert;
4
5
use CL\PsrCache\CacheItemPoolInterface;
6
use Exception;
7
8
/**
9
 * @author    Ivan Kerin <[email protected]>
10
 * @copyright 2014, Clippings Ltd.
11
 * @license   http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class CachedRemoteData
14
{
15
    /**
16
     * @var string
17
     */
18
    private $url;
19
20
    /**
21
     * @var string
22
     */
23
    private $key;
24
25
    /**
26
     * @var CacheItemPoolInterface
27
     */
28
    private $cachePool;
29
30
    /**
31
     * @param CacheItemPoolInterface $cachePool
32
     * @param string                 $url
33
     * @param string                 $key
34
     */
35 1
    public function __construct(CacheItemPoolInterface $cachePool, $url, $key = null)
36
    {
37 1
        $this->cachePool = $cachePool;
38 1
        $this->url = $url;
39 1
        $this->key = $key ?: $url;
40 1
    }
41
42
    /**
43
     * @return CacheItemPoolInterface
44
     */
45 1
    public function getCachePool()
46
    {
47 1
        return $this->cachePool;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 1
    public function getUrl()
54
    {
55 1
        return $this->url;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 1
    public function getKey()
62
    {
63 1
        return $this->key;
64
    }
65
66
    /**
67
     * @return string
68
     * @throws Exception If problems loading data
69
     */
70 1
    public function get()
71
    {
72 1
        $item = $this->cachePool->getItem($this->key);
73
74 1
        if (! $item->isHit()) {
75
            // Cache for a day
76
            $item
77 1
                ->set($this->load(), 86400)
78 1
                ->save();
79 1
        }
80
81 1
        return $item->get();
82
    }
83
84
    /**
85
     * @return string
86
     * @throws Exception If problems loading data
87
     */
88 2
    public function load()
89
    {
90 2
        $ch = curl_init();
91
92 2
        curl_setopt($ch, CURLOPT_URL, $this->url);
93 2
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
94 2
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
95 2
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)');
96
97 2
        $data = curl_exec($ch);
98
99 2
        if ($data === false) {
100 1
            throw new Exception(
101 1
                sprintf('Error %s when loading data from: %s', curl_error($ch), $this->url)
102 1
            );
103
        }
104
105 1
        curl_close($ch);
106
107 1
        return $data;
108
    }
109
}
110