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 ( 710d07...9f3829 )
by Jacky
35s
created

SectorManager::initOwnershipData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Sector Manager
5
 *
6
 * @author Expansion
7
 * @copyright Expansion - le jeu
8
 *
9
 * @package Gaia
10
 * @update 20.05.13
11
*/
12
namespace Asylamba\Modules\Gaia\Manager;
13
14
use Asylamba\Classes\Entity\EntityManager;
15
use Asylamba\Classes\Redis\RedisManager;
16
use Asylamba\Classes\Process\LoadBalancer;
17
use Asylamba\Classes\Task\TaskManager;
18
19
use Asylamba\Modules\Athena\Manager\OrbitalBaseManager;
20
use Asylamba\Modules\Zeus\Manager\PlayerManager;
21
use Asylamba\Modules\Gaia\Manager\SystemManager;
22
23
use Asylamba\Modules\Gaia\Model\Sector;
24
25
class SectorManager {
26
	/** @var EntityManager **/
27
	protected $entityManager;
28
    /** @var RedisManager **/
29
    protected $redisManager;
30
    /** @var LoadBalancer **/
31
    protected $loadBalancer;
32
    /** @var TaskManager **/
33
    protected $taskManager;
34
    /** @var OrbitalBaseManager **/
35
    protected $orbitalBaseManager;
36
    /** @var PlayerManager **/
37
    protected $playerManager;
38
    /** @var SystemManager **/
39
    protected $systemManager;
40
    /** @var array **/
41
    protected $scores = [];
42
	
43
	/**
44
	 * @param EntityManager $entityManager
45
     * @param RedisManager $redisManager
46
     * @param LoadBalancer $loadBalancer
47
     * @param TaskManager $taskManager
48
     * @param OrbitalBaseManager $orbitalBaseManager
49
     * @param PlayerManager $playerManager
50
     * @param SystemManager $systemManager
51
     * @param array $scores
52
	 */
53
	public function __construct(
54
        EntityManager $entityManager,
55
        RedisManager $redisManager,
56
        LoadBalancer $loadBalancer,
57
        TaskManager $taskManager,
58
        OrbitalBaseManager $orbitalBaseManager,
59
        PlayerManager $playerManager,
60
        SystemManager $systemManager,
61
        $scores
62
    )
63
	{
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...
64
		$this->entityManager = $entityManager;
65
        $this->redisManager = $redisManager;
66
        $this->loadBalancer = $loadBalancer;
67
        $this->taskManager = $taskManager;
68
        $this->orbitalBaseManager = $orbitalBaseManager;
69
        $this->playerManager = $playerManager;
70
        $this->systemManager = $systemManager;
71
        $this->scores = $scores;
72
	}
73
    
74
    public function initOwnershipData()
75
    {
76
        $this->loadBalancer->affectTask(
77
            $this->taskManager->createTechnicalTask('gaia.sector_manager', 'calculateAllOwnerships')
78
        );
79
    }
80
81
	/**
82
	 * @param int $id
83
	 * @return Sector
84
	 */
85
	public function get($id) {
86
		return $this->entityManager->getRepository(Sector::class)->get($id);
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 get() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository, Asylamba\Modules\Ares\Re...ry\LiveReportRepository, Asylamba\Modules\Ares\Repository\ReportRepository, Asylamba\Modules\Athena\...BuildingQueueRepository, Asylamba\Modules\Athena\...mmercialRouteRepository, Asylamba\Modules\Athena\...rcialShippingRepository, Asylamba\Modules\Athena\...y\OrbitalBaseRepository, Asylamba\Modules\Athena\...yclingMissionRepository, Asylamba\Modules\Athena\...ory\ShipQueueRepository, Asylamba\Modules\Athena\...y\TransactionRepository, Asylamba\Modules\Demeter...ository\ColorRepository, Asylamba\Modules\Demeter...ion\CandidateRepository, Asylamba\Modules\Demeter...tion\ElectionRepository, Asylamba\Modules\Demeter...m\FactionNewsRepository, Asylamba\Modules\Demeter...itory\Law\LawRepository, Asylamba\Modules\Gaia\Repository\PlaceRepository, Asylamba\Modules\Gaia\Repository\SectorRepository, Asylamba\Modules\Gaia\Repository\SystemRepository, Asylamba\Modules\Hermes\...\NotificationRepository, Asylamba\Modules\Prometh...chnologyQueueRepository, Asylamba\Modules\Zeus\Repository\PlayerRepository. 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...
87
	}
88
	
89
	/**
90
	 * @param int $factionId
91
	 * @return array
92
	 */
93
	public function getFactionSectors($factionId)
94
	{
95
		return $this->entityManager->getRepository(Sector::class)->getFactionSectors($factionId);
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 getFactionSectors() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Gaia\Repository\SectorRepository. 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...
96
	}
97
	
98
	/**
99
	 * @return array
100
	 */
101
	public function getAll()
102
	{
103
		return $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\...yclingMissionRepository, 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...
104
	}
105
	
106
	/**
107
	 * @param Sector $sector
108
	 */
109
	public function changeOwnership(Sector $sector)
110
	{
111
		$this->entityManager->getRepository(Sector::class)->changeOwnership($sector);
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 changeOwnership() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Gaia\Repository\SectorRepository, Asylamba\Modules\Gaia\Repository\SystemRepository. 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...
112
	}
113
    
114
    public function calculateAllOwnerships()
115
    {
116
        \Asylamba\Classes\Daemon\Server::debug('sectors');
117
        foreach ($this->getAll() as $sector) {
118
            $this->calculateOwnership($sector);
119
        }
120
    }
121
    
122
    /**
123
     * @param Sector $sector
124
     * @return array
125
     */
126
    public function calculateOwnership(Sector $sector)
127
    {
128
		$systems = $this->systemManager->getSectorSystems($sector->getId());
129
		$bases = $this->orbitalBaseManager->getSectorBases($sector->getId());
130
		$scores = [];
131
		
132
		foreach ($bases as $base)
133
		{
134
			$player = $this->playerManager->get($base->rPlayer);
135
			
136
			$scores[$player->rColor] =
137
				(!empty($scores[$player->rColor]))
138
				? $scores[$player->rColor] + $this->scores[$base->typeOfBase]
139
				: $this->scores[$base->typeOfBase]
140
			;
141
		}
142
		// For each system, the owning faction gains two points
143
		foreach ($systems as $system) {
144
			if ($system->rColor === 0) {
145
				continue;
146
			}
147
			$scores[$system->rColor] = (!empty($scores[$system->rColor])) ? $scores[$system->rColor] + 2 : 2;
148
		}
149
		$scores[0] = 0;
150
		arsort($scores);
151
		reset($scores);
152
        
153
        $this->redisManager->getConnection()->set('sector:' . $sector->getId(), serialize($scores));
154
        
155
        return $scores;
156
    }
157
}