Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

WidgetResolver::resolve()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 8.6737
c 2
b 0
f 0
cc 6
eloc 10
nc 8
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: paulandrieux
5
 * Date: 17/03/2016
6
 * Time: 17:28.
7
 */
8
namespace Victoire\Bundle\WidgetBundle\Resolver;
9
10
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
11
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
12
use Victoire\Bundle\CoreBundle\Helper\CurrentViewHelper;
13
use Victoire\Bundle\CriteriaBundle\Chain\DataSourceChain;
14
use Victoire\Bundle\CriteriaBundle\Entity\Criteria;
15
use Victoire\Bundle\WidgetBundle\Entity\Widget;
16
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
17
18
class WidgetResolver
19
{
20
    const OPERAND_EQUAL = 'equal';
21
    const OPERAND_TRUE = 'true';
22
    const OPERAND_FALSE = 'false';
23
    const OPERAND_IN = 'in';
24
    const IS_GRANTED = 'is_granted';
25
    const IS_NOT_GRANTED = 'is_not_granted';
26
27
    /**
28
     * @var DataSourceChain
29
     */
30
    private $dataSourceChain;
31
32
    private $authorizationChecker;
33
    /**
34
     * @var CurrentViewHelper
35
     */
36
    private $currentViewHelper;
37
38
    /**
39
     * WidgetResolver constructor.
40
     *
41
     * @param DataSourceChain      $dataSourceChain
42
     * @param AuthorizationChecker $authorizationChecker
43
     * @param CurrentViewHelper    $currentViewHelper
44
     */
45
    public function __construct(DataSourceChain $dataSourceChain, AuthorizationChecker $authorizationChecker, CurrentViewHelper $currentViewHelper)
46
    {
47
        $this->dataSourceChain = $dataSourceChain;
48
        $this->authorizationChecker = $authorizationChecker;
49
        $this->currentViewHelper = $currentViewHelper;
50
    }
51
52
    public function resolve(WidgetMap $widgetMap)
53
    {
54
        //TODO: orderize it
55
56
        $widgets = $widgetMap->getWidgets();
57
        // if the widgetmap is linked to no widgets, it seems that it is an overwrite of the position so keep the replaced widgets for display
58
        if ($widgetMap->getReplaced() && count($widgets) === 0) {
59
            $widgets = $widgetMap->getReplaced()->getWidgets();
60
        }
61
        /* @var Widget $widget */
62
        foreach ($widgets as $_widget) {
63
            /** @var Criteria $criteria */
64
            foreach ($_widget->getCriterias() as $criteria) {
65
                $value = $this->dataSourceChain->getData($criteria->getName());
66
                if (!$this->assert($value(), $criteria->getOperator(), $criteria->getValue())) {
67
                    continue 2; //try with break
68
                }
69
            }
70
71
            return $_widget;
72
        }
73
    }
74
75
    protected function assert($value, $operator, $expected)
76
    {
77
        $businessEntity = null;
78
        if ($this->currentViewHelper->getCurrentView() instanceof BusinessPage) {
79
            $businessEntity = $this->currentViewHelper->getCurrentView()->getBusinessEntity();
80
        }
81
        $result = false;
82
        switch ($operator) {
83
            case self::OPERAND_EQUAL:
84
                $result = $value === $expected;
85
                break;
86
            case self::OPERAND_TRUE:
87
                $result = $value == true;
88
                break;
89
            case self::OPERAND_FALSE:
90
                $result = $value == false;
91
                break;
92
            case self::OPERAND_IN:
93
                $result = in_array($value, unserialize($expected));
94
                break;
95
            case self::IS_GRANTED:
96
            case self::IS_NOT_GRANTED:
97
                if (!$this->authorizationChecker->isGranted('ROLE_VICTOIRE')) {
98
                    $granted = $this->authorizationChecker->isGranted($expected, $businessEntity);
99
                    if ($operator === self::IS_GRANTED) {
100
                        $result = $granted;
101
                    } else {
102
                        $result = (false === $granted);
103
                    }
104
                } else {
105
                    $result = true;
106
                }
107
                break;
108
        }
109
110
        return $result;
111
    }
112
}
113