Passed
Push — master ( 92a2e9...9d73f4 )
by Nico
41:55
created

TransformResources::handle()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 63
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 40
nc 28
nop 1
dl 0
loc 63
ccs 0
cts 40
cp 0
crap 156
rs 6.9666
c 2
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\Ship\Action\Mining;
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\Component\Ship\System\ShipSystemTypeEnum;
10
use Stu\Exception\SanityCheckException;
11
use Stu\Module\Control\ActionControllerInterface;
12
use Stu\Module\Control\GameControllerInterface;
13
use Stu\Module\Ship\Lib\ActivatorDeactivatorHelperInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Ship\View\ShowShip\ShowShip;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Ship\View\ShowShip\ShowShip 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...
16
use Stu\Module\Commodity\CommodityTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeEnum 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...
17
use Stu\Orm\Repository\BuildingCommodityRepositoryInterface;
18
use Stu\Orm\Repository\CommodityRepositoryInterface;
19
20
final class TransformResources implements ActionControllerInterface
21
{
22
    public const ACTION_IDENTIFIER = 'B_TRANSFORM_RESOURCES';
23
24
    public function __construct(
25
        private ShipLoaderInterface $shipLoader,
26
        private ActivatorDeactivatorHelperInterface $helper,
27
        private BuildingCommodityRepositoryInterface $buildingCommodityRepository,
28
        private CommodityRepositoryInterface $commodityRepository
29
    ) {}
30
31
    #[Override]
32
    public function handle(GameControllerInterface $game): void
33
    {
34
        $game->setView(ShowShip::VIEW_IDENTIFIER);
35
36
        $userId = $game->getUser()->getId();
37
        $shipId = request::indInt('id');
38
39
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
40
            $shipId,
41
            $userId
42
        );
43
44
        $ship = $wrapper->get();
45
        $aggregationsystem = $wrapper->getAggregationSystemSystemData();
46
47
        if ($aggregationsystem === null) {
48
            throw new SanityCheckException('collector = null ', self::ACTION_IDENTIFIER);
49
        }
50
51
        $commodityId = request::postInt('chosen');
52
53
54
55
        if ($commodityId === 0) {
56
            if ($ship->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM)) {
57
                $this->helper->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM, $game);
58
                $aggregationsystem->setCommodityId($commodityId)->update();
59
            }
60
            return;
61
        } else {
62
63
            if (!$ship->getSystemState(ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM)) {
64
                if (!$this->helper->activate($wrapper, ShipSystemTypeEnum::SYSTEM_AGGREGATION_SYSTEM, $game)) {
65
                    return;
66
                }
67
            }
68
69
            $canProduce = false;
70
            foreach (CommodityTypeEnum::COMMODITY_CONVERSIONS as $conversion) {
71
                if ($conversion[0] === $commodityId) {
72
                    $targetCommodityId = $conversion[1];
73
                    if ($this->buildingCommodityRepository->canProduceCommodity($userId, $targetCommodityId)) {
74
                        $canProduce = true;
75
                        $sourceCommodity = $this->commodityRepository->find($conversion[0]);
76
                        $targetCommodity = $this->commodityRepository->find($conversion[1]);
77
                        break;
78
                    }
79
                }
80
            }
81
82
            if (!$canProduce) {
83
                $game->addInformation("Diese Ressource kann nicht produziert werden");
84
                return;
85
            }
86
87
            $aggregationsystem->setCommodityId($commodityId)->update();
88
            if ($sourceCommodity &&  $targetCommodity) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sourceCommodity does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $targetCommodity does not seem to be defined for all execution paths leading up to this point.
Loading history...
89
                $game->addInformationf(
90
                    sprintf(
91
                        "%s wird in %s umgewandelt",
92
                        $sourceCommodity->getName(),
93
                        $targetCommodity->getName()
94
                    )
95
                );
96
            }
97
        }
98
    }
99
100
    #[Override]
101
    public function performSessionCheck(): bool
102
    {
103
        return true;
104
    }
105
}
106