Passed
Push — dev ( b01017...737777 )
by Janko
05:10
created

FlightCompanyFactory::getMembers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
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 6
    public function create(SpacecraftWrapperInterface $leadWrapper): FlightCompany
16
    {
17 6
        $subject = $this->getSubject($leadWrapper);
18
19 6
        return new FlightCompany(
20 6
            $subject,
21 6
            $this->getMembers($subject),
22 6
            $this->preFlightConditionsCheck
23 6
        );
24
    }
25
26 6
    private function getSubject(SpacecraftWrapperInterface $leadWrapper): SpacecraftWrapperInterface|FleetWrapperInterface
27
    {
28 6
        $fleetWrapper = $leadWrapper->getFleetWrapper();
29 6
        if ($fleetWrapper === null) {
30
            return $leadWrapper;
31
        }
32
33 6
        $fleetLeader = $fleetWrapper->getLeadWrapper()->get();
34
35
        //TODO check for isFleetLeader too?
36 6
        return $fleetLeader === $leadWrapper->get()
0 ignored issues
show
introduced by
The condition $fleetLeader === $leadWrapper->get() is always false.
Loading history...
37 6
            ? $fleetWrapper
38 6
            : $leadWrapper;
39
    }
40
41
    /** @return Collection<int, covariant SpacecraftWrapperInterface> */
42 6
    private function getMembers(SpacecraftWrapperInterface|FleetWrapperInterface $subject): Collection
43
    {
44 6
        return $subject instanceof FleetWrapperInterface
45 6
            ? $subject->getShipWrappers()
46 6
            : new ArrayCollection([$subject->get()->getId() => $subject]);
47
    }
48
}
49