Passed
Push — master ( 77a570...adeb98 )
by Nico
26:43
created

ShipRetrofitProvider::setTemplateVariables()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 8
nop 2
dl 0
loc 38
ccs 0
cts 20
cp 0
crap 90
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\OrbitShipListRetrieverInterface;
8
use Stu\Lib\Colony\PlanetFieldHostInterface;
9
use Stu\Lib\Colony\PlanetFieldHostProviderInterface;
10
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
13
use Stu\Orm\Entity\ColonyInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
use Stu\Orm\Repository\ShipRumpBuildingFunctionRepositoryInterface;
16
17
final class ShipRetrofitProvider implements GuiComponentProviderInterface
18
{
19
    public function __construct(private ShipRumpBuildingFunctionRepositoryInterface $shipRumpBuildingFunctionRepository, private PlanetFieldHostProviderInterface $planetFieldHostProvider, private ColonyLibFactoryInterface $colonyLibFactory, private OrbitShipListRetrieverInterface $orbitShipListRetriever, private ShipWrapperFactoryInterface $shipWrapperFactory) {}
20
21
    /** @param ColonyInterface $host */
22
    #[Override]
23
    public function setTemplateVariables(
24
        PlanetFieldHostInterface $host,
25
        GameControllerInterface $game
26
    ): void {
27
        $field = $this->planetFieldHostProvider->loadFieldViaRequestParameter($game->getUser(), false);
28
29
        $building = $field->getBuilding();
30
        if ($building === null) {
31
            throw new RuntimeException('building is null');
32
        }
33
34
        $fieldFunctions = $building->getFunctions()->toArray();
35
        $colonySurface = $this->colonyLibFactory->createColonySurface($host);
36
37
        if ($colonySurface->hasShipyard()) {
38
            $retrofitShips = [];
39
            foreach ($this->orbitShipListRetriever->retrieve($host) as $fleet) {
40
                /** @var ShipInterface $ship */
41
                foreach ($fleet['ships'] as $ship) {
42
                    $wrapper = $this->shipWrapperFactory->wrapShip($ship);
43
44
                    if (
45
                        !$wrapper->canBeRetrofitted() || $ship->isUnderRetrofit()
46
                    ) {
47
                        continue;
48
                    }
49
                    foreach ($this->shipRumpBuildingFunctionRepository->getByShipRump($ship->getRump()) as $rump_rel) {
50
                        if (array_key_exists($rump_rel->getBuildingFunction(), $fieldFunctions)) {
51
                            $retrofitShips[$ship->getId()] = $wrapper;
52
                            break;
53
                        }
54
                    }
55
                }
56
            }
57
58
            $game->setTemplateVar('RETROFIT_SHIP_LIST', $retrofitShips);
59
            $game->setTemplateVar('FIELD', $field);
60
        }
61
    }
62
}