Completed
Push — master ( 989cd3...c05280 )
by greg
167:19 queued 125:26
created

Mission::addMissionGames()   A

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 PlaygroundGame\Entity\Game;
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
8
use Zend\InputFilter\InputFilter;
9
use Zend\InputFilter\Factory as InputFactory;
10
use Zend\InputFilter\InputFilterAwareInterface;
11
12
/**
13
 * @ORM\Entity @HasLifecycleCallbacks
14
 * @ORM\Table(name="game_mission")
15
 */
16
class Mission extends Game implements InputFilterAwareInterface
17
{
18
    const CLASSTYPE = 'mission';
19
    
20
    /**
21
     * @ORM\OneToMany(targetEntity="MissionGame", mappedBy="mission", cascade={"persist","remove"}, orphanRemoval=true)
22
     */
23
    protected $missionGames;
24
25
    public function __construct()
26
    {
27
        parent::__construct();
28
        $this->setClassType(self::CLASSTYPE);
29
        $this->missionGames = new ArrayCollection();
30
    }
31
32
    /**
33
    * @return the $missionGames
34
    */
35
    public function getMissionGames()
36
    {
37
        return $this->missionGames;
38
    }
39
40
    /**
41
     * @param \PlaygroundGame\Entity\ArrayCollection $missionGames
42
     */
43
    public function setMissionGames($missionGames)
44
    {
45
        $this->missionGames = $missionGames;
46
        
47
        return $this;
48
    }
49
    
50
    public function addMissionGames(ArrayCollection $missionGames)
51
    {
52
        foreach ($missionGames as $missionGame) {
53
            $missionGame->setMission($this);
54
            $this->missionGames->add($missionGame);
55
        }
56
    }
57
    
58
    public function removeMissionGames(ArrayCollection $missionGames)
59
    {
60
        foreach ($missionGames as $missionGame) {
61
            $missionGame->setMission(null);
62
            $this->missionGames->removeElement($missionGame);
63
        }
64
    }
65
    
66
    /**
67
     * Add a game to the mission.
68
     *
69
     * @param MissionGame $missionGame
70
     *
71
     * @return void
72
     */
73
    public function addMissionGame($missionGame)
74
    {
75
        $this->missionGames[] = $missionGame;
76
    }
77
78
    /**
79
     * Get the playables game if any
80
     *
81
     * @return array
82
     */
83
    public function getPlayableGames($entry = null)
84
    {
85
        $sortedPlayableGames = array();
86
        foreach ($this->missionGames as $missionGame) {
87
            $g = $missionGame->getGame();
88
            
89
            if ($g->isStarted() && $g->isOnline()) {
90
                if (!$missionGame->getConditions() || $missionGame->fulfillConditions($entry)) {
91
                    $sortedPlayableGames[$missionGame->getPosition()] = $missionGame;
92
                }
93
            }
94
        }
95
96
        return $sortedPlayableGames;
97
    }
98
99
    /**
100
     * Get the previous playable game if any
101
     *
102
     * @return \PlaygroundGame\Entity\Game
103
     */
104
    public function getPreviousGame($entry = null)
105
    {
106
        $currentGame = $this->getNextPlayableGame($entry);
107
        $previousMissionGame = null;
108
109
        foreach ($this->missionGames as $missionGame) {
110
            if($missionGame->getGame()->getId() === $currentGame->getId()) {
111
                return $previousMissionGame;
112
            }
113
114
            $previousMissionGame = $missionGame->getGame();
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * Get the next playable game if any
122
     *
123
     * @return \PlaygroundGame\Entity\Game
124
     */
125
    public function getNextPlayableGame($entry = null)
126
    {
127
        $sortedPlayableGames = $this->getPlayableGames($entry);
128
129
        return (count($sortedPlayableGames)>=1)?current($sortedPlayableGames)->getGame():null;
130
    }
131
132
    /**
133
     * is this game playable ?
134
     *
135
     * @return boolean
136
     */
137
    public function isPlayable($subGame, $entry = null)
138
    {
139
        if (!$subGame) {
140
            return false;
141
        }
142
        
143
        $sortedPlayableGames = $this->getPlayableGames($entry);
144
        foreach ($sortedPlayableGames as $pgame) {
145
            if ($subGame->getIdentifier() === $pgame->getGame()->getIdentifier()) {
146
                return true;
147
            }
148
        }
149
150
        return false;
151
    }
152
153
    /**
154
     * Convert the object to an array.
155
     *
156
     * @return array
157
     */
158
    public function getArrayCopy()
159
    {
160
        return get_object_vars($this);
161
    }
162
163
    /**
164
     * Populate from an array.
165
     *
166
     * @param array $data
167
     */
168
    public function populate($data = array())
169
    {
170
    }
171
172 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...
173
    {
174
        if (! $this->inputFilter) {
175
            $inputFilter = new InputFilter();
0 ignored issues
show
Unused Code introduced by
$inputFilter 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...
176
            $factory = new InputFactory();
177
178
            $inputFilter = parent::getInputFilter();
179
            
180
            // This definition is mandatory for the hydration to work in a form !!!!
181
            $inputFilter->add($factory->createInput(array(
182
                'name' => 'missionGames',
183
                'required' => false,
184
            )));
185
            
186
187
            $this->inputFilter = $inputFilter;
188
        }
189
    
190
        return $this->inputFilter;
191
    }
192
}
193