Completed
Pull Request — master (#355)
by Paul
06:37
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
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
    public function __construct(Client $redis, AuthorizationChecker $authorizationChecker)
28
    {
29
        $this->redis = $redis;
30
        $this->authorizationChecker = $authorizationChecker;
31
    }
32
33
    /**
34
     * @param Widget $widget
35
     *
36
     * @return string
37
     */
38
    public function fetch(Widget $widget)
39
    {
40
        return $this->redis->get($this->getHash($widget));
41
    }
42
43
    /**
44
     * @param Widget $widget
45
     * @param        $content
46
     */
47
    public function save(Widget $widget, $content)
48
    {
49
        $hash = $this->getHash($widget);
50
        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...
51
            $this->redis->set($hash, $content);
52
            $this->redis->expire($hash, 7 * 24 * 60 * 1000); // cache for a week
53
        }
54
    }
55
56
    /**
57
     * @param Widget $widget
58
     *
59
     * @return string
60
     */
61
    protected function getHash(Widget $widget)
62
    {
63
        $hash = null;
64
        if (!$widget instanceof WidgetSlotInterface) {
65
            if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY
66
                && ($entity = $widget->getEntity())
67
                && method_exists($widget->getEntity(), 'getUpdatedAt')) {
68
                $hash = $this->generateBusinessEntityHash($widget);
69
            } elseif ($widget->getMode() == Widget::MODE_STATIC) {
70
                $hash = $this->generateHash($widget);
71
            }
72
        }
73
74
        return $hash;
75
    }
76
77
    protected function generateBusinessEntityHash(Widget $widget)
78
    {
79
        return sprintf('%s-%s-%s--%s-%s-%s',
80
            $widget->getId(),
81
            $widget->getUpdatedAt()->getTimestamp(),
82
            $widget->getCurrentView()->getReference()->getId(),
83
            $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...
84
            $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...
85
            (string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')
86
        );
87
    }
88
89
    private function generateHash($widget)
90
    {
91
        return sprintf('%s-%s-%s-%s',
92
            $widget->getId(),
93
            $widget->getUpdatedAt()->getTimestamp(),
94
            $widget->getCurrentView()->getReference()->getId(),
95
            (string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')
96
        );
97
    }
98
99
100
}
101