Passed
Push — dev ( 20f168...3a8ec2 )
by Nico
15:39
created

ManagementProvider::setTemplateVariables()   B

Complexity

Conditions 11
Paths 25

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 11.051

Importance

Changes 0
Metric Value
cc 11
eloc 38
nc 25
nop 2
dl 0
loc 62
ccs 37
cts 40
cp 0.925
crap 11.051
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use request;
7
use Stu\Component\Building\BuildingFunctionEnum;
8
use Stu\Component\Colony\ColonyFunctionManagerInterface;
9
use Stu\Component\Colony\OrbitShipWrappersRetrieverInterface;
10
use Stu\Lib\Colony\PlanetFieldHostInterface;
11
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Control\StuTime;
14
use Stu\Module\Database\View\Category\Wrapper\DatabaseCategoryWrapperFactoryInterface;
15
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
16
use Stu\Orm\Entity\ColonyDepositMiningInterface;
17
use Stu\Orm\Entity\ColonyInterface;
18
use Stu\Orm\Repository\ColonyDepositMiningRepositoryInterface;
19
use Stu\Orm\Repository\StationRepositoryInterface;
20
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
21
22
final class ManagementProvider implements PlanetFieldHostComponentInterface
23
{
24 1
    public function __construct(
25
        private readonly StationRepositoryInterface $stationRepository,
26
        private readonly TorpedoTypeRepositoryInterface $torpedoTypeRepository,
27
        private readonly ColonyDepositMiningRepositoryInterface $colonyDepositMiningRepository,
28
        private readonly DatabaseCategoryWrapperFactoryInterface $databaseCategoryWrapperFactory,
29
        private readonly OrbitShipWrappersRetrieverInterface $orbitShipWrappersRetriever,
30
        private readonly ColonyFunctionManagerInterface $colonyFunctionManager,
31
        private readonly ColonyLibFactoryInterface $colonyLibFactory,
32
        private readonly SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
33
        private readonly StuTime $stuTime
34 1
    ) {}
35
36 4
    #[Override]
37
    public function setTemplateVariables(
38
        $entity,
39
        GameControllerInterface $game
40
    ): void {
41
42 4
        if (!$entity instanceof ColonyInterface) {
43
            return;
44
        }
45
46 4
        $systemDatabaseEntry = $entity->getSystem()->getDatabaseEntry();
47 4
        if ($systemDatabaseEntry !== null) {
48 4
            $starsystem = $this->databaseCategoryWrapperFactory->createDatabaseCategoryEntryWrapper($systemDatabaseEntry, $game->getUser());
49 4
            $game->setTemplateVar('STARSYSTEM_ENTRY_TAL', $starsystem);
50
        }
51
52 4
        $firstOrbitShipWrapper = null;
53
54 4
        $targetId = request::indInt('target');
55 4
        $groups = $this->orbitShipWrappersRetriever->retrieve($entity);
56
57 4
        if ($targetId !== 0) {
58 4
            foreach ($groups as $group) {
59 4
                foreach ($group->getWrappers() as $wrapper) {
60 4
                    if ($wrapper->get()->getId() === $targetId) {
61
                        $firstOrbitShipWrapper = $wrapper;
62
                    }
63
                }
64
            }
65
        }
66 4
        if ($firstOrbitShipWrapper === null) {
67 4
            $firstGroup = $groups->first();
68 4
            $firstOrbitShipWrapper = $firstGroup ? $firstGroup->getWrappers()->first() : null;
69
        }
70
71 4
        $game->setTemplateVar(
72 4
            'POPULATION_CALCULATOR',
73 4
            $this->colonyLibFactory->createColonyPopulationCalculator($entity)
74 4
        );
75
76 4
        $station = $this->stationRepository->getStationOnLocation($entity->getLocation());
77 4
        if ($station !== null) {
78
            $game->setTemplateVar('ORBIT_STATION_WRAPPER', $this->spacecraftWrapperFactory->wrapStation($station));
79
        }
80 4
        $game->setTemplateVar('FIRST_ORBIT_SPACECRAFT', $firstOrbitShipWrapper);
81
82 4
        $particlePhalanx = $this->colonyFunctionManager->hasFunction($entity, BuildingFunctionEnum::PARTICLE_PHALANX);
83 4
        $game->setTemplateVar(
84 4
            'BUILDABLE_TORPEDO_TYPES',
85 4
            $particlePhalanx ? $this->torpedoTypeRepository->getForUser($game->getUser()->getId()) : null
86 4
        );
87
88 4
        $shieldingManager = $this->colonyLibFactory->createColonyShieldingManager($entity);
89 4
        $game->setTemplateVar('SHIELDING_MANAGER', $shieldingManager);
90 4
        $game->setTemplateVar('DEPOSIT_MININGS', $this->getUserDepositMinings($entity));
91 4
        $game->setTemplateVar('VISUAL_PANEL', $this->colonyLibFactory->createColonyScanPanel($entity));
92
93 4
        $timestamp = $this->stuTime->time();
94 4
        $game->setTemplateVar('COLONY_TIME_HOUR', $entity->getColonyTimeHour($timestamp));
95 4
        $game->setTemplateVar('COLONY_TIME_MINUTE', $entity->getColonyTimeMinute($timestamp));
96 4
        $game->setTemplateVar('COLONY_DAY_TIME_PREFIX', $entity->getDayTimePrefix($timestamp));
97 4
        $game->setTemplateVar('COLONY_DAY_TIME_NAME', $entity->getDayTimeName($timestamp));
98
    }
99
100
    /**
101
     * @return array<int, array{deposit: ColonyDepositMiningInterface, currentlyMined: int}>
102
     */
103 4
    private function getUserDepositMinings(PlanetFieldHostInterface $host): array
104
    {
105 4
        $production = $this->colonyLibFactory->createColonyCommodityProduction($host)->getProduction();
106
107 4
        $result = [];
108 4
        if (!$host instanceof ColonyInterface) {
109
            return $result;
110
        }
111
112 4
        $depositMinings = $this->colonyDepositMiningRepository->getCurrentUserDepositMinings($host);
113
114 4
        foreach ($depositMinings as $deposit) {
115
            $prod = $production[$deposit->getCommodity()->getId()] ?? null;
116
117
            $result[$deposit->getCommodity()->getId()] = [
118
                'deposit' => $deposit,
119
                'currentlyMined' => $prod === null ? 0 : $prod->getProduction()
120
            ];
121
        }
122
123 4
        return $result;
124
    }
125
}
126