Completed
Push — master ( 0cbd7a...258693 )
by Jan-Petter
04:51
created

RenderHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 19
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 182
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A separatorCheck() 0 12 2
A getMode() 0 4 1
A addInline() 0 7 2
A export() 0 4 1
A add() 0 11 2
A advanced() 0 13 4
A boolInsertSeparatorMode3() 0 10 3
A basic() 0 8 2
A generate() 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\Handler;
10
11
use vipnytt\RobotsTxtParser\Exceptions\ClientException;
12
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
13
14
/**
15
 * Class RenderHandler
16
 *
17
 * @package vipnytt\RobotsTxtParser\Handler
18
 */
19
class RenderHandler implements RobotsTxtInterface
20
{
21
    /**
22
     * Mode
23
     * @var int
24
     */
25
    private $mode;
26
27
    /**
28
     * Line separator
29
     * @var string
30
     */
31
    private $separator;
32
33
    /**
34
     * Rule strings
35
     * @var string[]
36
     */
37
    private $strings = [];
38
39
    /**
40
     * Current directive
41
     * @var string
42
     */
43
    private $directive;
44
45
    /**
46
     * Previous directive
47
     * @var string
48
     */
49
    private $previous;
50
51
    /**
52
     * Previous root level directive
53
     * @var string
54
     */
55
    private $previousRoot;
56
57
    /**
58
     * RenderHandler constructor.
59
     *
60
     * @param int $mode
61
     * @param string $lineSeparator
62
     */
63
    public function __construct($mode, $lineSeparator = "\r\n")
64
    {
65
        $this->mode = $mode;
66
        $this->separatorCheck($lineSeparator);
67
    }
68
69
    /**
70
     * Line separator check
71
     *
72
     * @param string $lineSeparator
73
     * @throws ClientException
74
     */
75
    private function separatorCheck($lineSeparator)
76
    {
77
        if (!in_array($lineSeparator, [
78
            "\r\n",
79
            "\n",
80
            "\r",
81
        ])
82
        ) {
83
            throw new ClientException('Invalid line separator');
84
        }
85
        $this->separator = $lineSeparator;
86
    }
87
88
    /**
89
     * Get mode
90
     *
91
     * @return int
92
     */
93
    public function getMode()
94
    {
95
        return $this->mode;
96
    }
97
98
    /**
99
     * Implode
100
     *
101
     * @param string $directive
102
     * @param RenderHandler $handler
103
     * @return bool
104
     */
105
    public function addInline($directive, RenderHandler $handler)
106
    {
107
        foreach ($handler->export() as $line) {
108
            $this->add($directive, trim($line));
109
        }
110
        return true;
111
    }
112
113
    /**
114
     * Export
115
     *
116
     * @return string[]
117
     */
118
    public function export()
119
    {
120
        return $this->strings;
121
    }
122
123
    /**
124
     * Add line
125
     *
126
     * @param string $directive
127
     * @param string $line
128
     * @return bool
129
     */
130
    public function add($directive, $line)
131
    {
132
        $this->previous = $this->directive;
133
        $this->directive = $directive;
134
        if ($this->mode >= 2) {
135
            $this->strings[] = $this->advanced() . $line;
136
            return true;
137
        }
138
        $this->strings[] = $this->basic() . $line;
139
        return true;
140
    }
141
142
    /**
143
     * Advanced beautifier
144
     *
145
     * @return string
146
     */
147
    private function advanced()
148
    {
149
        $result = '';
150
        if ($this->boolInsertSeparatorMode3() ||
151
            $this->previous !== self::DIRECTIVE_USER_AGENT &&
152
            $this->directive === self::DIRECTIVE_USER_AGENT
153
        ) {
154
            $result = $this->separator;
155
            $this->previousRoot = $this->directive;
156
        }
157
        $result .= $this->basic();
158
        return $result;
159
    }
160
161
    /**
162
     * Should insert line separator? requires mode 3
163
     *
164
     * @return bool
165
     */
166
    private function boolInsertSeparatorMode3()
167
    {
168
        return $this->mode >= 3 &&
169
        $this->previousRoot !== $this->directive &&
170
        in_array($this->directive, [
171
            self::DIRECTIVE_HOST,
172
            self::DIRECTIVE_CLEAN_PARAM,
173
            self::DIRECTIVE_SITEMAP,
174
        ]);
175
    }
176
177
    /**
178
     * Basic beautifier
179
     *
180
     * @return string
181
     */
182
    private function basic()
183
    {
184
        $result = $this->directive . ':';
185
        if ($this->mode === 0) {
186
            return $result;
187
        }
188
        return ucfirst($result) . ' ';
189
    }
190
191
    /**
192
     * Generate robots.txt
193
     *
194
     * @return string
195
     */
196
    public function generate()
197
    {
198
        return trim(implode($this->separator, $this->strings));
199
    }
200
}
201