|
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): 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); |
|
48
|
|
|
|
|
49
|
|
|
return $field; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
public function loadHostViaRequestParameters(UserInterface $user): 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); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
private function getHostInternal( |
|
68
|
|
|
int $id, |
|
69
|
|
|
PlanetFieldHostTypeEnum $hostType, |
|
70
|
|
|
UserInterface $user |
|
71
|
|
|
): PlanetFieldHostInterface { |
|
72
|
|
|
|
|
73
|
4 |
|
if ($hostType === PlanetFieldHostTypeEnum::COLONY) { |
|
74
|
1 |
|
return $this->colonyLoader->byIdAndUser( |
|
75
|
1 |
|
$id, |
|
76
|
1 |
|
$user->getId() |
|
77
|
1 |
|
); |
|
78
|
|
|
} |
|
79
|
3 |
|
$sandbox = $this->colonySandboxRepository->find($id); |
|
80
|
3 |
|
if ($sandbox === null) { |
|
81
|
1 |
|
throw new RuntimeException(sprintf('sandbox with following id does not exist: %d', $id)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
if ($sandbox->getUser() !== $user) { |
|
85
|
1 |
|
throw new SanityCheckException('sandbox does belong to other user'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return $sandbox; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|