Passed
Push — master ( e37139...a291da )
by Nico
27:10 queued 12:31
created

ChangeFleetLeader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

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