CleanParamParser   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 0
loc 122
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A client() 0 5 1
A sort() 0 7 3
A add() 0 21 4
A render() 0 5 2
A renderExtensive() 0 9 3
A renderCompressed() 0 14 4
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\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Client\Directives\CleanParamClient;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\Parser\UriParser;
14
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
15
16
/**
17
 * Class CleanParamParser
18
 *
19
 * @package vipnytt\RobotsTxtParser\Parser\Directives
20
 */
21
class CleanParamParser implements ParserInterface, RobotsTxtInterface
22
{
23
    /**
24
     * Clean-param array
25
     * @var string[][]
26
     */
27
    private $cleanParam = [];
28
29
    /**
30
     * Sort result
31
     * @var bool
32
     */
33
    private $sort = false;
34
35
    /**
36
     * CleanParamParser constructor.
37
     */
38
    public function __construct()
39
    {
40
    }
41
42
    /**
43
     * Client
44
     *
45
     * @return CleanParamClient
46
     */
47
    public function client()
48
    {
49
        $this->sort();
50
        return new CleanParamClient($this->cleanParam);
51
    }
52
53
    /**
54
     * Sort by length
55
     *
56
     * @return bool
57
     */
58
    private function sort()
59
    {
60
        if ($this->sort) {
61
            return $this->sort;
62
        };
63
        return $this->sort = array_multisort($this->cleanParam) && ksort($this->cleanParam);
64
    }
65
66
    /**
67
     * Add
68
     *
69
     * @param string $line
70
     * @return bool
71
     */
72
    public function add($line)
73
    {
74
        $path = '/';
75
        // split into parameter and path
76
        $array = array_map('trim', mb_split('\s+', $line, 2));
77
        if (isset($array[1])) {
78
            // strip any invalid characters from path prefix
79
            $uriParser = new UriParser(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1]));
80
            $path = rtrim($uriParser->encode(), '*');
81
            // make sure path is valid
82
            $path = in_array(substr($path, 0, 1), [
83
                '/',
84
                '*',
85
            ]) ? $path : '/';
86
        }
87
        $param = array_map('trim', explode('&', $array[0]));
88
        foreach ($param as $key) {
89
            $this->cleanParam[$key][] = $path;
90
        }
91
        return true;
92
    }
93
94
    /**
95
     * Render
96
     *
97
     * @param RenderHandler $handler
98
     * @return bool
99
     */
100
    public function render(RenderHandler $handler)
101
    {
102
        $this->sort();
103
        return $handler->getLevel() == 2 ? $this->renderExtensive($handler) : $this->renderCompressed($handler);
104
    }
105
106
    /**
107
     * Render extensive
108
     *
109
     * @param RenderHandler $handler
110
     * @return bool
111
     */
112
    private function renderExtensive(RenderHandler $handler)
113
    {
114
        foreach ($this->cleanParam as $param => $paths) {
115
            foreach ($paths as $path) {
116
                $handler->add(self::DIRECTIVE_CLEAN_PARAM, $param . ' ' . $path);
117
            }
118
        }
119
        return true;
120
    }
121
122
    /**
123
     * Render compressed
124
     *
125
     * @param RenderHandler $handler
126
     * @return bool
127
     */
128
    private function renderCompressed(RenderHandler $handler)
129
    {
130
        $pair = $this->cleanParam;
131
        while (($currentPair = current($pair)) !== false) {
132
            $equalParams = array_keys($pair, $currentPair);
133
            foreach ($currentPair as $path) {
134
                $handler->add(self::DIRECTIVE_CLEAN_PARAM, implode('&', $equalParams) . ' ' . $path);
135
            }
136
            foreach ($equalParams as $param) {
137
                unset($pair[$param]);
138
            }
139
        }
140
        return true;
141
    }
142
}
143