Passed
Push — dev ( 2eb11f...3c2dab )
by Janko
09:11
created

WebEmitterSystemSettings::setTemplateVariables()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 18.5574

Importance

Changes 0
Metric Value
cc 12
eloc 30
nc 12
nop 3
dl 0
loc 55
ccs 18
cts 28
cp 0.6429
crap 18.5574
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\View\ShowSystemSettings;
6
7
use RuntimeException;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
9
use Stu\Exception\SanityCheckException;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Ship\Lib\ShipWrapperInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Orm\Entity\SpacecraftInterface;
14
use Stu\Orm\Repository\TholianWebRepositoryInterface;
15
16
class WebEmitterSystemSettings implements SystemSettingsProviderInterface
17
{
18 1
    public function __construct(
19
        private TholianWebRepositoryInterface $tholianWebRepository
20 1
    ) {}
21
22 1
    public function setTemplateVariables(
23
        SpacecraftSystemTypeEnum $systemType,
24
        SpacecraftWrapperInterface $wrapper,
25
        GameControllerInterface $game
26
    ): void {
27
28 1
        if (!$wrapper instanceof ShipWrapperInterface) {
29
            throw new RuntimeException('this should not happen');
30
        }
31
32 1
        $user = $game->getUser();
33 1
        $ship = $wrapper->get();
34
35 1
        $game->setMacroInAjaxWindow('html/ship/webemitter.twig');
36
37 1
        $emitter = $wrapper->getWebEmitterSystemData();
38 1
        if ($emitter === null) {
39
            throw new SanityCheckException('no web emitter installed', null, ShowSystemSettings::VIEW_IDENTIFIER);
0 ignored issues
show
Bug introduced by
The type Stu\Module\Spacecraft\Vi...ings\ShowSystemSettings 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...
40
        }
41
42 1
        $webUnderConstruction = $emitter->getWebUnderConstruction();
43 1
        $ownWeb = $emitter->getOwnedTholianWeb();
44
45 1
        if ($ownWeb !== null && $ownWeb->isFinished()) {
46
            $game->setTemplateVar('OWNFINISHEDWEB', $ownWeb);
47
        }
48
49
        //helping under construction?
50 1
        if ($webUnderConstruction !== null && !$webUnderConstruction->isFinished()) {
51
            $game->setTemplateVar('WEBCONSTRUCT', $webUnderConstruction);
52
            $game->setTemplateVar('ISOWNCONSTRUCT', $webUnderConstruction === $ownWeb);
53
            return;
54
        }
55
56 1
        $web = $this->tholianWebRepository->getWebAtLocation($ship);
57
58 1
        if ($web === null) {
59
            // wenn keines da und isUseable -> dann Targetliste
60 1
            if ($emitter->isUseable()) {
61 1
                $possibleTargetList = $ship->getLocation()
62 1
                    ->getSpacecrafts()
63 1
                    ->filter(fn(SpacecraftInterface $target): bool => !$target->getCloakState() && !$target->isWarped() && $target !== $ship);
64
65 1
                $game->setTemplateVar('AVAILABLE_SHIPS', $possibleTargetList);
66
            } else {
67
                $game->setTemplateVar('COOLDOWN', $emitter->getCooldown());
68
            }
69
        } else {
70
71
            //can help under construction?
72
            //fremdes Netz under construction da? -> dann button für Support
73
            if (!$web->isFinished()) {
74
                $game->setTemplateVar('CANHELP', true);
75
            } else {
76
                $game->setTemplateVar('OWNFINISHED', $web->getUser() === $user);
77
            }
78
        }
79
    }
80
}
81