Completed
Push — develop ( 29e92f...f35b7e )
by greg
05:15
created

MissionGame::getServiceManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
        );
74
        $query->setParameter('gamesId', $gamesId);
75
        $games = $query->getResult();
76
77
        if(count($games) > 0) {
78
            return false;
79
        }
80
81
        return true;
82
    }
83
    /**
84
    * associate : Permet d'associer des jeux et des conditions à une mission
85
    * @param array $data 
86
    * @param Mission $mission
87
    *
88
    * @return MissionGameEntity $missionGameEntity
89
    */
90
    public function associate($data, $mission)
91
    {
92
        $missionGameEntity = new MissionGameEntity();
93
        $game = $this->getGameMapper()->findById($data['games']);
94
        $missionGameEntity->setGame($game);
95
        $missionGameEntity->setPosition($data['position']);
96
        $missionGameEntity->setMission($mission);
97
        $missionGameEntity = $this->getMissionGameMapper()->insert($missionGameEntity); 
98
99
        $missionGameConditionEntity = new MissionGameConditionEntity;
100
        $missionGameConditionEntity->setMissionGame($missionGameEntity);
101
        $missionGameConditionEntity->setAttribute($data['conditions']);
102
        $missionGameConditionEntity->setValue($data['points']);
103
        $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...
104
105
        return $missionGameEntity;
106
    }
107
108
    /**
109
    * clear : Permet de supprimer l'association des jeux et des conditions à une mission
110
    * @param Mission $mission
111
    */
112
    public function clear($mission)
113
    {
114
        $missionGames = $this->findMissionGameByMission($mission);
115
        foreach ($missionGames as $missionGames) {
116
            $this->getMissionGameMapper()->remove($missionGames); 
117
        }
118
    }
119
120
121 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...
122
    {
123
        $missionGame = $this->findMissionGameByGame($game);
124
        if(empty($missionGame)){
125
            return false;
126
        }
127
128
        if($missionGame->getMission()->getActive() === false){
129
            return false;
130
        }
131
132
        $nextMissionGame = $this->getMissionGameMapper()->getNextGame($missionGame->getMission(), $missionGame->getPosition());
133
        
134
        if(empty($nextMissionGame)){
135
            return false;
136
        }
137
138
        $missionGameConditions = $this->findMissionGameConditionByMissionGame($nextMissionGame);
139
        
140
        if(empty($missionGameConditions)){
141
            return false;
142
        }
143
144
        foreach ($missionGameConditions as $missionGameCondition) {
145
146
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) {
147
                continue;
148
            }
149
150
            // On passe au suivant si on a gagné
151
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) {
152
                if(!($winner || $prediction)){
153
                    return false;
154
                }
155
            }
156
157
            // On passe au suivant si on a perdu
158
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) {
159
                if($winner || $prediction){
160
                    return false;
161
                }
162
            }
163
164
            // On passe au suivant si on a perdu
165
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) {
166
                if (!$entry) {
167
                    return false;
168
                }
169
                if(!($entry->getPoints() > $missionGameCondition->getValue())){
170
                    return false;
171
                }
172
            }
173
174
            // On passe au suivant si on a perdu
175
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) {
176
                if (!$entry) {
177
                    return false;
178
                }
179
                if(!($entry->getPoints() < $missionGameCondition->getValue())){
180
                    return false;
181
                }
182
            }
183
        }
184
185
        return $nextMissionGame->getGame();
186
    }
187
188
    /**
189
    * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission
190
    * @param Mission $mission
191
    *
192
    * @return Collection de MissionGame $missionGames
193
    */
194
    public function findMissionGameByMission($mission){
195
        return $this->getMissionGameMapper()->findBy(array('mission'=>$mission));
196
    }
197
198
    /**
199
    * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission
200
    *
201
    * @return Collection de MissionGame $missionGames
202
    */
203
    public function findMissionGameByGame($game){
204
        return $this->getMissionGameMapper()->findOneBy(array('game'=>$game));
205
    }
206
207
    /**
208
    * findMissionGameConditionByMissionGame : Permet de recuperer les missionsGameCondition à partir d'une missionGame
209
    * @param MissionGame $missionGame
210
    *
211
    * @return Collection de MissionGameCondition $missionGameConditions
212
    */
213
    public function findMissionGameConditionByMissionGame($missionGame)
214
    {
215
        return $this->getMissionGameConditionMapper()->findBy(array('missionGame'=>$missionGame));
216
    } 
217
218
    /**
219
     * Retrieve service manager instance
220
     *
221
     * @return ServiceManager
222
     */
223
    public function getServiceManager()
224
    {
225
        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...
226
    }
227
228
    /**
229
     * Set service manager instance
230
     *
231
     * @return MissionGame
232
     */
233
    public function setServiceManager(ServiceManager $serviceManager)
234
    {
235
        $this->serviceManager = $serviceManager;
236
237
        return $this;
238
    }
239
240
     /**
241
    * getMissionGameConditionMapper : retrieve missionGameCondition mapper instance
242
    *
243
    * @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...
244
    */
245
    public function getMissionGameConditionMapper()
246
    {
247
        if (null === $this->missionGameConditionMapper) {
248
            $this->missionGameConditionMapper = $this->getServiceManager()->get('playgroundgame_mission_game_condition_mapper');
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...
249
        }
250
251
        return $this->missionGameConditionMapper;   
252
    }
253
254
    /**
255
    * getMissionGameMapper : retrieve missionGame mapper instance
256
    *
257
    * @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...
258
    */
259 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...
260
    {
261
        if (null === $this->missionGameMapper) {
262
            $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...
263
        }
264
265
        return $this->missionGameMapper;
266
    }
267
268
    /**
269
    * getGameMapper : retrieve game mapper instance
270
    *
271
    * @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...
272
    */
273 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...
274
    {
275
        if (null === $this->gameMapper) {
276
            $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...
277
        }
278
279
        return $this->gameMapper;
280
    }
281
}
282