Cache::getEvents()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
/*
4
 * This file is part of CalendR, a Fréquence web project.
5
 *
6
 * (c) 2012 Fréquence web
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CalendR\Event\Provider;
13
14
use Doctrine\Common\Cache\Cache as CacheInterface;
15
16
/**
17
 * Cache the result of a provider.
18
 *
19
 * @author Yohan Giarelli <[email protected]>
20
 */
21
class Cache implements ProviderInterface
22
{
23
    /**
24
     * @var CacheInterface
25
     */
26
    protected $cache;
27
28
    /**
29
     * @var ProviderInterface
30
     */
31
    protected $provider;
32
33
    /**
34
     * @var int
35
     */
36
    protected $lifetime;
37
38
    /**
39
     * @var string
40
     */
41
    protected $namespace;
42
43
    /**
44
     * @param CacheInterface    $cache
45
     * @param ProviderInterface $provider
46
     * @param int               $lifetime
47
     * @param string            $namespace
0 ignored issues
show
Documentation introduced by
Should the type for parameter $namespace not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
48
     */
49
    public function __construct(CacheInterface $cache, ProviderInterface $provider, $lifetime, $namespace = null)
50
    {
51
        $this->cache = $cache;
52
        $this->provider = $provider;
53
        $this->lifetime = $lifetime;
54
        $this->namespace = $namespace;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getEvents(\DateTime $begin, \DateTime $end, array $options = array())
61
    {
62
        $cacheKey = md5(serialize(array($begin, $end, $options)));
63
64
        if (null !== $this->namespace) {
65
            $cacheKey = $this->namespace.'.'.$cacheKey;
66
        }
67
68
        if ($this->cache->contains($cacheKey)) {
69
            return $this->cache->fetch($cacheKey);
70
        }
71
72
        $events = $this->provider->getEvents($begin, $end, $options);
73
        $this->cache->save($cacheKey, $events, $this->lifetime);
74
75
        return $events;
76
    }
77
}
78