Passed
Pull Request — master (#2141)
by Nico
28:26 queued 16:43
created

ProceedMigration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 7.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 47
ccs 2
cts 26
cp 0.0769
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B work() 0 27 7
A proceedImmigration() 0 8 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\ColonyInterface;
9
10
class ProceedMigration implements ColonyTickComponentInterface
11
{
12 1
    public function __construct(
13
        private readonly ColonyLibFactoryInterface $colonyLibFactory
14 1
    ) {}
15
16
    public function work(ColonyInterface $colony, array &$production, InformationInterface $information): void
17
    {
18
        if ($colony->getPopulation() > $colony->getMaxBev()) {
19
            if ($colony->getWorkless() !== 0) {
20
                $bev = random_int(1, $colony->getWorkless());
21
                $colony->setWorkless($colony->getWorkless() - $bev);
22
                $information->addInformationf("%d Einwohner sind ausgewandert", $bev);
23
            }
24
            return;
25
        }
26
27
        if ($colony->getPopulationLimit() > 0 && $colony->getPopulation() > $colony->getPopulationLimit() && $colony->getWorkless()) {
28
            if (($free = ($colony->getPopulationLimit() - $colony->getWorkers())) > 0) {
29
                $information->addInformationf(
30
                    _('Es sind %d Arbeitslose ausgewandert'),
31
                    ($colony->getWorkless() - $free)
32
                );
33
                $colony->setWorkless($free);
34
            } else {
35
                $information->addInformation('Es sind alle Arbeitslosen ausgewandert');
36
                $colony->setWorkless(0);
37
            }
38
        }
39
40
        $this->proceedImmigration(
41
            $colony,
42
            $production
43
        );
44
    }
45
46
    /**
47
     * @param array<int, ColonyProduction> $production
48
     */
49
    private function proceedImmigration(
50
        ColonyInterface $colony,
51
        array $production
52
    ): void {
53
        // @todo
54
        $colony->setWorkless(
55
            $colony->getWorkless() +
56
                $this->colonyLibFactory->createColonyPopulationCalculator($colony, $production)->getGrowth()
57
        );
58
    }
59
}
60