Completed
Push — master ( 5f3e5a...bee123 )
by Tobias
08:55 queued 08:37
created

ProviderCache.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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