1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vivait\TenantBundle\Locator; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Vivait\TenantBundle\Registry\TenantRegistry; |
7
|
|
|
|
8
|
|
|
class GetParameterLocator |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Request |
13
|
|
|
*/ |
14
|
|
|
private $request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $parameterName; |
20
|
|
|
/** |
21
|
|
|
* @var TenantRegistry |
22
|
|
|
*/ |
23
|
|
|
private $registry; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Request $request |
27
|
|
|
* @param string $parameterName |
28
|
|
|
* @param TenantRegistry $registry |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Request $request, $parameterName, TenantRegistry $registry) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->request = $request; |
33
|
|
|
$this->parameterName = $parameterName; |
34
|
|
|
$this->registry = $registry; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws \RuntimeException |
39
|
|
|
* @throws \OutOfBoundsException |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getTenant() |
43
|
|
|
{ |
44
|
|
|
$tenantName = $this->request->get($this->parameterName, null); |
45
|
|
|
|
46
|
|
|
if ( ! isset($tenantName)) { |
47
|
|
|
throw new \RuntimeException; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ( ! $this->registry->contains($tenantName)) { |
51
|
|
|
throw new \OutOfBoundsException; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $tenantName; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Request $request |
59
|
|
|
* @param string $parameterName |
60
|
|
|
* @param TenantRegistry $registry |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public static function getTenantFromRequest(Request $request, $parameterName = 'tenant', TenantRegistry $registry) |
65
|
|
|
{ |
66
|
|
|
$locator = new self($request, $parameterName, $registry); |
67
|
|
|
|
68
|
|
|
return $locator->getTenant(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|