Passed
Pull Request — dev (#2303)
by Janko
05:56
created

ProceedMigration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 56.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 55
ccs 17
cts 30
cp 0.5667
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B work() 0 34 7
A proceedImmigration() 0 9 1
1
<?php
2
3
namespace Stu\Module\Tick\Colony\Component;
4
5
use Stu\Lib\ColonyProduction\ColonyProduction;
6
use Stu\Lib\Information\InformationInterface;
7
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
8
use Stu\Orm\Entity\Colony;
9
10
class ProceedMigration implements ColonyTickComponentInterface
11
{
12 1
    public function __construct(
13
        private readonly ColonyLibFactoryInterface $colonyLibFactory
14 1
    ) {}
15
16 1
    #[\Override]
17
    public function work(Colony $colony, array &$production, InformationInterface $information): void
18
    {
19 1
        $changeable = $colony->getChangeable();
20
21 1
        if ($colony->getPopulation() > $changeable->getMaxBev()) {
22
            if ($changeable->getWorkless() !== 0) {
23
                $bev = random_int(1, $changeable->getWorkless());
24
                $changeable->setWorkless($changeable->getWorkless() - $bev);
25
                $information->addInformationf('%d Einwohner sind ausgewandert', $bev);
26
            }
27
            return;
28
        }
29
30
        if (
31 1
            $changeable->getPopulationLimit() > 0
32 1
            && $colony->getPopulation() > $changeable->getPopulationLimit()
33 1
            && $changeable->getWorkless()
34
        ) {
35
            if (($free = $changeable->getPopulationLimit() - $colony->getWorkers()) > 0) {
36
                $information->addInformationf(
37
                    _('Es sind %d Arbeitslose ausgewandert'),
38
                    $changeable->getWorkless() - $free
39
                );
40
                $changeable->setWorkless($free);
41
            } else {
42
                $information->addInformation('Es sind alle Arbeitslosen ausgewandert');
43
                $changeable->setWorkless(0);
44
            }
45
        }
46
47 1
        $this->proceedImmigration(
48 1
            $colony,
49 1
            $production
50 1
        );
51
    }
52
53
    /**
54
     * @param array<int, ColonyProduction> $production
55
     */
56 1
    private function proceedImmigration(
57
        Colony $colony,
58
        array $production
59
    ): void {
60 1
        $changeable = $colony->getChangeable();
61
62 1
        $changeable->setWorkless(
63 1
            $changeable->getWorkless()
64 1
            + $this->colonyLibFactory->createColonyPopulationCalculator($colony, $production)->getGrowth()
65 1
        );
66
    }
67
}
68