Passed
Push — master ( e61814...a9e4b9 )
by Nicolaas
09:16
created

ExternalURL   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 51
c 2
b 1
f 1
dl 0
loc 143
rs 10
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A scaffoldFormField() 0 6 1
A Domain() 0 7 2
A Path() 0 7 2
A forTemplate() 0 7 2
A DomainShort() 0 11 2
A __construct() 0 3 1
A Nice() 0 15 4
A saveInto() 0 32 6
A NoWWW() 0 4 1
1
<?php
2
3
namespace Sunnysideup\ExternalURLField;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\ORM\FieldType\DBVarchar;
7
use RuntimeException;
8
9
class ExternalURL extends DBVarchar
10
{
11
    private static $casting = [
12
        'Domain' => ExternalURL::class,
13
        'DomainShort' => 'Varchar',
14
        'URL' => ExternalURL::class,
15
        'Path' => 'Varchar',
16
    ];
17
18
    /**
19
     * 2083 is the lowest common denominator when it comes to url lengths.
20
     * however, 768 allows searching...
21
     *
22
     * @param null|mixed $name
23
     * @param mixed      $size
24
     * @param mixed      $options
25
     */
26
    public function __construct($name = null, $size = 2083, $options = [])
27
    {
28
        parent::__construct($name, $size, $options);
29
    }
30
31
    /**
32
     * Remove ugly parts of a url to make it nice.
33
     */
34
    public function Nice(): string
35
    {
36
        if ($this->value) {
37
            $parts = parse_url($this->URL());
38
            if ($parts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
                $remove = ['scheme', 'user', 'pass', 'port', 'query', 'fragment'];
40
                foreach ($remove as $part) {
41
                    unset($parts[$part]);
42
                }
43
            }
44
45
            return rtrim(http_build_url($parts), '/');
46
        }
47
48
        return '';
49
    }
50
51
    /**
52
     * Get just the domain of the url.
53
     */
54
    public function Domain()
55
    {
56
        if ($this->value) {
57
            return parse_url($this->URL(), PHP_URL_HOST);
58
        }
59
60
        return '';
61
    }
62
63
    /**
64
     * Get just the domain of the url without the www subdomain and without https://.
65
     */
66
    public function DomainShort()
67
    {
68
        if ($this->value) {
69
            $parsedUrl = parse_url($this->URL());
70
            $host = $parsedUrl['host'] ?? '';
71
            // Remove 'www.' if present.
72
            $domain = preg_replace('/^www\./', '', $host);
73
            return $domain;
74
        }
75
76
        return '';
77
    }
78
79
    /**
80
     * Remove the www subdomain, if present.
81
     */
82
    public function NoWWW()
83
    {
84
        //https://stackoverflow.com/questions/23349257/trim-a-full-string-instead-of-using-trim-with-characters
85
        return $url = preg_replace('/^(www\.)*/', '', (string) $this->value);
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
86
    }
87
88
    public function Path()
89
    {
90
        if ($this->value) {
91
            return trim((string) parse_url((string) $this->URL(), PHP_URL_PATH), '/');
92
        }
93
94
        return '';
95
    }
96
97
    /**
98
     * Scaffold the ExternalURLField for this ExternalURL.
99
     *
100
     * @param null|mixed $title
101
     * @param null|mixed $params
102
     */
103
    public function scaffoldFormField($title = null, $params = null)
104
    {
105
        $field = new ExternalURLField($this->name, $title);
106
        $field->setMaxLength($this->getSize());
107
108
        return $field;
109
    }
110
111
    public function forTemplate()
112
    {
113
        if ($this->value) {
114
            return (string) $this->URL();
115
        }
116
117
        return '';
118
    }
119
120
    public function saveInto($dataObject)
121
    {
122
        $url = (string) $this->value;
123
        if ($url) {
124
            $config = Config::inst()->get(ExternalURLField::class, 'default_config');
125
            $defaults = $config['defaultparts'];
126
            if (!preg_match('#^[a-zA-Z]+://#', $url)) {
127
                $url = $defaults['scheme'] . '://' . $url;
128
            }
129
130
            $parts = parse_url($url);
131
            if ($parts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132
                //can't parse url, abort
133
134
                foreach (array_keys($parts) as $part) {
135
                    if (true === $config['removeparts'][$part]) {
136
                        unset($parts[$part]);
137
                    }
138
                }
139
140
                // this causes errors!
141
                // $parts = array_filter($defaults, fn ($default) => ! isset($parts[$part]));
142
143
                $this->value = rtrim(http_build_url($defaults, $parts), '/');
144
            } else {
145
                $this->value = '';
146
            }
147
        } else {
148
            $this->value = '';
149
150
        }
151
        parent::saveInto($dataObject);
152
    }
153
}
154