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

ShipRetrofitProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 54
ccs 10
cts 24
cp 0.4167
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B setTemplateVariables() 0 44 9
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\Module\Ship\Lib\ShipWrapperInterface;
12
use Stu\Orm\Entity\ColonyInterface;
13
use Stu\Orm\Repository\ShipRumpBuildingFunctionRepositoryInterface;
14
15
final class ShipRetrofitProvider implements PlanetFieldHostComponentInterface
16
{
17 1
    public function __construct(
18
        private ShipRumpBuildingFunctionRepositoryInterface $shipRumpBuildingFunctionRepository,
19
        private PlanetFieldHostProviderInterface $planetFieldHostProvider,
20
        private ColonyLibFactoryInterface $colonyLibFactory,
21
        private OrbitShipWrappersRetrieverInterface $orbitShipWrappersRetriever
22 1
    ) {}
23
24
    /** @param ColonyInterface $entity */
25 1
    #[Override]
26
    public function setTemplateVariables(
27
        $entity,
28
        GameControllerInterface $game
29
    ): void {
30 1
        $field = $this->planetFieldHostProvider->loadFieldViaRequestParameter($game->getUser(), false);
31
32 1
        $building = $field->getBuilding();
33 1
        if ($building === null) {
34
            throw new RuntimeException('building is null');
35
        }
36
37 1
        $fieldFunctions = $building->getFunctions()->toArray();
38 1
        $colonySurface = $this->colonyLibFactory->createColonySurface($entity);
39
40 1
        if (!$colonySurface->hasShipyard()) {
41 1
            return;
42
        }
43
44
        $retrofitShipWrappers = [];
45
        $groups = $this->orbitShipWrappersRetriever->retrieve($entity);
46
47
        foreach ($groups as $group) {
48
49
            /** @var ShipWrapperInterface $wrapper */
50
            foreach ($group->getWrappers() as $wrapper) {
51
52
                $ship = $wrapper->get();
53
                if (
54
                    !$wrapper->canBeRetrofitted() || $ship->isUnderRetrofit()
55
                ) {
56
                    continue;
57
                }
58
                foreach ($this->shipRumpBuildingFunctionRepository->getByShipRump($ship->getRump()) as $rump_rel) {
59
                    if (array_key_exists($rump_rel->getBuildingFunction()->value, $fieldFunctions)) {
60
                        $retrofitShipWrappers[$ship->getId()] = $wrapper;
61
                        break;
62
                    }
63
                }
64
            }
65
        }
66
67
        $game->setTemplateVar('RETROFIT_SHIP_WRAPPERS', $retrofitShipWrappers);
68
        $game->setTemplateVar('FIELD', $field);
69
    }
70
}
71