Passed
Push — dev ( 854b00...9416fb )
by Janko
09:28
created

ShipDisassemblyProvider::setTemplateVariables()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 23.1793

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 8
nop 2
dl 0
loc 38
ccs 8
cts 21
cp 0.381
crap 23.1793
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
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...
6
use RuntimeException;
7
use Stu\Component\Colony\OrbitShipWrappersRetrieverInterface;
8
use Stu\Lib\Colony\PlanetFieldHostProviderInterface;
9
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Repository\ShipRumpBuildingFunctionRepositoryInterface;
13
14
final class ShipDisassemblyProvider implements PlanetFieldHostComponentInterface
15
{
16 1
    public function __construct(
17
        private ShipRumpBuildingFunctionRepositoryInterface $shipRumpBuildingFunctionRepository,
18
        private PlanetFieldHostProviderInterface $planetFieldHostProvider,
19
        private ColonyLibFactoryInterface $colonyLibFactory,
20
        private OrbitShipWrappersRetrieverInterface $orbitShipWrappersRetriever
21 1
    ) {}
22
23
    /** @param ColonyInterface $entity */
24 1
    #[Override]
25
    public function setTemplateVariables(
26
        $entity,
27
        GameControllerInterface $game
28
    ): void {
29 1
        $field = $this->planetFieldHostProvider->loadFieldViaRequestParameter($game->getUser(), false);
30
31 1
        $building = $field->getBuilding();
32 1
        if ($building === null) {
33
            throw new RuntimeException('building is null');
34
        }
35
36 1
        $fieldFunctions = $building->getFunctions()->toArray();
37 1
        $colonySurface = $this->colonyLibFactory->createColonySurface($entity);
38
39 1
        if (!$colonySurface->hasShipyard()) {
40 1
            return;
41
        }
42
43
        $repairableShips = [];
44
        foreach ($this->orbitShipWrappersRetriever->retrieve($entity) as $group) {
45
46
            foreach ($group->getWrappers() as $wrapper) {
47
                $ship = $wrapper->get();
48
                if ($ship->getUser() !== $game->getUser()) {
49
                    continue;
50
                }
51
                foreach ($this->shipRumpBuildingFunctionRepository->getByShipRump($ship->getRump()) as $rump_rel) {
52
                    if (array_key_exists($rump_rel->getBuildingFunction()->value, $fieldFunctions)) {
53
                        $repairableShips[$ship->getId()] = $ship;
54
                        break;
55
                    }
56
                }
57
            }
58
        }
59
60
        $game->setTemplateVar('SHIP_LIST', $repairableShips);
61
        $game->setTemplateVar('FIELD', $field);
62
    }
63
}
64