Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

CreatePrestigeLog::createLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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