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

CreatePrestigeLog   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 16%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 51
ccs 4
cts 25
cp 0.16
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createLogForDatabaseEntry() 0 11 1
A createLog() 0 3 1
A createLogIntern() 0 15 1
A __construct() 0 8 1
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