Completed
Push — master ( a39cf7...509ea1 )
by Matt
01:41
created

GetParameterLocator::getTenant()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
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)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
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