Passed
Push — dev ( 5b1d95...923b08 )
by Janko
23:33 queued 07:31
created

BussardCollectorHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 4.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 67
ccs 2
cts 41
cp 0.0488
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C handleSpacecraftTick() 0 60 12
1
<?php
2
3
namespace Stu\Module\Tick\Spacecraft\Handler;
4
5
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...
6
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
9
use Stu\Module\Ship\Lib\ShipWrapperInterface;
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
use Stu\Orm\Repository\LocationMiningRepositoryInterface;
12
13
class BussardCollectorHandler implements SpacecraftTickHandlerInterface
14
{
15 1
    public function __construct(
16
        private LocationMiningRepositoryInterface $locationMiningRepository,
17
        private StorageManagerInterface $storageManager
18 1
    ) {}
19
20
    #[Override]
21
    public function handleSpacecraftTick(
22
        SpacecraftWrapperInterface $wrapper,
23
        InformationInterface $information
24
    ): void {
25
26
        if (!$wrapper instanceof ShipWrapperInterface) {
27
            return;
28
        }
29
30
        $ship = $wrapper->get();
31
        $bussard = $wrapper->getBussardCollectorSystemData();
32
        $miningqueue = $ship->getMiningQueue();
33
34
        if ($bussard === null) {
35
            return;
36
        }
37
38
        if ($miningqueue == null) {
39
            return;
40
        } else {
41
            $locationmining = $miningqueue->getLocationMining();
42
            $actualAmount = $locationmining->getActualAmount();
43
            $freeStorage = $ship->getMaxStorage() - $ship->getStorageSum();
44
            $module = $ship->getSpacecraftSystem(SpacecraftSystemTypeEnum::BUSSARD_COLLECTOR)->getModule();
45
            $gathercount = 0;
46
47
            if ($module !== null) {
48
                if ($module->getFactionId() == null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $module->getFactionId() of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
49
                    $gathercount =  (int) min(min(round(mt_rand(95, 105)), $actualAmount), $freeStorage);
50
                } else {
51
                    $gathercount = (int) min(min(round(mt_rand(190, 220)), $actualAmount), $freeStorage);
52
                }
53
            }
54
55
            $newAmount = $actualAmount - $gathercount;
56
            if ($gathercount > 0 && $locationmining->getDepletedAt() !== null) {
57
                $locationmining->setDepletedAt(null);
58
            }
59
            if ($newAmount == 0 && $actualAmount > 0) {
60
                $locationmining->setDepletedAt(time());
61
                $information->addInformationf(
62
                    'Es sind keine %s bei den Koordinaten %s|%s vorhanden!',
63
                    $locationmining->getCommodity()->getName(),
64
                    (string)$locationmining->getLocation()->getCx(),
65
                    (string)$locationmining->getLocation()->getCy()
66
                );
67
            }
68
            $locationmining->setActualAmount($newAmount);
69
70
            $this->locationMiningRepository->save($locationmining);
71
            if ($gathercount + $ship->getStorageSum() >= $ship->getMaxStorage()) {
72
                $information->addInformationf('Der Lagerraum des Schiffes wurde beim Sammeln von %s voll!', $locationmining->getCommodity()->getName());
73
            }
74
75
            if ($gathercount > 0) {
76
                $this->storageManager->upperStorage(
77
                    $ship,
78
                    $locationmining->getCommodity(),
79
                    $gathercount
80
                );
81
            }
82
        }
83
    }
84
}
85