Passed
Push — dev ( 392257...472d6e )
by Janko
10:08
created

ColonyLoader::loadInternal()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\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\Exception\AccessViolation;
9
use Stu\Exception\EntityLockedException;
10
use Stu\Module\Tick\Lock\LockManagerInterface;
11
use Stu\Module\Tick\Lock\LockTypeEnum;
12
use Stu\Orm\Entity\ColonyInterface;
13
use Stu\Orm\Repository\ColonyRepositoryInterface;
14
15
final class ColonyLoader implements ColonyLoaderInterface
16
{
17 5
    public function __construct(private ColonyRepositoryInterface $colonyRepository, private LockManagerInterface $lockManager)
18
    {
19 5
    }
20
21 36
    #[Override]
22
    public function loadWithOwnerValidation(int $colonyId, int $userId, bool $checkForEntityLock = true): ColonyInterface
23
    {
24 36
        $colony = $this->loadInternal($colonyId, $checkForEntityLock);
25
26 34
        if ($colony->getUserId() !== $userId) {
27 1
            throw new AccessViolation(sprintf("Colony owned by another user (%d)! Fool: %d", $colony->getUserId(), $userId));
28
        }
29
30 33
        return $colony;
31
    }
32
33 1
    #[Override]
34
    public function load(int $colonyId, bool $checkForEntityLock = true): ColonyInterface
35
    {
36 1
        return $this->loadInternal($colonyId, $checkForEntityLock);
37
    }
38
39 37
    private function loadInternal(int $colonyId, bool $checkForEntityLock): ColonyInterface
40
    {
41 37
        if ($checkForEntityLock && $this->lockManager->isLocked($colonyId, LockTypeEnum::COLONY_GROUP)) {
42 1
            throw new EntityLockedException('Tick läuft gerade, Zugriff auf Kolonie ist daher blockiert');
43
        }
44
45 36
        $colony = $this->colonyRepository->find($colonyId);
46 36
        if ($colony === null) {
47 1
            throw new AccessViolation("Colony not existent!");
48
        }
49
50 35
        return $colony;
51
    }
52
}
53