Passed
Push — master ( 44623e...7a8014 )
by Janko
61:11 queued 21:50
created

ManageReactor::manage()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.0056

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 10
nop 3
dl 0
loc 45
ccs 25
cts 26
cp 0.9615
crap 10.0056
rs 7.6666
c 0
b 0
f 0

How to fix   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\Lib\ShipManagement\Manager;
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 RuntimeException;
9
use Stu\Lib\ShipManagement\Provider\ManagerProviderInterface;
10
use Stu\Component\Player\Relation\PlayerRelationDeterminatorInterface;
11
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
12
use Stu\Module\Ship\Lib\ReactorUtilInterface;
13
use Stu\Module\Ship\Lib\ReactorWrapperInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
17
class ManageReactor implements ManagerInterface
18
{
19 8
    public function __construct(private ReactorUtilInterface $reactorUtil, private CommodityCacheInterface $commodityCache, private PlayerRelationDeterminatorInterface $playerRelationDeterminator) {}
20
21 8
    #[Override]
22
    public function manage(ShipWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array
23
    {
24 8
        $values = $values['reactor'] ?? null;
25 8
        if ($values === null) {
26 1
            throw new RuntimeException('value array not existent');
27
        }
28
29 7
        $ship = $wrapper->get();
30
31 7
        if (!array_key_exists($ship->getId(), $values)) {
32 1
            return [];
33
        }
34
35 6
        if ($values[$ship->getId()] < 1) {
36 1
            return [];
37
        }
38
39 5
        $reactor = $wrapper->getReactorWrapper();
40 5
        if ($reactor === null) {
41 1
            return [];
42
        }
43
44 4
        if ($ship->getShieldState() && !$this->playerRelationDeterminator->isFriend($ship->getUser(), $managerProvider->getUser())) {
45 1
            $msg[] = sprintf(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$msg was never initialized. Although not strictly required by PHP, it is generally a good practice to add $msg = array(); before regardless.
Loading history...
46 1
                _('%s: Warpkern konnte wegen aktivierter Schilde nicht aufgeladen werden.'),
47 1
                $ship->getName()
48 1
            );
49 1
            return $msg;
50
        }
51
52 3
        $storage = $managerProvider->getStorage();
53
54 3
        if ($this->reactorUtil->storageContainsNeededCommodities($storage, $reactor)) {
55 2
            $load = $values[$ship->getId()] == 'm' ? PHP_INT_MAX : (int)$values[$ship->getId()];
56 2
            $loadMessage = $this->reactorUtil->loadReactor($ship, $load, $managerProvider, $reactor);
57
58 2
            if ($loadMessage !== null) {
59 2
                return [$loadMessage];
60
            }
61
        } else {
62 1
            return $this->createMissingCommoditiesMessage($ship, $reactor);
63
        }
64
65
        return [];
66
    }
67
68
    /**
69
     * @return array<string>
70
     */
71 1
    private function createMissingCommoditiesMessage(ShipInterface $ship, ReactorWrapperInterface $reactor): array
72
    {
73 1
        $msg = [];
74
75 1
        $msg[] = sprintf(
76 1
            _('%s: Es werden mindestens folgende Waren zum Aufladen des %ss benötigt:'),
77 1
            $ship->getName(),
78 1
            $reactor->get()->getSystemType()->getDescription()
79 1
        );
80
81 1
        foreach ($reactor->get()->getLoadCost() as $commodityId => $loadCost) {
82 1
            $commodity = $this->commodityCache->get($commodityId);
83 1
            $msg[] = sprintf(_('%d %s'), $loadCost, $commodity->getName());
84
        }
85
86 1
        return $msg;
87
    }
88
}
89