Passed
Pull Request — master (#2331)
by Janko
11:37 queued 06:03
created

ChangeFleetLeader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 56
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A change() 0 42 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Fleet;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use RuntimeException;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Module\Logging\LoggerUtilFactoryInterface;
11
use Stu\Module\Logging\LoggerUtilInterface;
12
use Stu\Module\Ship\Lib\CancelColonyBlockOrDefendInterface;
13
use Stu\Orm\Entity\Ship;
14
use Stu\Orm\Repository\FleetRepositoryInterface;
15
use Stu\Orm\Repository\ShipRepositoryInterface;
16
17
final class ChangeFleetLeader implements ChangeFleetLeaderInterface
18
{
19
    private LoggerUtilInterface $logger;
20
21 4
    public function __construct(
22
        private FleetRepositoryInterface $fleetRepository,
23
        private ShipRepositoryInterface $shipRepository,
24
        private CancelColonyBlockOrDefendInterface $cancelColonyBlockOrDefend,
25
        private EntityManagerInterface $entityManager,
26
        LoggerUtilFactoryInterface $loggerUtilFactory
27
    ) {
28 4
        $this->logger = $loggerUtilFactory->getLoggerUtil();
29
    }
30
31 2
    #[\Override]
32
    public function change(Ship $oldLeader): void
33
    {
34 2
        $fleet = $oldLeader->getFleet();
35 2
        if ($fleet === null) {
36
            throw new RuntimeException('no fleet available');
37
        }
38
39
        /** @var false|Ship */
40 2
        $newLeader = current(
41 2
            array_filter(
42 2
                $fleet->getShips()->toArray(),
43 2
                fn(Ship $ship): bool => $ship !== $oldLeader
44 2
            )
45 2
        );
46
47 2
        if ($newLeader === false) {
48 1
            $this->cancelColonyBlockOrDefend->work(
49 1
                $oldLeader,
50 1
                new InformationWrapper()
51 1
            );
52
        } else {
53 1
            $newLeader->setIsFleetLeader(true);
54 1
            $this->shipRepository->save($newLeader);
55
56 1
            $this->logger->logf('new leader of fleetId %d now is shipId %d', $fleet->getId(), $newLeader->getId());
57
58 1
            $fleet->setLeadShip($newLeader);
59 1
            $this->entityManager->flush();
60
        }
61
62 2
        $fleet->getShips()->removeElement($oldLeader);
63
64 2
        $oldLeader->setFleet(null);
65 2
        $oldLeader->setIsFleetLeader(false);
66 2
        $this->shipRepository->save($oldLeader);
67
68 2
        if ($newLeader === false) {
69 1
            $this->logger->logf('now deleting fleet %d', $fleet->getId());
70 1
            $this->fleetRepository->delete($fleet);
71
        } else {
72 1
            $this->logger->logf('changed fleet leader of fleet %d', $fleet->getId());
73
        }
74
    }
75
}
76