Passed
Push — master ( 44623e...7a8014 )
by Janko
61:11 queued 21:50
created

ManageBattery::manage()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 6
nop 3
dl 0
loc 49
ccs 30
cts 30
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\ShipManagement\Manager;
6
7
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...
8
use RuntimeException;
9
use Stu\Component\Ship\System\Data\EpsSystemData;
10
use Stu\Component\Player\Relation\PlayerRelationDeterminatorInterface;
11
use Stu\Lib\ShipManagement\Provider\ManagerProviderInterface;
12
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
13
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
use Stu\Module\Ship\View\ShowShip\ShowShip;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Ship\View\ShowShip\ShowShip 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...
16
use Stu\Orm\Entity\ShipInterface;
17
18
class ManageBattery implements ManagerInterface
19
{
20 10
    public function __construct(
21
        private PrivateMessageSenderInterface $privateMessageSender,
22
        private PlayerRelationDeterminatorInterface $playerRelationDeterminator
23 10
    ) {}
24
25 10
    #[Override]
26
    public function manage(ShipWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array
27
    {
28 10
        $msg = [];
29
30 10
        $batt = $values['batt'] ?? null;
31 10
        if ($batt === null) {
32 1
            throw new RuntimeException('value array not existent');
33
        }
34
35 9
        $ship = $wrapper->get();
36 9
        $shipId = $ship->getId();
37 9
        $epsSystem = $wrapper->getEpsSystemData();
38
39
        if (
40 9
            $epsSystem === null
41 9
            || !array_key_exists($shipId, $batt)
42
        ) {
43 2
            return $msg;
44
        }
45
46 7
        if ($ship->getShieldState() && !$this->playerRelationDeterminator->isFriend($ship->getUser(), $managerProvider->getUser())) {
47 1
            $msg[] = sprintf(
48 1
                _('%s: Batterie konnte wegen aktivierter Schilde nicht aufgeladen werden.'),
49 1
                $ship->getName()
50 1
            );
51 1
            return $msg;
52
        }
53
54
        if (
55 6
            $managerProvider->getEps() > 0
56 6
            && $epsSystem->getBattery() < $epsSystem->getMaxBattery()
57
        ) {
58 4
            $load = $this->determineLoad($batt[$shipId], $epsSystem, $managerProvider);
59
60 4
            if ($load > 0) {
61 4
                $epsSystem->setBattery($epsSystem->getBattery() + $load)->update();
62 4
                $managerProvider->lowerEps($load);
63 4
                $msg[] = sprintf(
64 4
                    _('%s: Batterie um %d Einheiten aufgeladen'),
65 4
                    $ship->getName(),
66 4
                    $load
67 4
                );
68
69 4
                $this->sendMessageToOwner($ship, $managerProvider, $load);
70
            }
71
        }
72
73 6
        return $msg;
74
    }
75
76 4
    private function determineLoad(
77
        string $value,
78
        EpsSystemData $epsSystem,
79
        ManagerProviderInterface $managerProvider
80
    ): int {
81 4
        if ($value === 'm') {
82 1
            $load = $epsSystem->getMaxBattery() - $epsSystem->getBattery();
83
        } else {
84 3
            $load = (int) $value;
85 3
            if ($epsSystem->getBattery() + $load > $epsSystem->getMaxBattery()) {
86 1
                $load = $epsSystem->getMaxBattery() - $epsSystem->getBattery();
87
            }
88
        }
89 4
        if ($load > $managerProvider->getEps()) {
90 1
            $load = $managerProvider->getEps();
91
        }
92
93 4
        return $load;
94
    }
95
96 4
    private function sendMessageToOwner(ShipInterface $ship, ManagerProviderInterface $managerProvider, int $load): void
97
    {
98 4
        $href = sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $ship->getId());
99
100 4
        $this->privateMessageSender->send(
101 4
            $managerProvider->getUser()->getId(),
102 4
            $ship->getUser()->getId(),
103 4
            sprintf(
104 4
                _('Die %s lädt in Sektor %s die Batterie der %s um %s Einheiten'),
105 4
                $managerProvider->getName(),
106 4
                $managerProvider->getSectorString(),
107 4
                $ship->getName(),
108 4
                $load
109 4
            ),
110 4
            PrivateMessageFolderTypeEnum::SPECIAL_TRADE,
111 4
            $href
112 4
        );
113
    }
114
}
115