Passed
Pull Request — master (#2129)
by Nico
23:37 queued 13:36
created

CreateDatabaseEntry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 4
dl 0
loc 6
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\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 2
    ) {}
24
25 1
    #[Override]
26
    public function createDatabaseEntryForUser(UserInterface $user, int $databaseEntryId): ?DatabaseEntryInterface
27
    {
28 1
        if ($databaseEntryId === 0) {
29
            return null;
30
        }
31
32 1
        if (!$user->hasColony()) {
33 1
            return null;
34
        }
35
36
        $databaseEntry = $this->databaseEntryRepository->find($databaseEntryId);
37
38
        if ($databaseEntry === null) {
39
            return null;
40
        }
41
42
        //create new user entry
43
        $userEntry = $this->databaseUserRepository->prototype()
44
            ->setUser($user)
45
            ->setDatabaseEntry($databaseEntry)
46
            ->setDate(time());
47
48
        $this->databaseUserRepository->save($userEntry);
49
50
51
        if (!$user->isNpc()) {
52
            //create prestige log
53
            $this->createPrestigeLog->createLogForDatabaseEntry($databaseEntry, $user, $userEntry->getDate());
54
55
            $this->checkForCategoryCompletion($user, $databaseEntry->getCategory(), $databaseEntryId, $databaseEntry->getLayerId());
56
        }
57
58
        return $databaseEntry;
59
    }
60
61
    public function checkForCategoryCompletion(
62
        UserInterface $user,
63
        DatabaseCategoryInterface $category,
64
        ?int $ignoredDatabaseEntryId = null,
65
        ?int $layerId = null
66
    ): void {
67
        //check if an award is configured for this category
68
        if ($category->getAward() === null) {
69
            return;
70
        }
71
72
        $award = $category->getAward();
73
74
        if ($ignoredDatabaseEntryId === null && $layerId === null) {
75
            $layerIds = $this->databaseEntryRepository->getDistinctLayerIdsByCategory($category->getId());
76
77
            foreach ($layerIds as $currentLayerId) {
78
                if ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer($user->getId(), $category->getId(), null, $currentLayerId)) {
79
                    $this->createUserAward->createAwardForUser($user, $award);
80
                }
81
            }
82
        } else {
83
            if ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer($user->getId(), $category->getId(), $ignoredDatabaseEntryId, $layerId)) {
84
                $this->createUserAward->createAwardForUser($user, $award);
85
            }
86
        }
87
    }
88
}
89