Passed
Push — dev ( b2109c...60db09 )
by Janko
10:33
created

PirateTick   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 9.52%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 53
ccs 2
cts 21
cp 0.0952
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRandomBehaviourType() 0 5 1
A __construct() 0 10 1
A work() 0 28 3
1
<?php
2
3
namespace Stu\Module\Tick\Pirate;
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\Lib\Pirate\Behaviour\PirateBehaviourInterface;
7
use Stu\Lib\Pirate\Component\ReloadMinimalEpsInterface;
8
use Stu\Lib\Pirate\PirateBehaviourEnum;
9
use Stu\Lib\Pirate\PirateCreationInterface;
10
use Stu\Lib\Pirate\PirateReactionInterface;
11
use Stu\Lib\Pirate\PirateReactionMetadata;
12
use Stu\Module\Control\StuRandom;
13
use Stu\Module\Logging\LoggerUtilFactoryInterface;
14
use Stu\Module\Logging\PirateLoggerInterface;
15
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
16
17
final class PirateTick implements PirateTickInterface
18
{
19
    private PirateLoggerInterface $logger;
20
21
    /** @param array<int, PirateBehaviourInterface> $behaviours */
22 1
    public function __construct(
23
        private PirateCreationInterface $pirateCreation,
24
        private PirateReactionInterface $pirateReaction,
25
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
26
        private ReloadMinimalEpsInterface $reloadMinimalEps,
27
        private StuRandom $stuRandom,
28
        LoggerUtilFactoryInterface $loggerUtilFactory,
29
        private array $behaviours
30
    ) {
31 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
32
    }
33
34
    #[Override]
35
    public function work(): void
36
    {
37
        $this->logger->log('PIRATE TICK:');
38
39
        // create new pirates (max 5 fleets)
40
        $pirateFleets = $this->pirateCreation->createPirateFleetsIfNeeded();
41
42
        // process pirate fleets
43
        foreach ($pirateFleets as $fleet) {
44
            $behaviourType = $this->getRandomBehaviourType();
45
46
            if ($behaviourType === PirateBehaviourEnum::DO_NOTHING) {
47
                continue;
48
            }
49
50
            $this->logger->log(sprintf('pirateFleetId %d does %s', $fleet->getId(), $behaviourType->name));
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Lib\Pirate\PirateBehaviourEnum.
Loading history...
51
52
            $fleetWrapper = $this->spacecraftWrapperFactory->wrapFleet($fleet);
53
54
            $this->behaviours[$behaviourType->value]->action(
55
                $fleetWrapper,
56
                $this->pirateReaction,
57
                new PirateReactionMetadata(),
58
                null
59
            );
60
61
            $this->reloadMinimalEps->reload($fleetWrapper);
62
        }
63
    }
64
65
    private function getRandomBehaviourType(): PirateBehaviourEnum
66
    {
67
        $value = $this->stuRandom->randomKeyOfProbabilities(PirateBehaviourEnum::getBehaviourProbabilities());
68
69
        return PirateBehaviourEnum::from($value);
70
    }
71
}
72