Passed
Pull Request — master (#1696)
by Nico
49:43 queued 22:34
created

ManagementProvider::getUserDepositMinings()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 30
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
use request;
6
use Stu\Component\Building\BuildingEnum;
7
use Stu\Component\Colony\ColonyFunctionManagerInterface;
8
use Stu\Component\Colony\OrbitShipListRetrieverInterface;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Database\View\Category\Tal\DatabaseCategoryTalFactoryInterface;
13
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
14
use Stu\Orm\Entity\ColonyDepositMiningInterface;
15
use Stu\Orm\Entity\ColonyInterface;
16
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
17
18
final class ManagementProvider implements GuiComponentProviderInterface
19
{
20
    private TorpedoTypeRepositoryInterface $torpedoTypeRepository;
21
22
    private DatabaseCategoryTalFactoryInterface $databaseCategoryTalFactory;
23
24
    private OrbitShipListRetrieverInterface $orbitShipListRetriever;
25
26
    private ShipWrapperFactoryInterface $shipWrapperFactory;
27
28
    private ColonyFunctionManagerInterface $colonyFunctionManager;
29
30
    private ColonyLibFactoryInterface $colonyLibFactory;
31
32
    public function __construct(
33
        TorpedoTypeRepositoryInterface $torpedoTypeRepository,
34
        DatabaseCategoryTalFactoryInterface $databaseCategoryTalFactory,
35
        OrbitShipListRetrieverInterface $orbitShipListRetriever,
36
        ColonyFunctionManagerInterface $colonyFunctionManager,
37
        ShipWrapperFactoryInterface $shipWrapperFactory,
38
        ColonyLibFactoryInterface $colonyLibFactory
39
    ) {
40
        $this->torpedoTypeRepository = $torpedoTypeRepository;
41
        $this->databaseCategoryTalFactory = $databaseCategoryTalFactory;
42
        $this->orbitShipListRetriever = $orbitShipListRetriever;
43
        $this->shipWrapperFactory = $shipWrapperFactory;
44
        $this->colonyFunctionManager = $colonyFunctionManager;
45
        $this->colonyLibFactory = $colonyLibFactory;
46
    }
47
48
    public function setTemplateVariables(
49
        PlanetFieldHostInterface $host,
50
        GameControllerInterface $game
51
    ): void {
52
53
        if (!$host instanceof ColonyInterface) {
54
            return;
55
        }
56
57
        $systemDatabaseEntry = $host->getSystem()->getDatabaseEntry();
58
        if ($systemDatabaseEntry !== null) {
59
            $starsystem = $this->databaseCategoryTalFactory->createDatabaseCategoryEntryTal($systemDatabaseEntry, $game->getUser());
60
            $game->setTemplateVar('STARSYSTEM_ENTRY_TAL', $starsystem);
61
        }
62
63
        $firstOrbitShip = null;
64
65
        $shipList = $this->orbitShipListRetriever->retrieve($host);
66
        if ($shipList !== []) {
67
            // if selected, return the current target
68
            $target = request::postInt('target');
69
70
            if ($target !== 0) {
71
                foreach ($shipList as $fleet) {
72
                    foreach ($fleet['ships'] as $idx => $ship) {
73
                        if ($idx == $target) {
74
                            $firstOrbitShip = $ship;
75
                        }
76
                    }
77
                }
78
            }
79
            if ($firstOrbitShip === null) {
80
                $firstOrbitShip = current(current($shipList)['ships']);
81
            }
82
        }
83
84
        $game->setTemplateVar(
85
            'SURFACE',
86
            $this->colonyLibFactory->createColonySurface($host)
87
        );
88
89
90
        $game->setTemplateVar(
91
            'FIRST_ORBIT_SHIP',
92
            $firstOrbitShip ? $this->shipWrapperFactory->wrapShip($firstOrbitShip) : null
93
        );
94
95
        $particlePhalanx = $this->colonyFunctionManager->hasFunction($host, BuildingEnum::BUILDING_FUNCTION_PARTICLE_PHALANX);
96
        $game->setTemplateVar(
97
            'BUILDABLE_TORPEDO_TYPES',
98
            $particlePhalanx ? $this->torpedoTypeRepository->getForUser($game->getUser()->getId()) : null
99
        );
100
101
        $game->setTemplateVar('DEPOSIT_MININGS', $this->getUserDepositMinings($host));
102
    }
103
104
    /**
105
     * @return array<int, array{deposit: ColonyDepositMiningInterface, currentlyMined: int}>
106
     */
107
    private function getUserDepositMinings(PlanetFieldHostInterface $host): array
108
    {
109
        $production = $this->colonyLibFactory->createColonyCommodityProduction($host)->getProduction();
110
111
        $result = [];
112
        if (!$host instanceof ColonyInterface) {
113
            return $result;
114
        }
115
116
        foreach ($host->getDepositMinings() as $deposit) {
117
            if ($deposit->getUser() === $host->getUser()) {
118
                $prod = $production[$deposit->getCommodity()->getId()] ?? null;
119
120
                $result[$deposit->getCommodity()->getId()] = [
121
                    'deposit' => $deposit,
122
                    'currentlyMined' => $prod === null ? 0 : $prod->getProduction()
123
                ];
124
            }
125
        }
126
127
        return $result;
128
    }
129
}
130