|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: paulandrieux |
|
5
|
|
|
* Date: 17/03/2016 |
|
6
|
|
|
* Time: 17:28 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Victoire\Bundle\WidgetBundle\Resolver; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
13
|
|
|
use Victoire\Bundle\CriteriaBundle\Chain\CriteriaChain; |
|
14
|
|
|
use Victoire\Bundle\CriteriaBundle\Chain\DataSourceChain; |
|
15
|
|
|
use Victoire\Bundle\CriteriaBundle\Entity\Criteria; |
|
16
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
|
17
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Widget; |
|
18
|
|
|
|
|
19
|
|
|
class WidgetResolver |
|
20
|
|
|
{ |
|
21
|
|
|
const OPERAND_EQUAL = "equal"; |
|
22
|
|
|
const OPERAND_IN = "in"; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var DataSourceChain |
|
26
|
|
|
*/ |
|
27
|
|
|
private $dataSourceChain; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* WidgetResolver constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param DataSourceChain $dataSourceChain |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(DataSourceChain $dataSourceChain) |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
$this->dataSourceChain = $dataSourceChain; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function resolve(WidgetMap $widgetMap) |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
$widget = null; |
|
44
|
|
|
$accessor = new PropertyAccessor(); |
|
|
|
|
|
|
45
|
|
|
//TODO: orderiaze it |
|
46
|
|
|
/** @var Widget $widget */ |
|
47
|
|
|
foreach ($widgetMap->getWidgets() as $widget) { |
|
|
|
|
|
|
48
|
|
|
/** @var Criteria $criteria */ |
|
49
|
|
|
foreach ($widget->getCriterias() as $criteria) { |
|
50
|
|
|
$value = $this->dataSourceChain->getData($criteria->getName()); |
|
51
|
|
|
if ($this->assert($value(), $criteria->getOperator(), $criteria->getValue())) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} else { |
|
54
|
|
|
continue 2; //try with break |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return $widget; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $widget; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function assert($value, $operator, $expected) |
|
64
|
|
|
{ |
|
65
|
|
|
$result = false; |
|
66
|
|
|
switch ($operator) { |
|
67
|
|
|
case self::OPERAND_EQUAL: |
|
68
|
|
|
$result = $value === $expected; |
|
69
|
|
|
break; |
|
70
|
|
|
case self::OPERAND_IN: |
|
71
|
|
|
$result = in_array($value, unserialize($expected)); |
|
72
|
|
|
break; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.