Passed
Push — dev ( b6688d...d42601 )
by Janko
11:12
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\Orm\Entity\DatabaseEntryInterface;
11
use Stu\Orm\Entity\UserInterface;
12
use Stu\Orm\Repository\PrestigeLogRepositoryInterface;
13
use Stu\Orm\Repository\UserRepositoryInterface;
14
15
final class CreatePrestigeLog implements CreatePrestigeLogInterface
16
{
17 2
    public function __construct(
18
        private PrestigeLogRepositoryInterface $prestigeLogRepository,
19
        private UserRepositoryInterface $userRepository,
20
        private ComponentRegistrationInterface $componentRegistration
21 2
    ) {}
22
23
    #[Override]
24
    public function createLog(int $amount, string $description, UserInterface $user, int $date): void
25
    {
26
        $this->createLogIntern($amount, $description, $user, $date);
27
    }
28
29
    #[Override]
30
    public function createLogForDatabaseEntry(DatabaseEntryInterface $databaseEntry, UserInterface $user, int $date): void
31
    {
32
        $amount = $databaseEntry->getCategory()->getPrestige();
33
        $description = sprintf(
34
            '%d Prestige erhalten für die Entdeckung von "%s" in der Kategorie "%s"',
35
            $amount,
36
            $databaseEntry->getDescription(),
37
            $databaseEntry->getCategory()->getDescription()
38
        );
39
40
        $this->createLogIntern($amount, $description, $user, $date);
41
    }
42
43
    private function createLogIntern(int $amount, string $description, UserInterface $user, int $date): void
44
    {
45
        $prestigeLog = $this->prestigeLogRepository->prototype();
46
        $prestigeLog->setUserId($user->getId());
47
        $prestigeLog->setAmount($amount);
48
        $prestigeLog->setDate($date);
49
        $prestigeLog->setDescription($description);
50
51
        $this->prestigeLogRepository->save($prestigeLog);
52
53
        //update user prestige
54
        $user->setPrestige($user->getPrestige() + $prestigeLog->getAmount());
55
        $this->userRepository->save($user);
56
57
        $this->componentRegistration->addComponentUpdate(GameComponentEnum::USER);
58
    }
59
}
60