Passed
Pull Request — dev (#2303)
by Janko
05:56
created

WebEmitterSystemSettings::setTemplateVariables()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 64
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 18.6626

Importance

Changes 0
Metric Value
cc 12
eloc 38
c 0
b 0
f 0
nc 12
nop 3
dl 0
loc 64
ccs 25
cts 39
cp 0.641
crap 18.6626
rs 6.9666

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\Spacecraft;
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
    #[\Override]
23
    public function setTemplateVariables(
24
        SpacecraftSystemTypeEnum $systemType,
25
        SpacecraftWrapperInterface $wrapper,
26
        GameControllerInterface $game
27
    ): void {
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(
40
                'no web emitter installed',
41
                null,
42
                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...
43
            );
44
        }
45
46 1
        $webUnderConstruction = $emitter->getWebUnderConstruction();
47 1
        $ownWeb = $emitter->getOwnedTholianWeb();
48
49 1
        if ($ownWeb !== null && $ownWeb->isFinished()) {
50
            $game->setTemplateVar('OWNFINISHEDWEB', $ownWeb);
51
        }
52
53
        //helping under construction?
54 1
        if ($webUnderConstruction !== null && !$webUnderConstruction->isFinished()) {
55
            $game->setTemplateVar('WEBCONSTRUCT', $webUnderConstruction);
56
            $game->setTemplateVar('ISOWNCONSTRUCT', $webUnderConstruction === $ownWeb);
57
            return;
58
        }
59
60 1
        $web = $this->tholianWebRepository->getWebAtLocation($ship);
61
62 1
        if ($web === null) {
63
            // wenn keines da und isUseable -> dann Targetliste
64 1
            if ($emitter->isUseable()) {
65 1
                $possibleTargetList = $ship
66 1
                    ->getLocation()
67 1
                    ->getSpacecraftsWithoutVacation()
68 1
                    ->filter(
69 1
                        fn(Spacecraft $target): bool => (
70 1
                            !$target->isCloaked()
71 1
                            && !$target->isWarped()
72 1
                            && $target !== $ship
73 1
                        )
74 1
                    );
75
76 1
                $game->setTemplateVar('AVAILABLE_SHIPS', $possibleTargetList);
77
            } else {
78
                $game->setTemplateVar('COOLDOWN', $emitter->getCooldown());
79
            }
80
        } elseif (!$web->isFinished()) {
81
            //can help under construction?
82
            //fremdes Netz under construction da? -> dann button für Support
83
            $game->setTemplateVar('CANHELP', true);
84
        } else {
85
            $game->setTemplateVar('OWNFINISHED', $web->getUser()->getId() === $user->getId());
86
        }
87
    }
88
}
89