Passed
Push — dev ( 6e0cf6...2a5475 )
by Janko
14:36
created

FlightCompanyFactory::getMembers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Movement;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Stu\Module\Ship\Lib\FleetWrapperInterface;
8
use Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight\PreFlightConditionsCheckInterface;
9
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
10
11
class FlightCompanyFactory
12
{
13 1
    public function __construct(private PreFlightConditionsCheckInterface $preFlightConditionsCheck) {}
14
15
    public function create(SpacecraftWrapperInterface $leadWrapper): FlightCompany
16
    {
17
        $subject = $this->getSubject($leadWrapper);
18
19
        return new FlightCompany(
20
            $subject,
21
            $this->getMembers($subject),
22
            $this->preFlightConditionsCheck
23
        );
24
    }
25
26
    private function getSubject(SpacecraftWrapperInterface $leadWrapper): SpacecraftWrapperInterface|FleetWrapperInterface
27
    {
28
        $fleetWrapper = $leadWrapper->getFleetWrapper();
29
        if ($fleetWrapper === null) {
30
            return $leadWrapper;
31
        }
32
33
        $fleetLeader = $fleetWrapper->getLeadWrapper()->get();
34
35
        //TODO check for isFleetLeader too?
36
        return $fleetLeader === $leadWrapper->get()
0 ignored issues
show
introduced by
The condition $fleetLeader === $leadWrapper->get() is always false.
Loading history...
37
            ? $fleetWrapper
38
            : $leadWrapper;
39
    }
40
41
    /** @return Collection<int, covariant SpacecraftWrapperInterface> */
42
    private function getMembers(SpacecraftWrapperInterface|FleetWrapperInterface $subject): Collection
43
    {
44
        return $subject instanceof FleetWrapperInterface
45
            ? $subject->getShipWrappers()
46
            : new ArrayCollection([$subject->get()->getId() => $subject]);
47
    }
48
}
49