Completed
Push — master ( 3768b1...900f6d )
by Tobias
20:31
created

ProviderCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 27.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 24
loc 87
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A geocodeQuery() 12 12 2
A reverseQuery() 12 12 2
A getName() 0 4 1
A __call() 0 4 1
A getCacheKey() 0 5 1

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\Cache;
14
15
use Geocoder\Collection;
16
use Geocoder\Query\GeocodeQuery;
17
use Geocoder\Query\ReverseQuery;
18
use Geocoder\Provider\Provider;
19
use Psr\SimpleCache\CacheInterface;
20
21
/**
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class ProviderCache implements Provider
25
{
26
    /**
27
     * @var Provider
28
     */
29
    protected $realProvider;
30
31
    /**
32
     * @var CacheInterface
33
     */
34
    protected $cache;
35
36
    /**
37
     * How long a result is going to be cached.
38
     *
39
     * @var int|null
40
     */
41
    protected $lifetime;
42
43
    /**
44
     * @param Provider       $realProvider
45
     * @param CacheInterface $cache
46
     * @param int            $lifetime
47
     */
48 6
    final public function __construct(Provider $realProvider, CacheInterface $cache, int $lifetime = null)
49
    {
50 6
        $this->realProvider = $realProvider;
51 6
        $this->cache = $cache;
52 6
        $this->lifetime = $lifetime;
53 6
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2 View Code Duplication
    final public function geocodeQuery(GeocodeQuery $query): Collection
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...
59
    {
60 2
        $cacheKey = $this->getCacheKey($query);
61 2
        if (null !== $result = $this->cache->get($cacheKey)) {
62 1
            return $result;
63
        }
64
65 1
        $result = $this->realProvider->geocodeQuery($query);
66 1
        $this->cache->set($cacheKey, $result, $this->lifetime);
67
68 1
        return $result;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2 View Code Duplication
    final public function reverseQuery(ReverseQuery $query): Collection
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...
75
    {
76 2
        $cacheKey = $this->getCacheKey($query);
77 2
        if (null !== $result = $this->cache->get($cacheKey)) {
78 1
            return $result;
79
        }
80
81 1
        $result = $this->realProvider->reverseQuery($query);
82 1
        $this->cache->set($cacheKey, $result, $this->lifetime);
83
84 1
        return $result;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function getName(): string
91
    {
92 1
        return sprintf('%s (cache)', $this->realProvider->getName());
93
    }
94
95 1
    final public function __call($method, $args)
96
    {
97 1
        return call_user_func_array([$this->realProvider, $method], $args);
98
    }
99
100
    /**
101
     * @param GeocodeQuery|ReverseQuery $query
102
     *
103
     * @return string
104
     */
105 4
    protected function getCacheKey($query): string
106
    {
107
        // Include the major version number of the geocoder to avoid issues unserializing.
108 4
        return 'v4'.sha1((string) $query);
109
    }
110
}
111