Completed
Pull Request — master (#269)
by greg
03:09
created

Mission::isPlayable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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