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

URLSanitisationService::addURL()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 23
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Service;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Environment;
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
19
    /**
20
     * Enable to force all URLs to be changed to https
21
     *
22
     * @var bool
23
     * @config
24
     */
25
    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...
26
27
    /**
28
     * @var array
29
     */
30
    private $urls = [];
31
32
    /**
33
     * @var int
34
     */
35
    private $pointer = 0;
36
37
    /**
38
     * @param string $url
39
     */
40
    public function addURL(string $url): void
41
    {
42
        // We never want to have stage=Stage present in our cached URLs. This will safely remove "stage" params, but
43
        // keep any others. It doesn't matter where in the string "stage=" exists.
44
        $url = preg_replace('/([?&])stage=[^&]+(&|$)/', '$1', $url);
45
        // Trim any trailing "?" or "&".
46
        $url = $string = rtrim($url, '&');
0 ignored issues
show
Unused Code introduced by
The assignment to $string is dead and can be removed.
Loading history...
47
        $url = $string = rtrim($url, '?');
48
49
        // Force SSL publishing as CLI will default to http
50
        $forceSSL = $this->getForceSSL();
51
        if ($forceSSL) {
52
            $url = str_replace('http://', 'https://', $url);
53
        }
54
55
        // Check if this URL is already represented.
56
        if (array_key_exists($url, $this->urls)) {
57
            // Don't need to add it again if it is.
58
            return;
59
        }
60
61
        $this->urls[$url] = $this->pointer;
62
        $this->pointer += 1;
63
    }
64
65
    /**
66
     * @param array $urls
67
     */
68
    public function addURLs(array $urls): void
69
    {
70
        foreach ($urls as $url) {
71
            $this->addURL($url);
72
        }
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getURLs($formatted = true): array
79
    {
80
        if (!$formatted) {
81
            return array_keys($this->urls);
82
        }
83
84
        return $this->urls;
85
    }
86
87
    /**
88
     * Override this function if runtime change is needed (CMS setting or Environment variable)
89
     *
90
     * @return bool
91
     */
92
    protected function getForceSSL()
93
    {
94
        return (bool) $this->config()->get('force_ssl');
95
    }
96
}
97