HeaderLocator::getTenantFromRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vivait\TenantBundle\Locator;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
class HeaderLocator implements TenantLocator
8
{
9
    private $request;
10
11
    /**
12
     * @var string
13
     */
14
    private $headerKey;
15
16
    public function __construct(Request $request, $headerKey)
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...
17
    {
18
        $this->request = $request;
19
        $this->headerKey = $headerKey;
20
    }
21
22
    /**
23
     * @return string Tenant key
24
     */
25
    public function getTenant()
26
    {
27
        if (!$this->request->headers->has($this->headerKey)) {
28
            throw new \RuntimeException('Could not locate a tenant header in the request');
29
        }
30
31
        return $this->request->headers->get($this->headerKey);
32
    }
33
34
    /**
35
     * @param Request $request
36
     * @param string $headerKey
37
     * @return string Tenant key
38
     */
39
    public static function getTenantFromRequest(Request $request, $headerKey = 'tenant')
40
    {
41
        $locator = new self($request, $headerKey);
42
43
        return $locator->getTenant();
44
    }
45
}
46