Completed
Push — master ( 0be3f5...793ac3 )
by Jan-Petter
02:16
created

RenderHandler::boolInsertSeparatorLevel3()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
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
     * Render level
23
     * @var int
24
     */
25
    private $level;
26
27
    /**
28
     * Line separator
29
     * @var string
30
     */
31
    private $eol;
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 $level
61
     * @param string $lineSeparator
62
     */
63
    public function __construct($level, $lineSeparator = "\r\n")
64
    {
65
        $this->level = $level;
66
        $this->separatorCheck($lineSeparator);
67
    }
68
69
    /**
70
     * Line separator check
71
     *
72
     * @param string $eol
73
     * @throws ClientException
74
     */
75
    private function separatorCheck($eol)
76
    {
77
        if (!in_array($eol, [
78
            "\r\n",
79
            "\n",
80
            "\r",
81
        ])
82
        ) {
83
            throw new ClientException('Invalid line separator');
84
        }
85
        $this->eol = $eol;
86
    }
87
88
    /**
89
     * Get mode
90
     *
91
     * @return int
92
     */
93
    public function getLevel()
94
    {
95
        return $this->level;
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
        $lines = array_map('trim', $handler->export());
108
        foreach ($lines as $line) {
109
            $this->add($directive, $line);
110
        }
111
        return true;
112
    }
113
114
    /**
115
     * Export
116
     *
117
     * @return string[]
118
     */
119
    public function export()
120
    {
121
        return $this->strings;
122
    }
123
124
    /**
125
     * Add line
126
     *
127
     * @param string $directive
128
     * @param string $line
129
     * @return bool
130
     */
131
    public function add($directive, $line)
132
    {
133
        $this->previous = $this->directive;
134
        $this->directive = $directive;
135
        if ($this->level >= 2) {
136
            $this->strings[] = $this->advanced() . $line;
137
            return true;
138
        }
139
        $this->strings[] = $this->basic() . $line;
140
        return true;
141
    }
142
143
    /**
144
     * Advanced beautifier
145
     *
146
     * @return string
147
     */
148
    private function advanced()
149
    {
150
        $result = '';
151
        if ($this->boolInsertSeparatorLevel3() ||
152
            $this->previous !== self::DIRECTIVE_USER_AGENT &&
153
            $this->directive === self::DIRECTIVE_USER_AGENT
154
        ) {
155
            $result = $this->eol;
156
            $this->previousRoot = $this->directive;
157
        }
158
        $result .= $this->basic();
159
        return $result;
160
    }
161
162
    /**
163
     * Should insert line separator? requires mode 3
164
     *
165
     * @return bool
166
     */
167
    private function boolInsertSeparatorLevel3()
168
    {
169
        return $this->level >= 3 &&
170
        $this->previousRoot !== $this->directive &&
171
        in_array($this->directive, [
172
            self::DIRECTIVE_HOST,
173
            self::DIRECTIVE_CLEAN_PARAM,
174
            self::DIRECTIVE_SITEMAP,
175
        ]);
176
    }
177
178
    /**
179
     * Basic beautifier
180
     *
181
     * @return string
182
     */
183
    private function basic()
184
    {
185
        $result = $this->directive . ':';
186
        if ($this->level === 0) {
187
            return $result;
188
        }
189
        return ucfirst($result) . ' ';
190
    }
191
192
    /**
193
     * Generate robots.txt
194
     *
195
     * @return string
196
     */
197
    public function generate()
198
    {
199
        return trim(implode($this->eol, $this->strings));
200
    }
201
}
202