GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 11c0a1...856a6b )
by Jacky
34s
created

RankingManager::processWinningFaction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 28
nc 3
nop 1
dl 0
loc 44
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
namespace Asylamba\Modules\Atlas\Manager;
4
5
use Asylamba\Classes\Entity\EntityManager;
6
7
use Asylamba\Modules\Demeter\Manager\ColorManager;
8
use Asylamba\Modules\Hermes\Manager\ConversationManager;
9
use Asylamba\Modules\Hermes\Manager\ConversationMessageManager;
10
11
use Asylamba\Modules\Atlas\Routine\DailyRoutine;
12
use Asylamba\Modules\Atlas\Routine\PlayerRoutine;
13
use Asylamba\Modules\Atlas\Routine\FactionRoutine;
14
15
use Asylamba\Modules\Hermes\Model\ConversationUser;
16
use Asylamba\Modules\Hermes\Model\ConversationMessage;
17
use Asylamba\Modules\Demeter\Resource\ColorResource;
18
use Asylamba\Modules\Athena\Helper\OrbitalBaseHelper;
19
use Asylamba\Modules\Zeus\Manager\PlayerManager;
20
21
use Asylamba\Modules\Zeus\Model\Player;
22
use Asylamba\Modules\Atlas\Model\PlayerRanking;
23
use Asylamba\Modules\Atlas\Model\FactionRanking;
24
use Asylamba\Modules\Atlas\Model\Ranking;
25
use Asylamba\Modules\Gaia\Model\Sector;
26
27
use Asylamba\Classes\Exception\ErrorException;
28
29
use Asylamba\Classes\Library\Utils;
30
31
class RankingManager
32
{
33
	/** @var EntityManager **/
34
	protected $entityManager;
35
	/** @var PlayerRankingManager **/
36
	protected $playerRankingManager;
37
	/** @var FactionRankingManager **/
38
	protected $factionRankingManager;
39
	/** @var ColorManager **/
40
	protected $colorManager;
41
	/** @var ConversationManager **/
42
	protected $conversationManager;
43
	/** @var ConversationMessageManager **/
44
	protected $conversationMessageManager;
45
	/** @var PlayerManager **/
46
	protected $playerManager;
47
	/** @var OrbitalBaseHelper **/
48
	protected $orbitalBaseHelper;
49
	/** @var FactionRoutine **/
50
	protected $factionRoutine;
51
	/** @var PlayerRoutine **/
52
	protected $playerRoutine;
53
	/** @var DailyRoutine **/
54
	protected $dailyRoutine;
55
	
56
	/**
57
	 * @param EntityManager $entityManager
58
	 * @param PlayerRankingManager $playerRankingManager
59
	 * @param FactionRankingManager $factionRankingManager
60
	 * @param ColorManager $colorManager
61
	 * @param ConversationManager $conversationManager
62
	 * @param ConversationMessageManager $conversationMessageManager
63
	 * @param PlayerManager $playerManager
64
	 * @param OrbitalBaseHelper $orbitalBaseHelper
65
	 */
66
	public function __construct(
67
		EntityManager $entityManager,
68
		PlayerRankingManager $playerRankingManager,
69
		FactionRankingManager $factionRankingManager,
70
		ColorManager $colorManager,
71
		ConversationManager $conversationManager,
72
		ConversationMessageManager $conversationMessageManager,
73
		PlayerManager $playerManager,
74
		OrbitalBaseHelper $orbitalBaseHelper
75
	)
76
	{
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
77
		$this->entityManager = $entityManager;
78
		$this->playerRankingManager = $playerRankingManager;
79
		$this->factionRankingManager = $factionRankingManager;
80
		$this->colorManager = $colorManager;
81
		$this->conversationManager = $conversationManager;
82
		$this->conversationMessageManager = $conversationMessageManager;
83
		$this->playerManager = $playerManager;
84
		$this->orbitalBaseHelper = $orbitalBaseHelper;
85
		
86
		$this->dailyRoutine = new DailyRoutine();
87
		$this->playerRoutine = new PlayerRoutine();
88
		$this->factionRoutine = new FactionRoutine();
89
	}
90
	
91
	public function processPlayersRanking()
92
	{
93
		if ($this->entityManager->getRepository(Ranking::class)->hasBeenAlreadyProcessed(true, false) === true) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Asylamba\Classes\Entity\AbstractRepository as the method hasBeenAlreadyProcessed() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Atlas\R...itory\RankingRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
94
			return;
95
		}
96
		
97
		$players = $this->playerManager->getByStatements([Player::ACTIVE, Player::INACTIVE, Player::HOLIDAY]);
98
		
99
		$playerRankingRepository = $this->entityManager->getRepository(PlayerRanking::class);
100
		
101
		$ranking = $this->createRanking(true, false);
102
		
103
		$this->playerRoutine->execute(
104
			$players,
105
			$playerRankingRepository->getPlayersResources(),
106
			$playerRankingRepository->getPlayersResourcesData(),
107
			$playerRankingRepository->getPlayersGeneralData(),
108
			$playerRankingRepository->getPlayersArmiesData(),
109
			$playerRankingRepository->getPlayersPlanetData(),
110
			$playerRankingRepository->getPlayersTradeRoutes(),
111
			$playerRankingRepository->getPlayersLinkedTradeRoutes(),
112
			$playerRankingRepository->getAttackersButcherRanking(),
113
			$playerRankingRepository->getDefendersButcherRanking(),
114
			$this->orbitalBaseHelper
115
		);
116
		
117
		$S_PRM1 = $this->playerRankingManager->getCurrentSession();
118
		$this->playerRankingManager->newSession();
119
		$this->playerRankingManager->loadLastContext();
120
121
		$this->playerRoutine->processResults($ranking, $players, $this->playerRankingManager, $playerRankingRepository);
0 ignored issues
show
Compatibility introduced by
$playerRankingRepository of type object<Asylamba\Classes\...ity\AbstractRepository> is not a sub-type of object<Asylamba\Modules\...layerRankingRepository>. It seems like you assume a child class of the class Asylamba\Classes\Entity\AbstractRepository to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
122
		
123
		$this->playerRankingManager->changeSession($S_PRM1);
124
		
125
		$this->entityManager->flush();
126
	}
