Passed
Push — master ( c62459...121b08 )
by Csaba
01:50
created

DefaultNormalizer::afterNormalization()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HumanDirect\Socially\Normalizer;
4
5
use HumanDirect\Socially\Factory;
6
use HumanDirect\Socially\Util;
7
8
/**
9
 * Class DefaultNormalizer.
10
 */
11
class DefaultNormalizer implements NormalizerInterface
12
{
13
    /**
14
     * @var bool
15
     */
16
    protected $stripSubdomain = true;
17
18
    /**
19
     * @var bool
20
     */
21
    protected $stripQueryStrings = true;
22
23
    /**
24
     * @var array
25
     */
26
    protected $allowedQueryParams = [
27
        'id'
28
    ];
29
30
    /**
31
     * Normalize a URL.
32
     *
33
     * @param string $url
34
     *
35
     * @return string
36
     */
37
    public function normalize(string $url): string
38
    {
39
        $url = rawurldecode($url);
40
        $url = $this->cleanUrl($url);
41
        $url = str_replace('http://', 'https://', $url);
42
43
        return $this->afterNormalization($url);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function afterNormalization(string $url): string
50
    {
51
        if (true === $this->stripSubdomain) {
52
            $url = $this->cleanSubdomain($url);
53
        }
54
55
        if (true === $this->stripQueryStrings) {
56
            $url = $this->cleanQueryString($url);
57
        }
58
59
        return $url;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function supports(string $platform): bool
66
    {
67
        return $this->getName() === Util::toCamelCase($platform);
68
    }
69
70
    /**
71
     * @throws \ReflectionException
72
     *
73
     * @return string
74
     */
75
    protected function getName(): string
76
    {
77
        $shortName = (new \ReflectionClass($this))->getShortName();
78
79
        return substr($shortName, 0, -10);
80
    }
81
82
    /**
83
     * @param string $url
84
     *
85
     * @return string
86
     */
87
    protected function cleanUrl(string $url): string
88
    {
89
        return rtrim(Util::cleanUrl($url), '/');
90
    }
91
92
    /**
93
     * @param string $url
94
     *
95
     * @throws \LayerShifter\TLDExtract\Exceptions\RuntimeException
96
     *
97
     * @return string
98
     */
99
    private function cleanSubdomain(string $url): string
100
    {
101
        $tldExtractor = Factory::createTldExtractor();
102
        $result = $tldExtractor->parse($url);
103
        $subdomain = $result->getSubdomain();
104
105
        if (null === $subdomain) {
106
            return $url;
107
        }
108
109
        return str_replace($subdomain . '.', '', $url);
110
    }
111
112
    /**
113
     * @param string $url
114
     *
115
     * @return string
116
     */
117
    private function cleanQueryString(string $url): string
118
    {
119
        // all query params and fragments have to be removed
120
        if (empty($this->allowedQueryParams)) {
121
            return strtok($url, '?');
122
        }
123
124
        // first remove fragments from URL
125
        $url = strtok($url, '#');
126
        $query = parse_url($url, PHP_URL_QUERY);
127
128
        if (null === $query) {
129
            return $url;
130
        }
131
132
        parse_str($query, $params);
133
        foreach ($params as $paramKey => $paramValue) {
134
            if (!\in_array($paramKey, $this->allowedQueryParams, true)) {
135
                unset($params[$paramKey]);
136
            }
137
        }
138
139
        return strtok($url, '?') . (\count($params) ? '?' . http_build_query($params) : '');
140
    }
141
}
142