MissionGame::addConditions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace PlaygroundGame\Entity;
3
4
use Doctrine\Common\Collections\ArrayCollection;
5
use Doctrine\ORM\Mapping as ORM;
6
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
7
use Doctrine\ORM\Mapping\PrePersist;
8
use Doctrine\ORM\Mapping\PreUpdate;
9
use Zend\InputFilter\InputFilter;
10
use Zend\InputFilter\InputFilterAwareInterface;
11
use Zend\InputFilter\InputFilterInterface;
12
use Zend\InputFilter\Factory as InputFactory;
13
14
/**
15
 * @ORM\Entity @HasLifecycleCallbacks
16
 * @ORM\Table(name="game_mission_game")
17
 */
18
class MissionGame implements InputFilterAwareInterface
19
{
20
    protected $inputFilter;
21
    
22
    /**
23
     * @ORM\Id
24
     * @ORM\Column(type="integer");
25
     * @ORM\GeneratedValue(strategy="AUTO")
26
     */
27
    protected $id;
28
29
    /**
30
    * @ORM\ManyToOne(targetEntity="PlaygroundGame\Entity\Game")
31
    * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE")
32
    **/
33
    protected $game;
34
35
    /**
36
     * @ORM\ManyToOne(targetEntity="Mission", inversedBy="missionGames")
37
     * @ORM\JoinColumn(name="mission_id", referencedColumnName="id", onDelete="CASCADE")
38
     *
39
     **/
40
    protected $mission;
41
    
42
    /**
43
     * @ORM\OneToMany(targetEntity="MissionGameCondition", mappedBy="missionGame", cascade={"persist","remove"})
44
     */
45
    protected $conditions;
46
47
    /**
48
     * position
49
     * @ORM\Column(name="position", type="integer", nullable=false)
50
     */
51
    protected $position;
52
53
    /**
54
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
55
     */
56
    protected $createdAt;
57
58
    /**
59
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
60
     */
61
    protected $updatedAt;
62
63
    /**
64
     * Constructor
65
     */
66
    public function __construct()
67
    {
68
        $this->conditions = new ArrayCollection();
69
    }
70
71
    public function __clone()
72
    {
73
        $this->conditions = new ArrayCollection();
74
    }
75
76
    /** @PrePersist */
77
    public function createChrono()
78
    {
79
        $this->createdAt = new \DateTime("now");
80
        $this->updatedAt = new \DateTime("now");
81
    }
82
83
    /** @PreUpdate */
84
    public function updateChrono()
85
    {
86
        $this->updatedAt = new \DateTime("now");
87
    }
88
89
    /**
90
     * @param $id
91
     * @return MissionGame
92
     */
93
    public function setId($id)
94
    {
95
        $this->id = (int) $id;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return integer
102
     */
103
    public function getId()
104
    {
105
        return $this->id;
106
    }
107
108
    /**
109
    * @param $id
110
    * @return MissionGame
111
    */
112
    public function setPosition($position)
113
    {
114
        $this->position = (int) $position;
115
116
        return $this;
117
    }
118
119
    /**
120
     * @return integer
121
     */
122
    public function getPosition()
123
    {
124
        return $this->position;
125
    }
126
127
    /**
128
     * @return the $game
129
     */
130
    public function getGame()
131
    {
132
        return $this->game;
133
    }
134
135
    /**
136
     * @param field_type $game
137
     */
138
    public function setGame($game)
139
    {
140
        $this->game = $game;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return \PlaygroundGame\Service\Mission $game
147
     */
148
    public function getMission()
149
    {
150
        return $this->mission;
151
    }
152
153
    /**
154
     * @param \PlaygroundGame\Service\Mission $mission
155
     */
156
    public function setMission($mission)
157
    {
158
        $this->mission = $mission;
159
160
        return $this;
161
    }
162
  
163
    /**
164
     * @return the $conditions
165
     */
166
    public function getConditions()
167
    {
168
        return $this->conditions;
169
    }
170
171
    /**
172
     * @param field_type $conditions
173
     */
174
    public function setConditions($conditions)
175
    {
176
        $this->conditions = $conditions;
177
        
178
        return $this;
179
    }
180
    
181
    public function addConditions(ArrayCollection $conditions)
182
    {
183
        foreach ($conditions as $condition) {
184
            $condition->setMissionGame($this);
185
            $this->conditions->add($condition);
186
        }
187
    }
188
    
189
    public function removeConditions(ArrayCollection $conditions)
190
    {
191
        foreach ($conditions as $condition) {
192
            $this->conditions->removeElement($condition);
193
        }
194
    }
195
    
196
    /**
197
     * Add a condition to the mission game.
198
     *
199
     * @param MisionGameCondition $condition
200
     *
201
     * @return void
202
     */
203
    public function addCondition($condition)
204
    {
205
        $this->conditions[] = $condition;
206
    }
207
208
    /**
209
     *
210
     * @return \DateTime $createdAt
211
     */
212
    public function getCreatedAt()
213
    {
214
        return $this->createdAt;
215
    }
216
217
    /**
218
     *
219
     * @param \DateTime $createdAt
220
     */
221
    public function setCreatedAt($createdAt)
222
    {
223
        $this->createdAt = $createdAt;
224
        
225
        return $this;
226
    }
227
228
    /**
229
     *
230
     * @return \DateTime $updatedAt
231
     */
232
    public function getUpdatedAt()
233
    {
234
        return $this->updatedAt;
235
    }
236
237
    /**
238
     *
239
     * @param \DateTime $updatedAt
240
     */
241
    public function setUpdatedAt($updatedAt)
242
    {
243
        $this->updatedAt = $updatedAt;
244
        
245
        return $this;
246
    }
247
248
    /**
249
     * Are the conditions linked to this game fulfilled ?
250
     *
251
     * @return boolean
252
     */
253
    public function fulfillConditions($entry = null)
254
    {
255
        foreach ($this->getConditions() as $condition) {
256
            if ($condition->getAttribute() == MissionGameCondition::NONE) {
257
                continue;
258
            }
259
    
260
            // On passe au suivant si on a gagné
261
            if ($condition->getAttribute() == MissionGameCondition::VICTORY) {
262
                if (!$entry || !$entry->getWinner()) {
263
                    return false;
264
                }
265
            }
266
    
267
            // On passe au suivant si on a perdu
268
            if ($condition->getAttribute() == MissionGameCondition::DEFEAT) {
269
                if (!$entry || $entry->getWinner()) {
270
                    return false;
271
                }
272
            }
273
    
274
            // On passe au suivant si on a plus de n points
275
            if ($condition->getAttribute() == MissionGameCondition::GREATER) {
276
                if (!$entry || !($entry->getPoints() >= $condition->getValue())) {
277
                    return false;
278
                }
279
            }
280
    
281
            // On passe au suivant si on a moins de n points
282
            if ($condition->getAttribute() == MissionGameCondition::LESS) {
283
                if (!$entry || !($entry->getPoints() < $condition->getValue())) {
284
                    return false;
285
                }
286
            }
287
        }
288
289
        return true;
290
    }
291
292
    public function setInputFilter(InputFilterInterface $inputFilter)
293
    {
294
        throw new \Exception("Not used");
295
    }
296
297 View Code Duplication
    public function getInputFilter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298
    {
299
        if (! $this->inputFilter) {
300
            $inputFilter = new InputFilter();
301
            $factory = new InputFactory();
302
303
            $inputFilter->add($factory->createInput(array(
304
                'name' => 'game',
305
                'required' => false,
306
            )));
307
            $this->inputFilter = $inputFilter;
308
        }
309
    
310
        return $this->inputFilter;
311
    }
312
}
313