Passed
Push — dev ( 0f0480...7566e1 )
by Janko
11:01
created

CreatePrestigeLog::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Prestige\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\Lib\Component\ComponentRegistrationInterface;
9
use Stu\Module\Game\Component\GameComponentEnum;
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
11
use Stu\Orm\Entity\DatabaseEntryInterface;
12
use Stu\Orm\Entity\UserInterface;
13
use Stu\Orm\Repository\PrestigeLogRepositoryInterface;
14
use Stu\Orm\Repository\UserRepositoryInterface;
15
16
final class CreatePrestigeLog implements CreatePrestigeLogInterface
17
{
18 2
    public function __construct(
19
        private PrestigeLogRepositoryInterface $prestigeLogRepository,
20
        private UserRepositoryInterface $userRepository,
21
        private ComponentRegistrationInterface $componentRegistration
22 2
    ) {}
23
24
    #[Override]
25
    public function createLog(int $amount, string $description, UserInterface $user, int $date): void
26
    {
27
        $this->createLogIntern($amount, $description, $user, $date);
28
    }
29
30
    #[Override]
31
    public function createLogForDatabaseEntry(DatabaseEntryInterface $databaseEntry, UserInterface $user, int $date): void
32
    {
33
        $amount = $databaseEntry->getCategory()->getPrestige();
34
        $description = sprintf(
35
            '%d Prestige erhalten für die Entdeckung von "%s" in der Kategorie "%s"',
36
            $amount,
37
            $databaseEntry->getDescription(),
38
            $databaseEntry->getCategory()->getDescription()
39
        );
40
41
        $this->createLogIntern($amount, $description, $user, $date);
42
    }
43
44
    private function createLogIntern(int $amount, string $description, UserInterface $user, int $date): void
45
    {
46
        if ($user->getId() < UserEnum::USER_FIRST_ID) {
47
            return;
48
        }
49
50
        $prestigeLog = $this->prestigeLogRepository->prototype();
51
        $prestigeLog->setUserId($user->getId());
52
        $prestigeLog->setAmount($amount);
53
        $prestigeLog->setDate($date);
54
        $prestigeLog->setDescription($description);
55
56
        $this->prestigeLogRepository->save($prestigeLog);
57
58
        //update user prestige
59
        $user->setPrestige($user->getPrestige() + $prestigeLog->getAmount());
60
        $this->userRepository->save($user);
61
62
        $this->componentRegistration->addComponentUpdate(GameComponentEnum::USER);
63
    }
64
}
65