Completed
Push — develop ( 4772a7...141cd6 )
by greg
03:47
created

Mission::removeMissionGames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 2
eloc 4
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 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
            if($missionGame->getGame()->isStarted() && $missionGame->getGame()->isOnline()){
91
                if(!$missionGame->getConditions() || $missionGame->fulfillConditions($entry)){
92
                    $sortedPlayableGames[$missionGame->getPosition()] = $missionGame;
93
                }
94
            }
95
        }
96
97
        return $sortedPlayableGames;
98
    }
99
100
    /**
101
     * Get the next playable game if any
102
     *
103
     * @return \PlaygroundGame\Entity\Game
104
     */
105
    public function getNextPlayableGame($entry=null)
106
    {
107
        $sortedPlayableGames = $this->getPlayableGames($entry);
108
109
        return (count($sortedPlayableGames)>=1)?current($sortedPlayableGames)->getGame():null;
110
    }
111
112
    /**
113
     * Convert the object to an array.
114
     *
115
     * @return array
116
     */
117
    public function getArrayCopy()
118
    {
119
        return get_object_vars($this);
120
    }
121
122
    /**
123
     * Populate from an array.
124
     *
125
     * @param array $data
126
     */
127
    public function populate($data = array())
128
    {
129
130
    }
131
132 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...
133
    {
134
        if (! $this->inputFilter) {
135
            $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...
136
            $factory = new InputFactory();
137
138
            $inputFilter = parent::getInputFilter();
139
            
140
            // This definition is mandatory for the hydration to work in a form !!!!
141
            $inputFilter->add($factory->createInput(array(
142
                'name' => 'missionGames',
143
                'required' => false,
144
            )));
145
            
146
147
            $this->inputFilter = $inputFilter;
148
        }
149
    
150
        return $this->inputFilter;
151
    }
152
}
153