Passed
Pull Request — master (#2027)
by Nico
21:19 queued 10:33
created

CreateDatabaseEntry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 1
cts 1
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\Database\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\Module\Award\Lib\CreateUserAwardInterface;
9
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
10
use Stu\Orm\Entity\DatabaseCategoryInterface;
11
use Stu\Orm\Entity\DatabaseEntryInterface;
12
use Stu\Orm\Entity\UserInterface;
13
use Stu\Orm\Repository\DatabaseEntryRepositoryInterface;
14
use Stu\Orm\Repository\DatabaseUserRepositoryInterface;
15
16
final class CreateDatabaseEntry implements CreateDatabaseEntryInterface
17
{
18 2
    public function __construct(
19
        private DatabaseEntryRepositoryInterface $databaseEntryRepository,
20
        private DatabaseUserRepositoryInterface $databaseUserRepository,
21
        private CreatePrestigeLogInterface $createPrestigeLog,
22
        private CreateUserAwardInterface $createUserAward
23
    ) {
24 2
    }
25
26 1
    #[Override]
27
    public function createDatabaseEntryForUser(UserInterface $user, int $databaseEntryId): ?DatabaseEntryInterface
28
    {
29 1
        if ($databaseEntryId === 0) {
30
            return null;
31
        }
32
33 1
        if (!$user->hasColony()) {
34 1
            return null;
35
        }
36
37
        $databaseEntry = $this->databaseEntryRepository->find($databaseEntryId);
38
39
        if ($databaseEntry === null) {
40
            return null;
41
        }
42
43
        //create new user entry
44
        $userEntry = $this->databaseUserRepository->prototype()
45
            ->setUser($user)
46
            ->setDatabaseEntry($databaseEntry)
47
            ->setDate(time());
48
49
        $this->databaseUserRepository->save($userEntry);
50
51
52
        if (!$user->isNpc()) {
53
            //create prestige log
54
            $this->createPrestigeLog->createLogForDatabaseEntry($databaseEntry, $user, $userEntry->getDate());
55
56
            $this->checkForCompletion($user, $databaseEntry->getCategory(), $databaseEntryId);
57
        }
58
59
        return $databaseEntry;
60
    }
61
62
    private function checkForCompletion(UserInterface $user, DatabaseCategoryInterface $category, int $databaseEntryId): void
63
    {
64
        //check if an award is configured for this category
65
        if ($category->getAward() === null) {
66
            return;
67
        }
68
69
        $award = $category->getAward();
70
71
        if ($this->databaseUserRepository->hasUserCompletedCategory($user->getId(), $category->getId(), $databaseEntryId)) {
72
            $this->createUserAward->createAwardForUser($user, $award);
73
        }
74
    }
75
}
76