Passed
Push — dev ( 1f861c...ab5646 )
by Janko
92:20
created

CreateDatabaseEntry::checkForCompletion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
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);
56
        }
57
58
        return $databaseEntry;
59
    }
60
61
    public function checkForCategoryCompletion(
62
        UserInterface $user,
63
        DatabaseCategoryInterface $category,
64
        ?int $ignoredDatabaseEntryId = null
65
    ): void {
66
        //check if an award is configured for this category
67
        if ($category->getAward() === null) {
68
            return;
69
        }
70
71
        $award = $category->getAward();
72
73
        if ($this->databaseUserRepository->hasUserCompletedCategory($user->getId(), $category->getId(), $ignoredDatabaseEntryId)) {
74
            $this->createUserAward->createAwardForUser($user, $award);
75
        }
76
    }
77
}
78