Passed
Push — dev ( c4f015...eeaa0f )
by Janko
27:51
created

EscapePodHandling::processUser()   B

Complexity

Conditions 8
Paths 22

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 22
nc 22
nop 1
dl 0
loc 36
ccs 0
cts 21
cp 0
crap 72
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Tick\Ship\ManagerComponent;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
7
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
8
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
9
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Module\Ship\Lib\ShipRemoverInterface;
11
use Stu\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Entity\ShipInterface;
13
use Stu\Orm\Repository\ShipCrewRepositoryInterface;
14
use Stu\Orm\Repository\ShipRepositoryInterface;
15
16
class EscapePodHandling implements ManagerComponentInterface
17
{
18
    public function __construct(private PrivateMessageSenderInterface $privateMessageSender, private ShipRemoverInterface $shipRemover, private ShipRepositoryInterface $shipRepository, private ShipCrewRepositoryInterface $shipCrewRepository, private ColonyLibFactoryInterface $colonyLibFactory)
19
    {
20
    }
21
22
    #[Override]
23
    public function work(): void
24
    {
25
        $escapedToColonies = [];
26
27
        foreach ($this->shipRepository->getEscapePods() as $escapePod) {
28
            if ($escapePod->getCrewCount() === 0) {
29
                $this->shipRemover->remove($escapePod);
30
            }
31
32
            if ($escapePod->getStarsystemMap() !== null) {
33
                $colony = $escapePod->getStarsystemMap()->getColony();
34
35
                if ($colony !== null) {
36
                    $count = $this->transferOwnCrewToColony($escapePod, $colony);
37
38
                    if ($count > 0) {
39
                        if (array_key_exists($colony->getId(), $escapedToColonies)) {
40
                            $oldCount = $escapedToColonies[$colony->getId()][1];
41
42
                            $escapedToColonies[$colony->getId()][1] = $oldCount +  $count;
43
                        } else {
44
                            $escapedToColonies[$colony->getId()] = [$colony, $count];
45
                        }
46
                    }
47
                }
48
            }
49
        }
50
51
        foreach ($escapedToColonies as [$colony, $count]) {
52
            $msg = sprintf(_('%d deiner Crewman sind aus Fluchtkapseln auf deiner Kolonie %s gelandet'), $count, $colony->getName());
53
            $this->privateMessageSender->send(
54
                UserEnum::USER_NOONE,
55
                $colony->getUser()->getId(),
56
                $msg,
57
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
58
            );
59
        }
60
    }
61
62
    private function transferOwnCrewToColony(ShipInterface $escapePod, ColonyInterface $colony): int
63
    {
64
        $count = 0;
65
66
        foreach ($escapePod->getCrewAssignments() as $crewAssignment) {
67
            if ($crewAssignment->getUser() !== $colony->getUser()) {
68
                continue;
69
            }
70
71
            $freeAssignmentCount = $this->colonyLibFactory->createColonyPopulationCalculator(
72
                $colony
73
            )->getFreeAssignmentCount();
74
75
            if ($freeAssignmentCount === 0) {
76
                break;
77
            }
78
79
            $count++;
80
            $crewAssignment->setShip(null);
81
            $crewAssignment->setSlot(null);
82
            $crewAssignment->setColony($colony);
83
            $escapePod->getCrewAssignments()->removeElement($crewAssignment);
84
            $colony->getCrewAssignments()->add($crewAssignment);
85
            $this->shipCrewRepository->save($crewAssignment);
86
        }
87
88
        return $count;
89
    }
90
}
91