1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Component. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Component\MultiTenancy\Resolver; |
16
|
|
|
|
17
|
|
|
use SWP\Component\MultiTenancy\Exception\TenantNotFoundException; |
18
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
19
|
|
|
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TenantResolver resolves the tenant based on subdomain. |
23
|
|
|
*/ |
24
|
|
|
class TenantResolver implements TenantResolverInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $domain; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TenantRepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $tenantRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Construct. |
38
|
|
|
* |
39
|
|
|
* @param string $domain |
40
|
|
|
* @param TenantRepositoryInterface $tenantRepository |
41
|
|
|
*/ |
42
|
|
|
public function __construct($domain, TenantRepositoryInterface $tenantRepository) |
43
|
|
|
{ |
44
|
|
|
$this->domain = $domain; |
45
|
|
|
$this->tenantRepository = $tenantRepository; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function resolve($host = null) |
52
|
|
|
{ |
53
|
|
|
if (null === $host) { |
54
|
|
|
$host = self::DEFAULT_TENANT; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$tenant = $this->tenantRepository->findBySubdomain($this->extractSubdomain($host)); |
58
|
|
|
|
59
|
|
|
$this->assertTenantIsFound($tenant); |
60
|
|
|
|
61
|
|
|
return $tenant; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Extracts subdomain from the host. |
66
|
|
|
* |
67
|
|
|
* @param string $host Hostname |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function extractSubdomain($host) |
72
|
|
|
{ |
73
|
|
|
if ($this->domain === $host) { |
74
|
|
|
return self::DEFAULT_TENANT; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$parts = explode('.', str_replace('.'.$this->domain, '', $host)); |
78
|
|
|
$subdomain = self::DEFAULT_TENANT; |
79
|
|
|
if (count($parts) === 1 && $parts[0] !== 'www') { |
80
|
|
|
$subdomain = $parts[0]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $subdomain; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param TenantInterface|null $tenant |
88
|
|
|
* |
89
|
|
|
* @throws TenantNotFoundException |
90
|
|
|
*/ |
91
|
|
|
private function assertTenantIsFound(TenantInterface $tenant = null) |
92
|
|
|
{ |
93
|
|
|
if (null === $tenant) { |
94
|
|
|
throw new TenantNotFoundException(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|