CleanParamClient   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 113
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A detectWithCommon() 0 9 1
A appendPath() 0 8 2
A parse() 0 17 5
A export() 0 4 1
A detect() 0 4 1
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Client\Directives;
10
11
/**
12
 * Class CleanParamClient
13
 *
14
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/CleanParamClient.md for documentation
15
 * @package vipnytt\RobotsTxtParser\Client\Directives
16
 */
17
class CleanParamClient implements ClientInterface
18
{
19
    use DirectiveClientTrait;
20
21
    /**
22
     * Common dynamic uri parameters
23
     * @var string[]
24
     */
25
    protected $commonParam = [
26
        'popup',
27
        'ref',
28
        'token',
29
        'utm_campaign',
30
        'utm_content',
31
        'utm_medium',
32
        'utm_source',
33
        'utm_term',
34
    ];
35
36
    /**
37
     * Clean-param
38
     * @var string[][]
39
     */
40
    private $cleanParam = [];
41
42
    /**
43
     * CleanParamClient constructor.
44
     *
45
     * @param string[][] $cleanParam
46
     */
47
    public function __construct(array $cleanParam)
48
    {
49
        $this->cleanParam = $cleanParam;
50
    }
51
52
    /**
53
     * Has robots.txt defined dynamic or common dynamic parameters check
54
     *
55
     * @param string $uri
56
     * @param string[] $customParam
57
     * @return string[]
58
     */
59
    public function detectWithCommon($uri, array $customParam = [])
60
    {
61
        $pairs = array_merge_recursive(
62
            $this->cleanParam,
63
            $this->appendPath($this->commonParam),
64
            $this->appendPath($customParam)
65
        );
66
        return $this->parse($uri, $pairs);
67
    }
68
69
    /**
70
     * Convert param list to an valid Clean-param list
71
     *
72
     * @param string[] $parameters
73
     * @return array
74
     */
75
    private function appendPath(array $parameters)
76
    {
77
        $result = [];
78
        foreach ($parameters as $parameter) {
79
            $result[$parameter] = ['/'];
80
        }
81
        return $result;
82
    }
83
84
    /**
85
     * Parse uri and return detected parameters
86
     *
87
     * @param string $uri
88
     * @param array $pairs
89
     * @return string[]
90
     */
91
    protected function parse($uri, array $pairs)
92
    {
93
        $path = $this->getPathFromUri($uri);
94
        $result = [];
95
        foreach ($pairs as $param => $paths) {
96
            if ((
97
                    strpos($uri, "?$param=") ||
98
                    strpos($uri, "&$param=")
99
                ) &&
100
                $this->checkPaths($path, $paths) !== false
101
            ) {
102
                $result[] = $param;
103
            }
104
        }
105
        sort($result);
106
        return $result;
107
    }
108
109
    /**
110
     * Export
111
     *
112
     * @return string[][]
113
     */
114
    public function export()
115
    {
116
        return $this->cleanParam;
117
    }
118
119
    /**
120
     * Detect dynamic parameters
121
     *
122
     * @param  string $uri
123
     * @return string[]
124
     */
125
    public function detect($uri)
126
    {
127
        return $this->parse($uri, $this->cleanParam);
128
    }
129
}
130