Passed
Push — master ( 3d9598...e4e562 )
by Nico
52:36 queued 23:35
created

PlanetFieldHostProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 75
ccs 25
cts 35
cp 0.7143
rs 10
c 1
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A loadHostViaRequestParameters() 0 13 3
A loadFieldViaRequestParameter() 0 16 3
A getHostInternal() 0 24 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Colony;
6
7
use request;
8
use RuntimeException;
9
use Stu\Exception\SanityCheckException;
10
use Stu\Module\Colony\Lib\ColonyLoaderInterface;
11
use Stu\Orm\Entity\PlanetFieldInterface;
12
use Stu\Orm\Entity\UserInterface;
13
use Stu\Orm\Repository\ColonySandboxRepositoryInterface;
14
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
15
16
class PlanetFieldHostProvider implements PlanetFieldHostProviderInterface
17
{
18
    private ColonySandboxRepositoryInterface $colonySandboxRepository;
19
20
    private PlanetFieldRepositoryInterface $planetFieldRepository;
21
22
    private ColonyLoaderInterface $colonyLoader;
23
24 6
    public function __construct(
25
        ColonySandboxRepositoryInterface $colonySandboxRepository,
26
        PlanetFieldRepositoryInterface $planetFieldRepository,
27
        ColonyLoaderInterface $colonyLoader
28
    ) {
29 6
        $this->colonySandboxRepository = $colonySandboxRepository;
30 6
        $this->planetFieldRepository = $planetFieldRepository;
31 6
        $this->colonyLoader = $colonyLoader;
32
    }
33
34
    public function loadFieldViaRequestParameter(UserInterface $user, bool $checkForEntityLock = true): PlanetFieldInterface
35
    {
36
        if (!request::has('fid')) {
37
            throw new RuntimeException(sprintf('request param "fid" is missing'));
38
        }
39
40
        $fid = request::indInt('fid');
41
        $field = $this->planetFieldRepository->find($fid);
42
        if ($field === null) {
43
            throw new RuntimeException(sprintf('planetField with following id does not exist: %s', $fid));
44
        }
45
46
        $host = $field->getHost();
47
        $this->getHostInternal($host->getId(), $host->getHostType(), $user, $checkForEntityLock);
48
49
        return $field;
50
    }
51
52 6
    public function loadHostViaRequestParameters(UserInterface $user, bool $checkForEntityLock = true): PlanetFieldHostInterface
53
    {
54 6
        if (!request::has('id')) {
55 1
            throw new RuntimeException(sprintf('request param "id" is missing'));
56
        }
57 5
        if (!request::has('hosttype')) {
58 1
            throw new RuntimeException(sprintf('request param "hosttype" is missing'));
59
        }
60
61 4
        $id = request::indInt('id');
62 4
        $hostType = PlanetFieldHostTypeEnum::from(request::indInt('hosttype'));
63
64 4
        return $this->getHostInternal($id, $hostType, $user, $checkForEntityLock);
65
    }
66
67 4
    private function getHostInternal(
68
        int $id,
69
        PlanetFieldHostTypeEnum $hostType,
70
        UserInterface $user,
71
        bool $checkForEntityLock
72
    ): PlanetFieldHostInterface {
73
74 4
        if ($hostType === PlanetFieldHostTypeEnum::COLONY) {
75 1
            return $this->colonyLoader->byIdAndUser(
76 1
                $id,
77 1
                $user->getId(),
78 1
                $checkForEntityLock
79 1
            );
80
        }
81 3
        $sandbox = $this->colonySandboxRepository->find($id);
82 3
        if ($sandbox === null) {
83 1
            throw new RuntimeException(sprintf('sandbox with following id does not exist: %d', $id));
84
        }
85
86 2
        if ($sandbox->getUser() !== $user) {
87 1
            throw new SanityCheckException('sandbox does belong to other user');
88
        }
89
90 1
        return $sandbox;
91
    }
92
}
93