Completed
Pull Request — master (#357)
by Paul
08:47 queued 02:41
created

WidgetCache::generateHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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