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

CommodityTransferStrategy   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 47.5%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 36
dl 0
loc 76
ccs 19
cts 40
cp 0.475
rs 10
c 2
b 1
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setTemplateVariables() 0 24 4
A __construct() 0 4 1
A createStorageBar() 0 9 1
B transfer() 0 32 8
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