Passed
Pull Request — master (#1863)
by Nico
55:34 queued 27:10
created

ManageBattery::manage()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9.3086

Importance

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