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