Passed
Pull Request — master (#1657)
by Nico
09:45
created

SatisfiedWorkerRanking   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 77
ccs 0
cts 41
cp 0
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 24 1
A setPointsForUser() 0 27 4
A __construct() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Database\View\SatisfiedWorkerRanking;
6
7
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
8
use Stu\Module\Commodity\CommodityTypeEnum;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Control\ViewControllerInterface;
11
use Stu\Module\Database\Lib\DatabaseTopListWithPoints;
12
use Stu\Module\Database\Lib\DatabaseUiFactoryInterface;
13
use Stu\Module\Logging\LoggerUtilFactoryInterface;
14
use Stu\Module\Logging\LoggerUtilInterface;
15
use Stu\Orm\Repository\ColonyRepositoryInterface;
16
17
final class SatisfiedWorkerRanking implements ViewControllerInterface
18
{
19
    public const VIEW_IDENTIFIER = 'SHOW_SATISFIED_WORKER';
20
21
    private DatabaseUiFactoryInterface $databaseUiFactory;
22
23
    private ColonyRepositoryInterface $colonyRepository;
24
25
    private ColonyLibFactoryInterface $colonyLibFactory;
26
27
    private LoggerUtilInterface $loggerUtil;
28
29
    public function __construct(
30
        DatabaseUiFactoryInterface $databaseUiFactory,
31
        ColonyRepositoryInterface $colonyRepository,
32
        ColonyLibFactoryInterface $colonyLibFactory,
33
        LoggerUtilFactoryInterface $loggerUtilFactory
34
    ) {
35
        $this->databaseUiFactory = $databaseUiFactory;
36
        $this->colonyRepository = $colonyRepository;
37
        $this->colonyLibFactory = $colonyLibFactory;
38
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
39
    }
40
41
    public function handle(GameControllerInterface $game): void
42
    {
43
        $game->setNavigation([
44
            [
45
                'url' => 'database.php',
46
                'title' => 'Datenbank'
47
            ],
48
            [
49
                'url' => sprintf('database.php?%s=1', static::VIEW_IDENTIFIER),
50
                'title' => 'Die Top 10 der besten Arbeitgeber'
51
            ]
52
        ]);
53
        $game->setPageTitle('/ Datenbank / Die Top 10 besten Arbeitgeber');
54
        $game->showMacro('html/database.xhtml/top_employer');
55
56
        $game->setTemplateVar(
57
            'EMPLOYER_LIST',
58
            array_map(
59
                fn (array $data): DatabaseTopListWithPoints => $this->databaseUiFactory->createDatabaseTopListWithPoints($data['user_id'], (string)$data['satisfied']),
60
                $this->colonyRepository->getSatisfiedWorkerTop10()
61
            )
62
        );
63
64
        $this->setPointsForUser($game);
65
    }
66
67
    private function setPointsForUser(GameControllerInterface $game): void
68
    {
69
        $user = $game->getUser();
70
71
        $colonies = $user->getColonies();
72
        if ($colonies->isEmpty()) {
73
            return;
74
        }
75
76
        $satisfiedWorkers = 0;
77
78
        foreach ($colonies as $colony) {
79
            $workers = $colony->getWorkers();
80
81
            $colonyProduction = $this->colonyLibFactory->createColonyCommodityProduction($colony)->getProduction();
82
            if (array_key_exists(CommodityTypeEnum::COMMODITY_EFFECT_LIFE_STANDARD, $colonyProduction)) {
83
                $lifestandard = $colonyProduction[CommodityTypeEnum::COMMODITY_EFFECT_LIFE_STANDARD]->getProduction();
84
            } else {
85
                $lifestandard = 0;
86
            }
87
88
            $this->loggerUtil->log(sprintf('colony: %s, lifestandard: %d', $colony->getName(), $lifestandard));
89
90
            $satisfiedWorkers += min($workers, $lifestandard);
91
        }
92
93
        $game->setTemplateVar('USER_POINTS', (string) $satisfiedWorkers);
94
    }
95
}
96