Completed
Push — master ( 82203b...8e6d72 )
by Kirill
17:25
created

Trigger::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
7
/**
8
 * @ORM\Entity
9
 * @ORM\Table(name="triggers")
10
 */
11
class Trigger
12
{
13
14
    public $allowedOperators = [
15
        '>','<','>=','<=','==','!='
16
    ];
17
18
    /**
19
     * @ORM\Column(type="integer")
20
     * @ORM\Id
21
     * @ORM\GeneratedValue(strategy="AUTO")
22
     */
23
    private $id;
24
25
    /**
26
     * @ORM\Column(type="string", length=100)
27
     */
28
    private $name;
29
30
    /**
31
     * @ORM\Column(type="string", length = 2)
32
     */
33
    private $sign;
34
35
    /**
36
     * @ORM\Column(type="string", length = 100)
37
     */
38
    private $value;
39
40
41
    public function getExpression()
42
    {
43
        return $this->sign.''.$this->value;
44
    }
45
46
    /**
47
     * @ORM\Column(type="boolean")
48
     */
49
    private $state = false;
50
51
    /**
52
     * @var Variable
53
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Variable", inversedBy="triggers")
54
     */
55
    public $variable;
56
57
    /**
58
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Action", inversedBy="activateTriggers")
59
     */
60
    public $onActivate;
61
62
    /**
63
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Action", inversedBy="deactivateTriggers")
64
     */
65
    public $onDeactivate;
66
67
    /** @ORM\Column(type="text") */
68
    public $activateParams;
69
    /** @ORM\Column(type="text") */
70
    public $deactivateParams;
71
72
    /**
73
     * @param $state boolean
74
     * @return Trigger
75
     */
76
    public function setState($state)
77
    {
78
        $this->state = !!$state;
79
        return $this;
80
    }
81
82
    public function getState()
83
    {
84
        return $this->state;
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90
    public function getSign()
91
    {
92
        return $this->sign;
93
    }
94
95
    /**
96
     * @param mixed $sign
97
     */
98
    public function setSign($sign)
99
    {
100
        $this->sign = $sign;
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106
    public function getValue()
107
    {
108
        return $this->value;
109
    }
110
111
    /**
112
     * @param mixed $value
113
     */
114
    public function setValue($value)
115
    {
116
        $this->value = $value;
117
    }
118
119
120
    /**
121
     * @return mixed
122
     */
123
    public function getId()
124
    {
125
        return $this->id;
126
    }
127
128
    /**
129
     * @param mixed $id
130
     * @return Trigger
131
     */
132
    public function setId($id)
133
    {
134
        $this->id = $id;
135
        return $this;
136
    }
137
138
    /**
139
     * @return mixed
140
     */
141
    public function getName()
142
    {
143
        return $this->name;
144
    }
145
146
    /**
147
     * @param string $name
148
     * @return Trigger
149
     */
150
    public function setName($name)
151
    {
152
        $this->name = $name;
153
        return $this;
154
    }
155
156
    public function checkState()
157
    {
158
        $value = $this->getVariable()->getValue();
159
160
        if (!is_numeric($this->getValue())) {
161
            return false;
162
        }
163
        if (!in_array($this->getSign(), $this->allowedOperators)) {
164
            return false;
165
        }
166
167
        return (bool) eval('return '.$value.' '.$this->getExpression().';');
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
168
    }
169
170
    /**
171
     * @return Variable
172
     */
173
    public function getVariable()
174
    {
175
        return $this->variable;
176
    }
177
178
    /**
179
     * @param mixed $variable
180
     * @return Trigger
181
     */
182
    public function setVariable($variable)
183
    {
184
        $this->variable = $variable;
185
        return $this;
186
    }
187
188
    public function __toString()
189
    {
190
        return $this->getName(). ($this->getState() ? ' (on)':' (off)');
191
    }
192
}
193