CachedExchangeRateProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 33.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 39
loc 118
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getAllRatesExchanges() 0 13 2
A getRatesExchanges() 13 13 2
A getRateExchange() 13 13 2
A getRatesExchangesDynamic() 13 13 2
A createCacheKey() 0 5 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
 * (c) itmedia.by <[email protected]>
4
 */
5
6
namespace Submarine\NbrbExchangeRatesBundle\Provider;
7
8
9
use Doctrine\Common\Cache\Cache;
10
11
class CachedExchangeRateProvider implements ExchangeRatesProviderInterface
12
{
13
14
    /**
15
     * @var ExchangeRatesProviderInterface
16
     */
17
    private $provider;
18
19
20
    /**
21
     * @var Cache
22
     */
23
    private $cache;
24
25
    /**
26
     * @var int
27
     */
28
    private $lifetime;
29
30
31
    /**
32
     * CachedExchangeRateProvider constructor.
33
     * @param ExchangeRatesProviderInterface $provider
34
     * @param Cache $cache
35
     * @param int $lifetime
36
     */
37
    public function __construct(ExchangeRatesProviderInterface $provider, Cache $cache, $lifetime = 10800)
38
    {
39
        $this->provider = $provider;
40
        $this->cache = $cache;
41
        $this->lifetime = $lifetime;
42
    }
43
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getAllRatesExchanges(\DateTime $date = null)
49
    {
50
        $cacheKey = $this->createCacheKey('submarine_nbrb', $date);
51
52
        $result = $this->cache->fetch($cacheKey);
53
        if ($result) {
54
            return $result;
55
        }
56
57
        $result = $this->provider->getAllRatesExchanges($date);
58
        $this->cache->save($cacheKey, $result, $this->lifetime);
59
        return $result;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 View Code Duplication
    public function getRatesExchanges(array $codes, \DateTime $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
    {
67
        $cacheKey = $this->createCacheKey('submarine_nbrb_' . implode('', $codes), $date);
68
69
        $result = $this->cache->fetch($cacheKey);
70
        if ($result) {
71
            return $result;
72
        }
73
74
        $result = $this->provider->getRatesExchanges($codes, $date);
75
        $this->cache->save($cacheKey, $result, $this->lifetime);
76
        return $result;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 View Code Duplication
    public function getRateExchange($code, \DateTime $date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
83
    {
84
        $cacheKey = $this->createCacheKey('submarine_nbrb_' . $code, $date);
85
86
        $result = $this->cache->fetch($cacheKey);
87
        if ($result) {
88
            return $result;
89
        }
90
91
        $result = $this->provider->getRateExchange($code, $date);
92
        $this->cache->save($cacheKey, $result, $this->lifetime);
93
        return $result;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 View Code Duplication
    public function getRatesExchangesDynamic($code, \DateTime $firstDate, \DateTime $lastDate)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
100
    {
101
        $cacheKey = 'submarine_nbrb_dyn_' . $firstDate->format('dmy') . $lastDate->format('dmy');
102
103
        $result = $this->cache->fetch($cacheKey);
104
        if ($result) {
105
            return $result;
106
        }
107
108
        $result = $this->provider->getRatesExchangesDynamic($code, $firstDate, $lastDate);
109
        $this->cache->save($cacheKey, $result, $this->lifetime);
110
        return $result;
111
    }
112
113
114
    /**
115
     * Ключ массива
116
     *
117
     * @param $name
118
     * @param \DateTime|null $date
119
     * @return string
120
     */
121
    private function createCacheKey($name, \DateTime $date = null)
122
    {
123
        $dateKey = ($date === null) ? date('dmy') : $date->format('dmy');
124
        return $name . '_' . $dateKey;
125
    }
126
127
128
}