Passed
Push — dev ( 996fbb...25004f )
by Janko
16:40
created

CommodityTransferStrategy::shieldsAreBlocking()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 3
nop 3
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer\Strategy;
6
7
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...
8
use request;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Lib\Transfer\Wrapper\StorageEntityWrapperInterface;
11
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Template\StatusBarColorEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Template\StatusBarColorEnum 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...
14
use Stu\Module\Template\StatusBarFactoryInterface;
15
use Stu\Orm\Entity\ColonyInterface;
16
use Stu\Orm\Entity\UserInterface;
17
18
class CommodityTransferStrategy implements TransferStrategyInterface
19
{
20 1
    public function __construct(
21
        private ColonyLibFactoryInterface $colonyLibFactory,
22
        private StatusBarFactoryInterface $statusBarFactory
23 1
    ) {}
24
25 9
    #[Override]
26
    public function setTemplateVariables(
27
        bool $isUnload,
28
        StorageEntityWrapperInterface $source,
29
        StorageEntityWrapperInterface $targetWrapper,
30
        GameControllerInterface $game
31
    ): void {
32
33 9
        $target = $targetWrapper->get();
34 9
        $beamableStorage = $isUnload ? $source->get()->getBeamableStorage() : $target->getBeamableStorage();
35
36 9
        $game->setTemplateVar(
37 9
            'BEAMABLE_STORAGE',
38 9
            $beamableStorage
39 9
        );
40
41 9
        if ($target instanceof ColonyInterface) {
42 2
            $game->setTemplateVar(
43 2
                'SHOW_SHIELD_FREQUENCY',
44 2
                $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled() && $target->getUser() !== $source->getUser()
45 2
            );
46
        }
47
48 9
        $game->setTemplateVar('SOURCE_STORAGE_BAR', $this->createStorageBar($source));
49 9
        $game->setTemplateVar('TARGET_STORAGE_BAR', $this->createStorageBar($targetWrapper));
50
    }
51
52 9
    private function createStorageBar(StorageEntityWrapperInterface $entityWrapper): string
53
    {
54 9
        return $this->statusBarFactory
55 9
            ->createStatusBar()
56 9
            ->setColor(StatusBarColorEnum::STATUSBAR_GREEN)
57 9
            ->setLabel(_('Lager'))
58 9
            ->setMaxValue($entityWrapper->get()->getMaxStorage())
59 9
            ->setValue($entityWrapper->get()->getStorageSum())
60 9
            ->render();
61
    }
62
63
    #[Override]
64
    public function transfer(
65
        bool $isUnload,
66
        StorageEntityWrapperInterface $source,
67
        StorageEntityWrapperInterface $target,
68
        InformationInterface $information
69
    ): void {
70
71
        $from = $isUnload ? $source : $target;
72
        if ($from->get()->getBeamableStorage()->isEmpty()) {
73
            $information->addInformation('Keine Waren zum Beamen vorhanden');
74
            return;
75
        }
76
77
        $commodities = request::postArray('commodities');
78
        $gcount = request::postArray('count');
79
        if (count($commodities) == 0 || count($gcount) == 0) {
80
            $information->addInformation("Es wurden keine Waren zum Beamen ausgewählt");
81
            return;
82
        }
83
84
        if ($this->shieldsAreBlocking($target, $source->getUser(), $information)) {
85
            return;
86
        }
87
88
        $destination = $isUnload ? $target : $source;
89
        if ($destination->get()->getStorageSum() >= $destination->get()->getMaxStorage()) {
90
            $information->addInformationf('Der Lagerraum der %s ist voll', $destination->getName());
91
            return;
92
        }
93
94
        $source->transfer($isUnload, $target, $information);
95
    }
96
97
    private function shieldsAreBlocking(StorageEntityWrapperInterface $targetWrapper, UserInterface $user, InformationInterface $information): bool
98
    {
99
        $target = $targetWrapper->get();
100
101
        if (
102
            $target instanceof ColonyInterface
103
            && $target->getUser() !== $user
104
            && $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled()
105
            && $target->getShieldFrequency()
0 ignored issues
show
Bug Best Practice introduced by
The expression $target->getShieldFrequency() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
106
        ) {
107
            $frequency = request::postInt('frequency');
108
            if ($frequency !== $target->getShieldFrequency()) {
109
                $information->addInformation("Die Schildfrequenz ist nicht korrekt");
110
                return true;
111
            }
112
        }
113
114
        return false;
115
    }
116
}
117