Trigger   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 71.74%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 205
ccs 33
cts 46
cp 0.7174
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsEnabled() 0 4 1
A setIsEnabled() 0 5 1
A getExpression() 0 4 1
A setState() 0 5 1
A getState() 0 4 1
A getSign() 0 4 1
A setSign() 0 4 1
A getValue() 0 4 1
A setValue() 0 4 1
A getId() 0 4 1
A setId() 0 5 1
A getName() 0 4 1
A setName() 0 5 1
A checkState() 0 13 3
A getVariable() 0 4 1
A setVariable() 0 5 1
A __toString() 0 4 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 1
    public function setIsEnabled($isEnabled)
43
    {
44 1
        $this->isEnabled = $isEnabled;
45 1
        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 1
    public function getExpression()
65
    {
66 1
        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 1
    public function setState($state)
100
    {
101 1
        $this->state = !!$state;
102 1
        return $this;
103
    }
104
105 1
    public function getState()
106
    {
107 1
        return $this->state;
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113 1
    public function getSign()
114
    {
115 1
        return $this->sign;
116
    }
117
118
    /**
119
     * @param mixed $sign
120
     */
121 1
    public function setSign($sign)
122
    {
123 1
        $this->sign = $sign;
124 1
    }
125
126
    /**
127
     * @return mixed
128
     */
129 1
    public function getValue()
130
    {
131 1
        return $this->value;
132
    }
133
134
    /**
135
     * @param mixed $value
136
     */
137 1
    public function setValue($value)
138
    {
139 1
        $this->value = $value;
140 1
    }
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 1
    public function setName($name)
174
    {
175 1
        $this->name = $name;
176 1
        return $this;
177
    }
178
179 1
    public function checkState()
180
    {
181 1
        $value = $this->getVariable()->getValue();
182
183 1
        if (!is_numeric($this->getValue())) {
184
            return false;
185
        }
186 1
        if (!in_array($this->getSign(), $this->allowedOperators)) {
187
            return false;
188
        }
189
190 1
        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 1
    public function getVariable()
197
    {
198 1
        return $this->variable;
199
    }
200
201
    /**
202
     * @param mixed $variable
203
     * @return Trigger
204
     */
205 1
    public function setVariable($variable)
206
    {
207 1
        $this->variable = $variable;
208 1
        return $this;
209
    }
210
211
    public function __toString()
212
    {
213
        return $this->getName().($this->getState() ? ' (on)' : ' (off)');
214
    }
215
}
216