CacheHandlerFactory::getCacheHandler()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Netgen\Bundle\OpenWeatherMapBundle\Factory;
4
5
use Netgen\Bundle\OpenWeatherMapBundle\Cache\Memcached as MemcachedHandler;
6
use Netgen\Bundle\OpenWeatherMapBundle\Cache\NoCache;
7
use Netgen\Bundle\OpenWeatherMapBundle\Cache\Stash;
8
use Netgen\Bundle\OpenWeatherMapBundle\DependencyInjection\CacheConstraints;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class CacheHandlerFactory implements CacheHandlerFactoryInterface
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $cacheSettings;
17
18
    /**
19
     * @var ContainerInterface
20
     */
21
    protected $container;
22
23
    /**
24
     * CacheHandlerFactory constructor.
25
     *
26
     * @param array $cacheSettings
27
     * @param ContainerInterface $container
28
     */
29
    public function __construct(array $cacheSettings, ContainerInterface $container)
30
    {
31
        $this->cacheSettings = $cacheSettings;
32
        $this->container = $container;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getCacheHandler()
39
    {
40
        switch ($this->cacheSettings['handler']) {
41
            case CacheConstraints::MEMCACHED:
42
                return $this->provideMemcached();
43
44
            case CacheConstraints::STASH:
45
                return $this->provideStash();
46
47
            default:
48
                return new NoCache();
49
        }
50
    }
51
52
    /**
53
     * @throws \Exception
54
     *
55
     * @return MemcachedHandler
56
     *
57
     * @codeCoverageIgnore
58
     */
59
    protected function provideMemcached()
60
    {
61
        if (!class_exists(\Memcached::class)) {
62
            throw new \Exception('Memcached class do not exist, please install memcached php extension');
63
        }
64
65
        $memcached = new \Memcached();
66
        $memcached->addServer($this->cacheSettings['server'], $this->cacheSettings['port']);
67
68
        return new MemcachedHandler($memcached, $this->cacheSettings['ttl']);
69
    }
70
71
    /**
72
     * @return Stash
73
     */
74
    protected function provideStash()
75
    {
76
        $stash = $this->container
77
            ->get('stash');
78
79
        return new Stash($stash, $this->cacheSettings['ttl']);
0 ignored issues
show
Documentation introduced by
$stash is of type object|null, but the function expects a object<Tedivm\StashBundle\Service\CacheService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
    }
81
}
82