Passed
Pull Request — master (#1687)
by Nico
22:43
created

ManageReactor::manage()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8.0079

Importance

Changes 0
Metric Value
cc 8
eloc 20
c 0
b 0
f 0
nc 9
nop 3
dl 0
loc 36
ccs 19
cts 20
cp 0.95
crap 8.0079
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\ShipManagement\Manager;
6
7
use RuntimeException;
8
use Stu\Lib\ShipManagement\Provider\ManagerProviderInterface;
9
use Stu\Module\Commodity\Lib\CommodityCacheInterface;
10
use Stu\Module\Ship\Lib\ReactorUtilInterface;
11
use Stu\Module\Ship\Lib\ReactorWrapperInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperInterface;
13
use Stu\Orm\Entity\ShipInterface;
14
15
class ManageReactor implements ManagerInterface
16
{
17
    private ReactorUtilInterface $reactorUtil;
18
19
    private CommodityCacheInterface $commodityCache;
20
21 7
    public function __construct(
22
        ReactorUtilInterface $reactorUtil,
23
        CommodityCacheInterface $commodityCache
24
    ) {
25 7
        $this->reactorUtil = $reactorUtil;
26 7
        $this->commodityCache = $commodityCache;
27
    }
28
29 7
    public function manage(ShipWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array
30
    {
31 7
        $values = $values['reactor'] ?? null;
32 7
        if ($values === null) {
33 1
            throw new RuntimeException('value array not existent');
34
        }
35
36 6
        $ship = $wrapper->get();
37
38 6
        if (!array_key_exists($ship->getId(), $values)) {
39 1
            return [];
40
        }
41
42 5
        if ($values[$ship->getId()] < 1) {
43 1
            return [];
44
        }
45
46 4
        $reactor = $wrapper->getReactorWrapper();
47 4
        if ($reactor === null) {
48 1
            return [];
49
        }
50
51 3
        $storage = $managerProvider->getStorage();
52
53 3
        if ($this->reactorUtil->storageContainsNeededCommodities($storage, $reactor)) {
54 2
            $load = $values[$ship->getId()] == 'm' ? PHP_INT_MAX : (int)$values[$ship->getId()];
55 2
            $loadMessage = $this->reactorUtil->loadReactor($ship, $load, $managerProvider, $reactor);
56
57 2
            if ($loadMessage !== null) {
58 2
                return [$loadMessage];
59
            }
60
        } else {
61 1
            return $this->createMissingCommoditiesMessage($ship, $reactor);
62
        }
63
64
        return [];
65
    }
66
67
    /**
68
     * @return array<string>
69
     */
70 1
    private function createMissingCommoditiesMessage(ShipInterface $ship, ReactorWrapperInterface $reactor): array
71
    {
72 1
        $msg = [];
73
74 1
        $msg[] = sprintf(
75 1
            _('%s: Es werden mindestens folgende Waren zum Aufladen des %ss benötigt:'),
76 1
            $ship->getName(),
77 1
            $reactor->get()->getSystemType()->getDescription()
78 1
        );
79
80 1
        foreach ($reactor->get()->getLoadCost() as $commodityId => $loadCost) {
81 1
            $commodity = $this->commodityCache->get($commodityId);
82 1
            $msg[] = sprintf(_('%d %s'), $loadCost, $commodity->getName());
83
        }
84
85 1
        return $msg;
86
    }
87
}
88