Passed
Push — dev ( ecf2ba...80e888 )
by Janko
07:58
created

GameState::getGameConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Control;
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\Game\GameStateEnum;
7
use Stu\Exception\MaintenanceGameStateException;
8
use Stu\Exception\RelocationGameStateException;
9
use Stu\Exception\ResetGameStateException;
10
use Stu\Orm\Entity\GameConfig;
11
use Stu\Orm\Repository\GameConfigRepositoryInterface;
12
13
final class GameState implements GameStateInterface
0 ignored issues
show
Bug introduced by
The type Stu\Module\Control\GameStateInterface 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...
14
{
15
    /** @var GameConfig[]|null */
16
    private ?array $gameConfig = null;
17
18 12
    public function __construct(
19
        private readonly GameConfigRepositoryInterface $gameConfigRepository
20 12
    ) {}
21
22 220
    #[Override]
23
    public function getGameState(): GameStateEnum
24
    {
25 220
        return GameStateEnum::from($this->getGameConfig()[self::CONFIG_GAMESTATE]->getValue());
26
    }
27
28 10
    #[Override]
29
    public function checkGameState(bool $isAdmin): void
30
    {
31 10
        $gameState = $this->getGameState();
32
33 10
        if ($gameState === GameStateEnum::MAINTENANCE && !$isAdmin) {
34 1
            throw new MaintenanceGameStateException();
35
        }
36
37 9
        if ($gameState === GameStateEnum::RESET) {
38 2
            throw new ResetGameStateException();
39
        }
40
41 7
        if ($gameState === GameStateEnum::RELOCATION) {
42 2
            throw new RelocationGameStateException();
43
        }
44
    }
45
    /**
46
     * @return array<int, GameConfig>
47
     */
48 220
    private function getGameConfig(): array
49
    {
50 220
        if ($this->gameConfig === null) {
51 11
            $this->gameConfig = [];
52
53 11
            foreach ($this->gameConfigRepository->findAll() as $item) {
54 11
                $this->gameConfig[$item->getOption()] = $item;
55
            }
56
        }
57 220
        return $this->gameConfig;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->gameConfig could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
58
    }
59
}
60