127
	
128
	public function processFactionsRanking()
129
	{
130
		if ($this->entityManager->getRepository(Ranking::class)->hasBeenAlreadyProcessed(false, true) === true) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Asylamba\Classes\Entity\AbstractRepository as the method hasBeenAlreadyProcessed() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Atlas\R...itory\RankingRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
131
			return;
132
		}
133
		
134
		$factions = $this->colorManager->getInGameFactions();
135
		$playerRankingRepository = $this->entityManager->getRepository(PlayerRanking::class);
136
		$factionRankingRepository = $this->entityManager->getRepository(FactionRanking::class);
137
		$sectors = $this->entityManager->getRepository(Sector::class)->getAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Asylamba\Classes\Entity\AbstractRepository as the method getAll() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Athena\...BuildingQueueRepository, Asylamba\Modules\Athena\...rcialShippingRepository, Asylamba\Modules\Athena\...y\OrbitalBaseRepository, Asylamba\Modules\Athena\...ory\ShipQueueRepository, Asylamba\Modules\Demeter...ository\ColorRepository, Asylamba\Modules\Gaia\Repository\SectorRepository, Asylamba\Modules\Gaia\Repository\SystemRepository, Asylamba\Modules\Prometh...chnologyQueueRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
138
		
139
		$ranking = $this->createRanking(false, true);
140
		
141
		foreach ($factions as $faction) {
142
			$routesIncome = $factionRankingRepository->getRoutesIncome($faction);
143
			$playerRankings = $playerRankingRepository->getFactionPlayerRankings($faction);
144
			
145
			$this->factionRoutine->execute($faction, $playerRankings, $routesIncome, $sectors);
146
		}
147
		
148
		$S_FRM1 = $this->factionRankingManager->getCurrentSession();
149
		$this->factionRankingManager->newSession();
150
		$this->factionRankingManager->loadLastContext();
151
		
152
		$winningFactionId = $this->factionRoutine->processResults($ranking, $factions, $this->factionRankingManager);
153
154
		$this->factionRankingManager->changeSession($S_FRM1);
155
		
156
		if ($winningFactionId !== null) {
157
			$this->processWinningFaction($winningFactionId);
158
		}
159
		$this->entityManager->flush();
160
	}
161
	
162
	protected function processWinningFaction($factionId)
163
	{
164
		$faction = $this->colorManager->get($factionId);
165
		$faction->isWinner = Color::WIN;
166
167
		# envoyer un message de Jean-Mi
168
		$winnerName = ColorResource::getInfo($faction->id, 'officialName');
169
		$content = 'Salut,<br /><br />La victoire vient d\'être remportée par : <br /><strong>' . $winnerName . '</strong><br />';
170
		$content .= 'Cette faction a atteint les ' . POINTS_TO_WIN . ' points, la partie est donc terminée.<br /><br />Bravo et un grand merci à tous les participants !';
171
172
		$S_CVM1 = $this->conversationManager->getCurrentSession();
173
		$this->conversationManager->newSession();
174
		$this->conversationManager->load(
175
			['cu.rPlayer' => ID_JEANMI]
176
		);
177
178
		if ($this->conversationManager->size() == 1) {
179
			$conv = $this->conversationManager->get();
180
181
			$conv->messages++;
182
			$conv->dLastMessage = Utils::now();
183
184
			# désarchiver tous les users
185
			$users = $conv->players;
186
			foreach ($users as $user) {
187
				$user->convStatement = ConversationUser::CS_DISPLAY;
188
			}
189
190
			# création du message
191
			$message = new ConversationMessage();
192
193
			$message->rConversation = $conv->id;
194
			$message->rPlayer = ID_JEANMI;
195
			$message->type = ConversationMessage::TY_STD;
196
			$message->content = $content;
0 ignored issues
show
Documentation Bug introduced by
The property $content was declared of type integer, but $content is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
197
			$message->dCreation = Utils::now();
0 ignored issues
show
Documentation Bug introduced by
The property $dCreation was declared of type integer, but \Asylamba\Classes\Library\Utils::now() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
198
			$message->dLastModification = NULL;
199
200
			$this->conversationMessageManager->add($message);
201
		} else {
202
			throw new ErrorException('La conversation n\'existe pas ou ne vous appartient pas.');
203
		}
204
		$this->conversationManager->changeSession($S_CVM1);
205
	}
206
	
207
	protected function createRanking($isPlayer, $isFaction)
208
	{
209
		$ranking =
210
			(new Ranking())
211
			->setIsPlayer($isPlayer)
212
			->setIsFaction($isFaction)
213
			->setCreatedAt(Utils::now())
214
		;
215
		$this->entityManager->persist($ranking);
216
		$this->entityManager->flush($ranking);
217
		return $ranking;
218
	}
219
}