Passed
Pull Request — master (#1863)
by Nico
55:34 queued 27:10
created

ManageReactor::createMissingCommoditiesMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 2
rs 9.9666
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 7
    public function __construct(private ReactorUtilInterface $reactorUtil, private CommodityCacheInterface $commodityCache, private PlayerRelationDeterminatorInterface $playerRelationDeterminator) {}
20
21 7
    #[Override]
22
    public function manage(ShipWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array
23
    {
24 7
        $values = $values['reactor'] ?? null;
25 7
        if ($values === null) {
26 1
            throw new RuntimeException('value array not existent');
27
        }
28
29 6
        $ship = $wrapper->get();
30
31 6
        if (!array_key_exists($ship->getId(), $values)) {
32 1
            return [];
33
        }
34
35 5
        if ($values[$ship->getId()] < 1) {
36 1
            return [];
37
        }
38
39 4
        $reactor = $wrapper->getReactorWrapper();
40 4
        if ($reactor === null) {
41 1
            return [];
42
        }
43
44 3
        if (!$this->playerRelationDeterminator->isFriend($ship->getUser(), $managerProvider->getUser()) && $ship->getShieldState()) {
45
            $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
                _('%s: Warpkern konnte wegen aktivierter Schilde nicht aufgeladen werden.'),
47
                $ship->getName()
48
            );
49
            return $msg;
50
        } else {
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
    /**
70
     * @return array<string>
71
     */
72 1
    private function createMissingCommoditiesMessage(ShipInterface $ship, ReactorWrapperInterface $reactor): array
73
    {
74 1
        $msg = [];
75
76 1
        $msg[] = sprintf(
77 1
            _('%s: Es werden mindestens folgende Waren zum Aufladen des %ss benötigt:'),
78 1
            $ship->getName(),
79 1
            $reactor->get()->getSystemType()->getDescription()
80 1
        );
81
82 1
        foreach ($reactor->get()->getLoadCost() as $commodityId => $loadCost) {
83 1
            $commodity = $this->commodityCache->get($commodityId);
84 1
            $msg[] = sprintf(_('%d %s'), $loadCost, $commodity->getName());
85
        }
86
87 1
        return $msg;
88
    }
89
}
90