Completed
Pull Request — master (#268)
by greg
07:26 queued 04:08
created

MissionGame::findMissionGameByMission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use Zend\ServiceManager\ServiceManagerAwareInterface;
6
use Zend\ServiceManager\ServiceManager;
7
use ZfcBase\EventManager\EventProvider;
8
use PlaygroundGame\Entity\MissionGame as MissionGameEntity;
9
use PlaygroundGame\Entity\MissionGameCondition as MissionGameConditionEntity;
10
11
class MissionGame extends EventProvider implements ServiceManagerAwareInterface
12
{
13
14
    /**
15
    * @var missionMapper
16
    */
17
    protected $missionMapper;
18
     /**
19
    * @var missionGameMapper
20
    */
21
    protected $missionGameMapper;
22
     /**
23
    * @var missionGameConditionMapper
24
    */
25
    protected $missionGameConditionMapper;
26
     /**
27
    * @var gameMapper
28
    */
29
    protected $gameMapper;
30
     /**
31
    * @var options
32
    */
33
    protected $options;
34
35
36
    public function checkGames($dataGames)
37
    {
38
39
        $nbGames = count($dataGames);
40
        for ($i=0; $i < $nbGames; $i++) {
41
            if (!empty($dataGames[$i+1])) {
42
                $game1 = $this->getGameMapper()->findById($dataGames[$i]['games']);
43
                $game2 = $this->getGameMapper()->findById($dataGames[$i+1]['games']);
44
45
                if ($game2->getEndDate() === null) {
46
                    continue;
47
                }
48
49
                // Si la date de fin du jeu 2 est inférieur a la date du jeu 1
50
                if ($game2->getEndDate()->getTimestamp() < $game1->getStartDate()->getTimestamp()) {
51
                    return false;
52
                }
53
            }
54
        }
55
56
        return true;
57
    }
58
59
60
    public function checkGamesInMission($dataGames)
61
    {
62
        $gamesId = array();
63
        $nbGames = count($dataGames);
64
        for ($i=0; $i < $nbGames; $i++) {
65
            $gamesId[] = $dataGames[$i]['games'];
66
        }
67
68
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
69
70
        $query = $em->createQuery('SELECT mg 
71
                                   FROM PlaygroundGame\Entity\MissionGame mg
72
                                   WHERE mg.game IN (:gamesId)');
73
        $query->setParameter('gamesId', $gamesId);
74
        $games = $query->getResult();
75
76
        if (count($games) > 0) {
77
            return false;
78
        }
79
80
        return true;
81
    }
82
    /**
83
    * associate : Permet d'associer des jeux et des conditions à une mission
84
    * @param array $data
85
    * @param Mission $mission
86
    *
87
    * @return MissionGameEntity $missionGameEntity
88
    */
89
    public function associate($data, $mission)
90
    {
91
        $missionGameEntity = new MissionGameEntity();
92
        $game = $this->getGameMapper()->findById($data['games']);
93
        $missionGameEntity->setGame($game);
94
        $missionGameEntity->setPosition($data['position']);
95
        $missionGameEntity->setMission($mission);
96
        $missionGameEntity = $this->getMissionGameMapper()->insert($missionGameEntity);
97
98
        $missionGameConditionEntity = new MissionGameConditionEntity;
99
        $missionGameConditionEntity->setMissionGame($missionGameEntity);
100
        $missionGameConditionEntity->setAttribute($data['conditions']);
101
        $missionGameConditionEntity->setValue($data['points']);
102
        $missionGameConditionEntity = $this->getMissionGameConditionMapper()->insert($missionGameConditionEntity);
0 ignored issues
show
Unused Code introduced by
$missionGameConditionEntity 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...
103
104
        return $missionGameEntity;
105
    }
106
107
    /**
108
    * clear : Permet de supprimer l'association des jeux et des conditions à une mission
109
    * @param Mission $mission
110
    */
111
    public function clear($mission)
112
    {
113
        $missionGames = $this->findMissionGameByMission($mission);
114
        foreach ($missionGames as $missionGames) {
115
            $this->getMissionGameMapper()->remove($missionGames);
116
        }
117
    }
118
119
120 View Code Duplication
    public function checkCondition($game, $winner, $prediction, $entry)
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...
121
    {
122
        $missionGame = $this->findMissionGameByGame($game);
123
        if (empty($missionGame)) {
124
            return false;
125
        }
126
127
        if ($missionGame->getMission()->getActive() === false) {
128
            return false;
129
        }
130
131
        $nextMissionGame = $this->getMissionGameMapper()->getNextGame(
132
            $missionGame->getMission(),
133
            $missionGame->getPosition()
134
        );
135
        
136
        if (empty($nextMissionGame)) {
137
            return false;
138
        }
139
140
        $missionGameConditions = $this->findMissionGameConditionByMissionGame($nextMissionGame);
141
        
142
        if (empty($missionGameConditions)) {
143
            return false;
144
        }
145
146
        foreach ($missionGameConditions as $missionGameCondition) {
147
148
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) {
149
                continue;
150
            }
151
152
            // On passe au suivant si on a gagné
153
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) {
154
                if (!($winner || $prediction)) {
155
                    return false;
156
                }
157
            }
158
159
            // On passe au suivant si on a perdu
160
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) {
161
                if ($winner || $prediction) {
162
                    return false;
163
                }
164
            }
165
166
            // On passe au suivant si on a perdu
167
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) {
168
                if (!$entry) {
169
                    return false;
170
                }
171
                if (!($entry->getPoints() > $missionGameCondition->getValue())) {
172
                    return false;
173
                }
174
            }
175
176
            // On passe au suivant si on a perdu
177
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) {
178
                if (!$entry) {
179
                    return false;
180
                }
181
                if (!($entry->getPoints() < $missionGameCondition->getValue())) {
182
                    return false;
183
                }
184
            }
