Completed
Pull Request — master (#354)
by Sébastien
06:19
created

WidgetCache::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Cache;
4
5
use Predis\Client;
6
use Victoire\Bundle\WidgetBundle\Entity\Widget;
7
8
/**
9
 * This class handle the saving of Widgets.
10
 * Widgets are stored for a week, but are invalidated as soon as
11
 * the Widget's or BusinessEntity's updatedAt field is changed.
12
 */
13
class WidgetCache
14
{
15
    /**
16
     * @var Client
17
     */
18
    private $redis;
19
20
    public function __construct(Client $redis)
21
    {
22
        $this->redis = $redis;
23
    }
24
25
    /**
26
     * @param Widget $widget
27
     *
28
     * @return string
29
     */
30
    public function fetch(Widget $widget)
31
    {
32
        return $this->redis->get($this->getHash($widget));
33
    }
34
35
    /**
36
     * @param Widget $widget
37
     * @param        $content
38
     */
39
    public function save(Widget $widget, $content)
40
    {
41
        $hash = $this->getHash($widget);
42
        if ($hash) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hash of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
43
            $this->redis->set($hash, $content);
44
            $this->redis->expire($hash, 7 * 24 * 60 * 1000); // cache for a week
45
        }
46
    }
47
48
    /**
49
     * @param Widget $widget
50
     *
51
     * @return string
52
     */
53
    protected function getHash(Widget $widget)
54
    {
55
        $hash = null;
56
        if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY
57
            && ($entity = $widget->getEntity())
58
            && method_exists($widget->getEntity(), 'getUpdatedAt')) {
59
            $hash = sprintf('%s-%s--%s-%s',
60
                $widget->getId(),
61
                $widget->getUpdatedAt()->getTimestamp(),
62
                $entity->getId(),
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $entity (of type integer|double).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63
                $entity->getUpdatedAt()->getTimestamp()
0 ignored issues
show
Bug introduced by
The method getUpdatedAt cannot be called on $entity (of type integer|double).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
64
            );
65
        } elseif ($widget->getMode() == Widget::MODE_STATIC) {
66
            $hash = sprintf('%s-%s', $widget->getId(), $widget->getUpdatedAt()->getTimestamp());
67
        }
68
69
        return $hash;
70
    }
71
72
73
}
74