Passed
Pull Request — master (#104)
by
unknown
02:45
created

URLSanitisationService::enforceTrailingSlash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Service;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
8
/**
9
 * Class URLSanitisationService
10
 * Provides an abstraction for populating and generating a list of
11
 * URLs to be cached by StaticPublishQueue.
12
 *
13
 * @package SilverStripe\StaticPublishQueue\Service
14
 */
15
class URLSanitisationService
16
{
17
    use Configurable;
18
    use Injectable;
19
20
    /**
21
     * Enable to force all URLs to be changed to https
22
     *
23
     * @var bool
24
     * @config
25
     */
26
    private static $force_ssl = false;
0 ignored issues
show
introduced by
The private property $force_ssl is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @var array
30
     */
31
    private $urls = [];
32
33
    /**
34
     * @var int
35
     */
36
    private $pointer = 0;
37
38
    /**
39
     * @param string $url
40
     */
41
    public function addURL($url)
42
    {
43
        $url = $this->enforceTrailingSlash($url);
44
        $url = $this->enforceSSL($url);
45
46
        // Check if this URL is already represented.
47
        if (array_key_exists($url, $this->urls)) {
48
            // Don't need to add it again if it is.
49
            return;
50
        }
51
52
        $this->urls[$url] = $this->pointer;
53
        $this->pointer += 1;
54
    }
55
56
    /**
57
     * @param array $urls
58
     */
59
    public function addURLs(array $urls)
60
    {
61
        foreach ($urls as $url) {
62
            $this->addURL($url);
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getURLs($formatted = false)
70
    {
71
        if ($formatted) {
72
            return $this->urls;
73
        }
74
75
        return array_keys($this->urls);
76
    }
77
78
    /**
79
     * Make sure we don't have multiple variations of the same URL (with and without trailing slash)
80
     *
81
     * @param string $url
82
     * @return string
83
     */
84
    protected function enforceTrailingSlash($url)
85
    {
86
        $query = parse_url($url, PHP_URL_QUERY);
87
        $queryString = strlen($query) > 0
88
            ? '?' . $query
89
            : '';
90
91
        $url = str_replace('?' . $query, '', $url);
92
        $url = rtrim($url, '/') . '/';
93
94
        return $url . $queryString;
95
    }
96
97
    /**
98
     * Force SSL if needed
99
     *
100
     * @param string $url
101
     * @return string
102
     */
103
    protected function enforceSSL($url)
104
    {
105
        $forceSSL = $this->getForceSSL();
106
        if (!$forceSSL) {
107
            return $url;
108
        }
109
110
        return str_replace('http://', 'https://', $url);
111
    }
112
113
    /**
114
     * Override this function if runtime change is needed (CMS setting or Environment variable)
115
     *
116
     * @return bool
117
     */
118
    protected function getForceSSL()
119
    {
120
        return (bool) $this->config()->get('force_ssl');
121
    }
122
}
123