Passed
Push — dev ( 48e13f...0aa780 )
by Janko
09:21
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 1
Bugs 1 Features 0
Metric Value
cc 11
eloc 38
c 1
b 1
f 0
nc 25
nop 2
dl 0
loc 62
ccs 37
cts 40
cp 0.925
crap 11.051
rs 7.3166

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