Passed
Push — master ( 5a11dc...578008 )
by Janko
08:39
created

CreateDatabaseEntry::createDatabaseEntryForUser()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 2
dl 0
loc 33
ccs 0
cts 17
cp 0
crap 30
rs 9.3888
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\DatabaseCategory;
11
use Stu\Orm\Entity\DatabaseEntry;
12
use Stu\Orm\Entity\User;
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
    #[Override]
28
    public function createDatabaseEntryForUser(User $user, int $databaseEntryId): ?DatabaseEntry
29
    {
30
        if ($databaseEntryId === 0) {
31
            return null;
32
        }
33
34
        if (!$user->hasColony()) {
35
            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
        User $user,
64
        DatabaseCategory $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
            foreach ($layerIds as $currentLayerId) {
71
                if ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer($user->getId(), $category->getId(), null, $currentLayerId)) {
72
                    $categoryAward = $this->databaseCategoryAwardRepository->findByCategoryIdAndLayerId($category->getId(), $currentLayerId);
73
74
                    if ($categoryAward !== null && $categoryAward->getAward() !== null) {
75
                        $this->createUserAward->createAwardForUser($user, $categoryAward->getAward());
76
                    }
77
                }
78
            }
79
        } elseif ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer($user->getId(), $category->getId(), $ignoredDatabaseEntryId, $layerId)) {
80
            $categoryAward = $this->databaseCategoryAwardRepository->findByCategoryIdAndLayerId($category->getId(), $layerId);
81
            if ($categoryAward !== null && $categoryAward->getAward() !== null) {
82
                $this->createUserAward->createAwardForUser($user, $categoryAward->getAward());
83
            }
84
        }
85
    }
86
}
87