Passed
Push — master ( c539b9...a2853f )
by Nico
42:04 queued 28:25
created

ProceedMigration::work()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 27
ccs 0
cts 19
cp 0
crap 56
rs 8.8333
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