Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

CommodityTransferStrategy::transfer()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 17
nc 14
nop 4
dl 0
loc 32
ccs 0
cts 17
cp 0
crap 72
rs 8.4444
c 1
b 1
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
17
class CommodityTransferStrategy implements TransferStrategyInterface
18
{
19 1
    public function __construct(
20
        private ColonyLibFactoryInterface $colonyLibFactory,
21
        private StatusBarFactoryInterface $statusBarFactory
22 1
    ) {}
23
24 9
    #[Override]
25
    public function setTemplateVariables(
26
        bool $isUnload,
27
        StorageEntityWrapperInterface $source,
28
        StorageEntityWrapperInterface $target,
29
        GameControllerInterface $game
30
    ): void {
31
32 9
        $beamableStorage = $isUnload ? $source->get()->getBeamableStorage() : $target->get()->getBeamableStorage();
33
34 9
        $game->setTemplateVar(
35 9
            'BEAMABLE_STORAGE',
36 9
            $beamableStorage
37 9
        );
38
39 9
        if ($target instanceof ColonyInterface) {
40
            $game->setTemplateVar(
41
                'SHOW_SHIELD_FREQUENCY',
42
                $this->colonyLibFactory->createColonyShieldingManager($target)->isShieldingEnabled() && $target->getUser() !== $source->getUser()
43
            );
44
        }
45
46 9
        $game->setTemplateVar('SOURCE_STORAGE_BAR', $this->createStorageBar($source));
47 9
        $game->setTemplateVar('TARGET_STORAGE_BAR', $this->createStorageBar($target));
48
    }
49
50 9
    private function createStorageBar(StorageEntityWrapperInterface $entityWrapper): string
51
    {
52 9
        return $this->statusBarFactory
53 9
            ->createStatusBar()
54 9
            ->setColor(StatusBarColorEnum::STATUSBAR_GREEN)
55 9
            ->setLabel(_('Lager'))
56 9
            ->setMaxValue($entityWrapper->get()->getMaxStorage())
57 9
            ->setValue($entityWrapper->get()->getStorageSum())
58 9
            ->render();
59
    }
60
61
    #[Override]
62
    public function transfer(
63
        bool $isUnload,
64
        StorageEntityWrapperInterface $source,
65
        StorageEntityWrapperInterface $target,
66
        InformationInterface $information
67
    ): void {
68
69
        $from = $isUnload ? $source : $target;
70
        if ($from->get()->getBeamableStorage()->isEmpty()) {
71
            $information->addInformation('Keine Waren zum Beamen vorhanden');
72
            return;
73
        }
74
75
        $commodities = request::postArray('commodities');
76
        $gcount = request::postArray('count');
77
        if (count($commodities) == 0 || count($gcount) == 0) {
78
            $information->addInformation("Es wurden keine Waren zum Beamen ausgewählt");
79
            return;
80
        }
81
82
        if (!$target->canPenetrateShields($source->getUser(), $information)) {
83
            return;
84
        }
85
86
        $destination = $isUnload ? $target : $source;
87
        if ($destination->get()->getStorageSum() >= $destination->get()->getMaxStorage()) {
88
            $information->addInformationf('Der Lagerraum der %s ist voll', $destination->getName());
89
            return;
90
        }
91
92
        $source->transfer($isUnload, $target, $information);
93
    }
94
}
95