Completed
Pull Request — master (#375)
by Paul
06:22
created

WidgetResolver::resolve()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 4
nop 1
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();
0 ignored issues
show
Unused Code introduced by
$accessor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
        //TODO: orderiaze it
46
        /** @var Widget $widget */
47
        foreach ($widgetMap->getWidgets() as $widget) {
0 ignored issues
show
Bug introduced by
The expression $widgetMap->getWidgets() of type object<Victoire\Bundle\W...etBundle\Entity\Widget> is not traversable.
Loading history...
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