185
        }
186
187
        return $nextMissionGame->getGame();
188
    }
189
190
    /**
191
    * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission
192
    * @param Mission $mission
193
    *
194
    * @return Collection de MissionGame $missionGames
195
    */
196
    public function findMissionGameByMission($mission)
197
    {
198
        return $this->getMissionGameMapper()->findBy(array('mission'=>$mission));
199
    }
200
201
    /**
202
    * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission
203
    *
204
    * @return Collection de MissionGame $missionGames
205
    */
206
    public function findMissionGameByGame($game)
207
    {
208
        return $this->getMissionGameMapper()->findOneBy(array('game'=>$game));
209
    }
210
211
    /**
212
    * findMissionGameConditionByMissionGame : Permet de recuperer les missionsGameCondition à partir d'une missionGame
213
    * @param MissionGame $missionGame
214
    *
215
    * @return Collection de MissionGameCondition $missionGameConditions
216
    */
217
    public function findMissionGameConditionByMissionGame($missionGame)
218
    {
219
        return $this->getMissionGameConditionMapper()->findBy(array('missionGame'=>$missionGame));
220
    }
221
222
    /**
223
     * Retrieve service manager instance
224
     *
225
     * @return ServiceManager
226
     */
227
    public function getServiceManager()
228
    {
229
        return $this->serviceManager;
0 ignored issues
show
Bug introduced by
The property serviceManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
230
    }
231
232
    /**
233
     * Set service manager instance
234
     *
235
     * @return MissionGame
236
     */
237
    public function setServiceManager(ServiceManager $serviceManager)
238
    {
239
        $this->serviceManager = $serviceManager;
240
241
        return $this;
242
    }
243
244
     /**
245
    * getMissionGameConditionMapper : retrieve missionGameCondition mapper instance
246
    *
247
    * @return Mapper/missionGameCondition $missionGameConditionMapper
0 ignored issues
show
Documentation introduced by
The doc-type Mapper/missionGameCondition could not be parsed: Unknown type name "Mapper/missionGameCondition" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
248
    */
249
    public function getMissionGameConditionMapper()
250
    {
251
        if (null === $this->missionGameConditionMapper) {
252
            $this->missionGameConditionMapper = $this->getServiceManager()->get(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceManager...game_condition_mapper') can also be of type array. However, the property $missionGameConditionMapper is declared as type object<PlaygroundGame\Se...ionGameConditionMapper>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
253
                'playgroundgame_mission_game_condition_mapper'
254
            );
255
        }
256
257
        return $this->missionGameConditionMapper;
258
    }
259
260
    /**
261
    * getMissionGameMapper : retrieve missionGame mapper instance
262
    *
263
    * @return Mapper/MissionGameMapper $missionGameMapper
0 ignored issues
show
Documentation introduced by
The doc-type Mapper/MissionGameMapper could not be parsed: Unknown type name "Mapper/MissionGameMapper" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
264
    */
265 View Code Duplication
    public function getMissionGameMapper()
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...
266
    {
267
        if (null === $this->missionGameMapper) {
268
            $this->missionGameMapper = $this->getServiceManager()->get('playgroundgame_mission_game_mapper');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceManager...e_mission_game_mapper') can also be of type array. However, the property $missionGameMapper is declared as type object<PlaygroundGame\Service\missionGameMapper>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
269
        }
270
271
        return $this->missionGameMapper;
272
    }
273
274
    /**
275
    * getGameMapper : retrieve game mapper instance
276
    *
277
    * @return Mapper/GameMapper $gameMapper
0 ignored issues
show
Documentation introduced by
The doc-type Mapper/GameMapper could not be parsed: Unknown type name "Mapper/GameMapper" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
278
    */
279 View Code Duplication
    public function getGameMapper()
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...
280
    {
281
        if (null === $this->gameMapper) {
282
            $this->gameMapper = $this->getServiceManager()->get('playgroundgame_game_mapper');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceManager...roundgame_game_mapper') can also be of type array. However, the property $gameMapper is declared as type object<PlaygroundGame\Service\gameMapper>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
283
        }
284
285
        return $this->gameMapper;
286
    }
287
}
288