Completed
Push — master ( 692aa1...25e5a7 )
by Paweł
60:22
created

TenantResolver::extractSubdomain()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 6
loc 19
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 5
eloc 10
nc 3
nop 1
crap 30
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\Repository\TenantRepositoryInterface;
19
20
/**
21
 * TenantResolver resolves the tenant based on subdomain.
22
 */
23
class TenantResolver implements TenantResolverInterface
24
{
25
    /**
26
     * @var TenantRepositoryInterface
27
     */
28
    private $tenantRepository;
29
30
    /**
31
     * TenantResolver constructor.
32
     *
33
     * @param TenantRepositoryInterface $tenantRepository
34
     */
35
    public function __construct(TenantRepositoryInterface $tenantRepository)
36
    {
37
        $this->tenantRepository = $tenantRepository;
38
    }
39
40
    /**
41 102
     * {@inheritdoc}
42
     */
43 102
    public function resolve($host = null)
44 102
    {
45 102
        $domain = $this->extractDomain($host);
46
        $subdomain = $this->extractSubdomain($host);
47
48 View Code Duplication
        if (null !== $subdomain) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $tenant = $this->tenantRepository->findOneBySubdomainAndDomain($subdomain, $domain);
50 102
        } else {
51
            $tenant = $this->tenantRepository->findOneByDomain($domain);
52 102
        }
53 102
54
        if (null === $tenant) {
55
            throw new TenantNotFoundException($host);
56 102
        }
57 102
58
        return $tenant;
59 102
    }
60
61
    /**
62
     * @param $host
63 102
     *
64
     * @return string
65
     */
66
    protected function extractDomain($host)
67
    {
68
        if (null === $host || TenantResolverInterface::LOCALHOST === $host) {
69
            return TenantResolverInterface::LOCALHOST;
70
        }
71
72
        $result = $this->extractHost($host);
73 102
74
        // handle case for ***.localhost
75 102 View Code Duplication
        if (TenantResolverInterface::LOCALHOST === $result->getSuffix() &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 94
            null !== $result->getHostname() &&
77
            null === $result->getSubdomain()
78
        ) {
79 102
            return $result->getSuffix();
80 102
        }
81 102
82 102
        $domainString = $result->getHostname();
83
        if (null !== $result->getSuffix()) {
84
            $domainString = $domainString.'.'.$result->getSuffix();
85 102
        }
86
87
        return $domainString;
88
    }
89
90
    /**
91
     * Extracts subdomain from the host.
92
     *
93
     * @param string $host Hostname
94
     *
95
     * @return string
96
     */
97
    protected function extractSubdomain($host)
98
    {
99
        $result = $this->extractHost($host);
100
101
        // handle case for ***.localhost
102 View Code Duplication
        if (TenantResolverInterface::LOCALHOST === $result->getSuffix() &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            null !== $result->getHostname() &&
104
            null === $result->getSubdomain()
105
        ) {
106
            return $result->getHostname();
107
        }
108
109
        $subdomain = $result->getSubdomain();
110
        if (null !== $subdomain) {
111
            return $subdomain;
112
        }
113
114
        return;
115
    }
116
117
    private function extractHost($host)
118
    {
119
        $extract = new \LayerShifter\TLDExtract\Extract();
120
121
        return $extract->parse($host);
122
    }
123
}
124