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

ShipRepairProvider::setTemplateVariables()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 29.8773

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 8
nop 2
dl 0
loc 41
ccs 8
cts 22
cp 0.3636
crap 29.8773
rs 8.0555
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 ShipRepairProvider 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
        $repairableShipWrappers = [];
44
        $groups = $this->orbitShipWrappersRetriever->retrieve($entity);
45
46
        foreach ($groups as $group) {
47
48
            foreach ($group->getWrappers() as $wrapper) {
49
50
                $ship = $wrapper->get();
51
                if (!$wrapper->canBeRepaired() || $ship->isUnderRepair()) {
52
                    continue;
53
                }
54
                foreach ($this->shipRumpBuildingFunctionRepository->getByShipRump($ship->getRump()) as $rump_rel) {
55
                    if (array_key_exists($rump_rel->getBuildingFunction()->value, $fieldFunctions)) {
56
                        $repairableShipWrappers[$ship->getId()] = $wrapper;
57
                        break;
58
                    }
59
                }
60
            }
61
        }
62
63
        $game->setTemplateVar('REPAIRABLE_SHIP_WRAPPERS', $repairableShipWrappers);
64
        $game->setTemplateVar('FIELD', $field);
65
    }
66
}
67