Passed
Push — dev ( 7afdcb...e5a558 )
by Nico
14:32
created

CreateDatabaseEntry   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 19.35%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 69
ccs 6
cts 31
cp 0.1935
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B checkForCategoryCompletion() 0 24 10
A createDatabaseEntryForUser() 0 33 5
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\DatabaseCategoryAwardRepositoryInterface;
14
use Stu\Orm\Repository\DatabaseEntryRepositoryInterface;
15
use Stu\Orm\Repository\DatabaseUserRepositoryInterface;
16
17
final class CreateDatabaseEntry implements CreateDatabaseEntryInterface
18
{
19 2
    public function __construct(
20
        private DatabaseEntryRepositoryInterface $databaseEntryRepository,
21
        private DatabaseUserRepositoryInterface $databaseUserRepository,
22
        private CreatePrestigeLogInterface $createPrestigeLog,
23
        private CreateUserAwardInterface $createUserAward,
24
        private DatabaseCategoryAwardRepositoryInterface $databaseCategoryAwardRepository
25 2
    ) {}
26
27 1
    #[Override]
28
    public function createDatabaseEntryForUser(UserInterface $user, int $databaseEntryId): ?DatabaseEntryInterface
29
    {
30 1
        if ($databaseEntryId === 0) {
31
            return null;
32
        }
33
34 1
        if (!$user->hasColony()) {
35 1
            return null;
36
        }
37
38
        $databaseEntry = $this->databaseEntryRepository->find($databaseEntryId);
39
40
        if ($databaseEntry === null) {
41
            return null;
42
        }
43
44
        //create new user entry
45
        $userEntry = $this->databaseUserRepository->prototype()
46
            ->setUser($user)
47
            ->setDatabaseEntry($databaseEntry)
48
            ->setDate(time());
49
50
        $this->databaseUserRepository->save($userEntry);
51
52
        if (!$user->isNpc()) {
53
            //create prestige log
54
            $this->createPrestigeLog->createLogForDatabaseEntry($databaseEntry, $user, $userEntry->getDate());
55
56
            $this->checkForCategoryCompletion($user, $databaseEntry->getCategory(), $databaseEntryId, $databaseEntry->getLayerId());
57
        }
58
59
        return $databaseEntry;
60
    }
61
62
    public function checkForCategoryCompletion(
63
        UserInterface $user,
64
        DatabaseCategoryInterface $category,
65
        ?int $ignoredDatabaseEntryId = null,
66
        ?int $layerId = null
67
    ): void {
68
        if ($ignoredDatabaseEntryId === null && $layerId === null) {
69
            $layerIds = $this->databaseEntryRepository->getDistinctLayerIdsByCategory($category->getId());
70
71
            foreach ($layerIds as $currentLayerId) {
72
                if ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer($user->getId(), $category->getId(), null, $currentLayerId)) {
73
                    $categoryAward = $this->databaseCategoryAwardRepository->findByCategoryIdAndLayerId($category->getId(), $currentLayerId);
74
75
                    if ($categoryAward !== null && $categoryAward->getAward() !== null) {
76
                        $this->createUserAward->createAwardForUser($user, $categoryAward->getAward());
77
                    }
78
                }
79
            }
80
        } else {
81
            if ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer($user->getId(), $category->getId(), $ignoredDatabaseEntryId, $layerId)) {
82
                $categoryAward = $this->databaseCategoryAwardRepository->findByCategoryIdAndLayerId($category->getId(), $layerId);
83
84
                if ($categoryAward !== null && $categoryAward->getAward() !== null) {
85
                    $this->createUserAward->createAwardForUser($user, $categoryAward->getAward());
86
                }
87
            }
88
        }
89
    }
90
}
91