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

FlightCompanyFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 5%

Importance

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

4 Methods

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