Completed
Push — master ( 8e6d72...13d1e3 )
by Kirill
04:06
created

Trigger::setIsEnabled()   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
     * @return boolean
32
     */
33
    public function getIsEnabled()
34
    {
35
        return $this->isEnabled;
36
    }
37
38
    /**
39
     * @param boolean $isEnabled
40
     * @return Trigger
41
     */
42
    public function setIsEnabled($isEnabled)
43
    {
44
        $this->isEnabled = $isEnabled;
45
        return $this;
46
    }
47
48
    /**
49
     * @ORM\Column(type="boolean")
50
     */
51
    private $isEnabled = true;
52
53
    /**
54
     * @ORM\Column(type="string", length = 2)
55
     */
56
    private $sign;
57
58
    /**
59
     * @ORM\Column(type="string", length = 100)
60
     */
61
    private $value;
62
63
64
    public function getExpression()
65
    {
66
        return $this->sign.''.$this->value;
67
    }
68
69
    /**
70
     * @ORM\Column(type="boolean")
71
     */
72
    private $state = false;
73
74
    /**
75
     * @var Variable
76
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Variable", inversedBy="triggers")
77
     */
78
    public $variable;
79
80
    /**
81
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Action", inversedBy="activateTriggers")
82
     */
83
    public $onActivate;
84
85
    /**
86
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Action", inversedBy="deactivateTriggers")
87
     */
88
    public $onDeactivate;
89
90
    /** @ORM\Column(type="text") */
91
    public $activateParams;
92
    /** @ORM\Column(type="text") */
93
    public $deactivateParams;
94
95
    /**
96
     * @param $state boolean
97
     * @return Trigger
98
     */
99
    public function setState($state)
100
    {
101
        $this->state = !!$state;
102
        return $this;
103
    }
104
105
    public function getState()
106
    {
107
        return $this->state;
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113
    public function getSign()
114
    {
115
        return $this->sign;
116
    }
117
118
    /**
119
     * @param mixed $sign
120
     */
121
    public function setSign($sign)
122
    {
123
        $this->sign = $sign;
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    public function getValue()
130
    {
131
        return $this->value;
132
    }
133
134
    /**
135
     * @param mixed $value
136
     */
137
    public function setValue($value)
138
    {
139
        $this->value = $value;
140
    }
141
142
143
    /**
144
     * @return mixed
145
     */
146
    public function getId()
147
    {
148
        return $this->id;
149
    }
150
151
    /**
152
     * @param mixed $id
153
     * @return Trigger
154
     */
155
    public function setId($id)
156
    {
157
        $this->id = $id;
158
        return $this;
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164
    public function getName()
165
    {
166
        return $this->name;
167
    }
168
169
    /**
170
     * @param string $name
171
     * @return Trigger
172
     */
173
    public function setName($name)
174
    {
175
        $this->name = $name;
176
        return $this;
177
    }
178
179
    public function checkState()
180
    {
181
        $value = $this->getVariable()->getValue();
182
183
        if (!is_numeric($this->getValue())) {
184
            return false;
185
        }
186
        if (!in_array($this->getSign(), $this->allowedOperators)) {
187
            return false;
188
        }
189
190
        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...
191
    }
192
193
    /**
194
     * @return Variable
195
     */
196
    public function getVariable()
197
    {
198
        return $this->variable;
199
    }
200
201
    /**
202
     * @param mixed $variable
203
     * @return Trigger
204
     */
205
    public function setVariable($variable)
206
    {
207
        $this->variable = $variable;
208
        return $this;
209
    }
210
211
    public function __toString()
212
    {
213
        return $this->getName(). ($this->getState() ? ' (on)':' (off)');
214
    }
215
}
216