Completed
Push — develop ( 30c481...efed9c )
by greg
03:20
created

Mission::setMissionGames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
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 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) return false;
121
        
122
        $sortedPlayableGames = $this->getPlayableGames($entry);
123
        foreach($sortedPlayableGames as $pgame){
124
            if($subGame->getIdentifier() === $pgame->getGame()->getIdentifier())
125
                return true;
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * Convert the object to an array.
133
     *
134
     * @return array
135
     */
136
    public function getArrayCopy()
137
    {
138
        return get_object_vars($this);
139
    }
140
141
    /**
142
     * Populate from an array.
143
     *
144
     * @param array $data
145
     */
146
    public function populate($data = array())
147
    {
148
149
    }
150
151 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...
152
    {
153
        if (! $this->inputFilter) {
154
            $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...
155
            $factory = new InputFactory();
156
157
            $inputFilter = parent::getInputFilter();
158
            
159
            // This definition is mandatory for the hydration to work in a form !!!!
160
            $inputFilter->add($factory->createInput(array(
161
                'name' => 'missionGames',
162
                'required' => false,
163
            )));
164
            
165
166
            $this->inputFilter = $inputFilter;
167
        }
168
    
169
        return $this->inputFilter;
170
    }
171
}
172