Passed
Push — master ( 4cbcfa...1ee8de )
by Nico
53:18 queued 29:25
created

ColonyLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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