Passed
Push — master ( ad0a87...920d42 )
by Nico
05:24
created

EntryCreator::addEntry()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 12
nop 3
dl 0
loc 32
ccs 0
cts 20
cp 0
crap 42
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\History\Lib;
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 Stu\Component\History\HistoryTypeEnum;
9
use Stu\Lib\Map\EntityWithLocationInterface;
10
use Stu\Orm\Entity\Alliance;
11
use Stu\Orm\Entity\Colony;
12
use Stu\Orm\Entity\Location;
13
use Stu\Orm\Entity\Spacecraft;
14
use Stu\Orm\Repository\HistoryRepositoryInterface;
15
16
final class EntryCreator implements EntryCreatorInterface
17
{
18 2
    public function __construct(private HistoryRepositoryInterface $historyRepository) {}
19
20
    #[Override]
21
    public function addEntry(
22
        string $text,
23
        int $sourceUserId,
24
        Spacecraft|Colony|Alliance $target
25
    ): void {
26
27
        if ($target instanceof Spacecraft) {
28
            $type = $target->isStation() ? HistoryTypeEnum::STATION : HistoryTypeEnum::SHIP;
0 ignored issues
show
Bug introduced by
The method isStation() does not exist on Stu\Orm\Entity\Colony. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            $type = $target->/** @scrutinizer ignore-call */ isStation() ? HistoryTypeEnum::STATION : HistoryTypeEnum::SHIP;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method isStation() does not exist on Stu\Orm\Entity\Alliance. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            $type = $target->/** @scrutinizer ignore-call */ isStation() ? HistoryTypeEnum::STATION : HistoryTypeEnum::SHIP;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
            $targetUser = $target->getUser();
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on Stu\Orm\Entity\Alliance. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            /** @scrutinizer ignore-call */ 
30
            $targetUser = $target->getUser();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
        } elseif ($target instanceof Colony) {
31
            $type = HistoryTypeEnum::COLONY;
32
            $targetUser = $target->getUser();
33
        } else {
34
            $type = HistoryTypeEnum::ALLIANCE;
35
            $targetUser = $target->getFounder()->getUser();
36
        }
37
38
        if ($targetUser === null) {
39
            return;
40
        }
41
42
        $location = $target instanceof EntityWithLocationInterface
43
            ? $target->getLocation()
44
            : null;
45
46
        $this->createEntry(
47
            $type,
48
            $text,
49
            $sourceUserId,
50
            $targetUser->getId(),
51
            $location
52
        );
53
    }
54
55
    #[Override]
56
    public function createEntry(
57
        HistoryTypeEnum $type,
58
        string $text,
59
        int $sourceUserId,
60
        int $targetUserId,
61
        ?Location $location = null
62
    ): void {
63
        $entry = $this->historyRepository->prototype();
64
        $entry->setText($text);
65
        $entry->setSourceUserId($sourceUserId);
66
        $entry->setTargetUserId($targetUserId);
67
        $entry->setDate(time());
68
        $entry->setType($type);
69
        $entry->setLocation($location);
70
71
        $this->historyRepository->save($entry);
72
    }
73
}
74