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

FlightCompanyFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 36
ccs 19
cts 20
cp 0.95
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A create() 0 8 1
A getSubject() 0 13 3
A getMembers() 0 5 2
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