Completed
Push — master ( 0bd33c...f1035c )
by Damian
07:12
created

MultiDomain   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 76
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A domain_for_url() 0 12 3
A get_all_domains() 0 13 4
A get_primary_domain() 0 4 1
A get_domain() 0 9 2
1
<?php
2
3
namespace SilverStripe\MultiDomain;
4
5
use SilverStripe\Core\Object;
6
use SilverStripe\MultiDomain\MultiDomainDomain;
7
8
/**
9
 * A utility class that provides several static methods for parsing URLs
10
 * and resolving hostnames.
11
 *
12
 * @package  silverstripe-multi-domain
13
 * @author  Aaron Carlino <[email protected]>
14
 */
15
class MultiDomain extends Object
16
{
17
    /**
18
     * The key for the "primary" domain
19
     *
20
     * @var string
21
     */
22
    const KEY_PRIMARY = 'primary';
23
24
    /**
25
     * Given a url, get the domain that maps to it, e.g.
26
     *
27
     * /company/ -> silverstripe.com
28
     * /company/partners -> silverstripe.com
29
     * /community/forum -> silverstripe.org
30
     *
31
     * @param  string $url
32
     * @return MultiDomainDomain
33
     */
34
    public static function domain_for_url($url)
35
    {
36
        $url = trim($url, '/');
37
38
        foreach (self::get_all_domains() as $domain) {
39
            if ($domain->hasURL($url)) {
40
                return $domain;
41
            }
42
        }
43
44
        return self::get_primary_domain();
45
    }
46
47
    /**
48
     * Gets all the domains that have been configured
49
     *
50
     * @param  boolean $includePrimary If true, include the primary domain
51
     * @return array
52
     */
53
    public static function get_all_domains($includePrimary = false)
54
    {
55
        $domains = array ();
56
57
        foreach (self::config()->domains as $key => $config) {
58
            if (!$includePrimary && $key === self::KEY_PRIMARY) {
59
                continue;
60
            }
61
            $domains[] = MultiDomainDomain::create($key, $config);
62
        }
63
64
        return $domains;
65
    }
66
67
    /**
68
     * Gets the domain marked as "primary"
69
     * @return MultiDomainDomain
70
     */
71
    public static function get_primary_domain()
72
    {
73
        return self::get_domain(self::KEY_PRIMARY);
74
    }
75
76
    /**
77
     * Gets a domain by its key, e.g. 'org','com'
78
     * @param  string $domain
79
     * @return MultiDomainDomain
80
     */
81
    public static function get_domain($domain)
82
    {
83
        if (isset(self::config()->domains[$domain])) {
84
            return MultiDomainDomain::create(
85
                $domain,
86
                self::config()->domains[$domain]
87
            );
88
        }
89
    }
90
}
91