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

LowerHull::work()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
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\Lib\Information\InformationWrapper;
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\Destruction\ShipDestructionCauseEnum;
11
use Stu\Module\Ship\Lib\Destruction\ShipDestructionInterface;
12
use Stu\Module\Ship\Lib\ShipRemoverInterface;
13
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
14
use Stu\Orm\Repository\ShipRepositoryInterface;
15
use Stu\Orm\Repository\TradePostRepositoryInterface;
16
17
class LowerHull implements ManagerComponentInterface
18
{
19
    public function __construct(
20
        private PrivateMessageSenderInterface $privateMessageSender,
21
        private ShipRemoverInterface $shipRemover,
22
        private ShipDestructionInterface $shipDestruction,
23
        private ShipRepositoryInterface $shipRepository,
24
        private TradePostRepositoryInterface $tradePostRepository,
25
        private ShipWrapperFactoryInterface $shipWrapperFactory
26
    ) {
27
    }
28
29
    #[Override]
30
    public function work(): void
31
    {
32
        $this->lowerTrumfieldHull();
33
        $this->lowerOrphanizedTradepostHull();
34
        $this->lowerStationConstructionHull();
35
    }
36
37
    private function lowerTrumfieldHull(): void
38
    {
39
        foreach ($this->shipRepository->getDebrisFields() as $ship) {
40
            $lower = random_int(5, 15);
41
            if ($ship->getHull() <= $lower) {
42
                $this->shipRemover->remove($ship);
43
                continue;
44
            }
45
            $ship->setHuell($ship->getHull() - $lower);
46
47
            $this->shipRepository->save($ship);
48
        }
49
    }
50
51
    private function lowerOrphanizedTradepostHull(): void
52
    {
53
        foreach ($this->tradePostRepository->getByUser(UserEnum::USER_NOONE) as $tradepost) {
54
            $ship = $tradepost->getShip();
55
56
            $lower = (int)ceil($ship->getMaxHull() / 100);
57
58
            if ($ship->getHull() <= $lower) {
59
                $this->shipDestruction->destroy(
60
                    null,
61
                    $this->shipWrapperFactory->wrapShip($ship),
62
                    ShipDestructionCauseEnum::ORPHANIZED_TRADEPOST,
63
                    new InformationWrapper()
64
                );
65
66
                continue;
67
            }
68
            $ship->setHuell($ship->getHull() - $lower);
69
70
            $this->shipRepository->save($ship);
71
        }
72
    }
73
74
    private function lowerStationConstructionHull(): void
75
    {
76
        foreach ($this->shipRepository->getStationConstructions() as $ship) {
77
            $lower = random_int(5, 15);
78
            if ($ship->getHull() <= $lower) {
79
                $msg = sprintf(_('Dein Konstrukt bei %s war zu lange ungenutzt und ist daher zerfallen'), $ship->getSectorString());
80
                $this->privateMessageSender->send(
81
                    UserEnum::USER_NOONE,
82
                    $ship->getUser()->getId(),
83
                    $msg,
84
                    PrivateMessageFolderTypeEnum::SPECIAL_STATION
85
                );
86
87
                $this->shipRemover->remove($ship);
88
                continue;
89
            }
90
            $ship->setHuell($ship->getHull() - $lower);
91
92
            $this->shipRepository->save($ship);
93
        }
94
    }
95
}
96