Completed
Push — master ( 9745cb...81e988 )
by Paul
06:43
created

WidgetCache::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Cache;
4
5
use Predis\Client;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
7
use Victoire\Bundle\WidgetBundle\Entity\Widget;
8
use Victoire\Bundle\WidgetBundle\Entity\WidgetSlotInterface;
9
use Victoire\Bundle\WidgetBundle\Helper\WidgetHelper;
10
11
/**
12
 * This class handle the saving of Widgets.
13
 * Widgets are stored for a week, but are invalidated as soon as
14
 * the Widget's or BusinessEntity's updatedAt field is changed.
15
 */
16
class WidgetCache
17
{
18
    /**
19
     * @var Client
20
     */
21
    private $redis;
22
    /**
23
     * @var AuthorizationChecker
24
     */
25
    private $authorizationChecker;
26
    /**
27
     * @var WidgetHelper
28
     */
29
    private $widgetHelper;
30
31
    /**
32
     * WidgetCache constructor.
33
     *
34
     * @param Client               $redis
35
     * @param AuthorizationChecker $authorizationChecker
36
     * @param WidgetHelper         $widgetHelper
37
     */
38
    public function __construct(Client $redis, AuthorizationChecker $authorizationChecker, WidgetHelper $widgetHelper)
39
    {
40
        $this->redis = $redis;
41
        $this->authorizationChecker = $authorizationChecker;
42
        $this->widgetHelper = $widgetHelper;
43
    }
44
45
    /**
46
     * @param Widget $widget
47
     *
48
     * @return string
49
     */
50
    public function fetch(Widget $widget)
51
    {
52
        return $this->redis->get($this->getHash($widget));
53
    }
54
55
    /**
56
     * @param Widget $widget
57
     *
58
     * @return int
59
     */
60
    public function remove(Widget $widget)
61
    {
62
        return $this->redis->del($this->getHash($widget));
63
    }
64
65
    /**
66
     * @param Widget $widget
67
     * @param        $content
68
     */
69
    public function save(Widget $widget, $content)
70
    {
71
        $hash = $this->getHash($widget);
72
        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...
73
            $this->redis->set($hash, $content);
74
            $this->redis->expire($hash, $this->widgetHelper->getCacheTimeout($widget)); // cache for a week
75
        }
76
    }
77
78
    /**
79
     * clear all redis cache.
80
     */
81
    public function clear()
82
    {
83
        $this->redis->executeCommand($this->redis->createCommand('FLUSHALL'));
84
    }
85
86
    /**
87
     * @param Widget $widget
88
     *
89
     * @return string
90
     */
91
    protected function getHash(Widget $widget)
92
    {
93
        $hash = null;
94
        if (!$widget instanceof WidgetSlotInterface) {
95
            if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY
96
                && ($entity = $widget->getEntity())
97
                && method_exists($widget->getEntity(), 'getUpdatedAt')) {
98
                $hash = $this->generateBusinessEntityHash($widget);
99
            } elseif ($widget->getMode() == Widget::MODE_STATIC) {
100
                $hash = $this->generateHash($widget);
101
            }
102
        }
103
104
        return $hash;
105
    }
106
107
    protected function generateBusinessEntityHash(Widget $widget)
108
    {
109
        return sprintf('%s--%s-%s-%s',
110
            $widget->generateCacheId(),
111
            $widget->getEntity()->getId(),
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $widget->getEntity() (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...
112
            $widget->getEntity()->getUpdatedAt()->getTimestamp(),
0 ignored issues
show
Bug introduced by
The method getUpdatedAt cannot be called on $widget->getEntity() (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...
113
            (string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')
114
        );
115
    }
116
117
    private function generateHash(Widget $widget)
118
    {
119
        return sprintf('%s-%s',
120
            $widget->generateCacheId(),
121
            (string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')
122
        );
123
    }
124
}
125