Test Failed
Pull Request — master (#236)
by
unknown
04:56
created

Robots   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 94
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFromDB() 0 3 1
A __construct() 0 1 1
A listRules() 0 3 1
A addRule() 0 9 1
A writeFile() 0 22 4
A removeRule() 0 5 1
A getContent() 0 16 3
A sortByUserAgent() 0 12 3
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
class Robots {
20
21
	//https://developers.google.com/search/reference/robots_txt?hl=de
22
23
	protected $robots = array();
24
25
	public function __construct () {
26
		//
27
	}
28
29
	public function loadFromDB () {
30
		$rows = DataBase::getInstance()->listRows("SELECT * FROM `{prefix}robots` WHERE `activated` = '1'; ");
31
		$this->robots = $rows;
32
	}
33
34
	public function sortByUserAgent () {
35
		$array = array();
36
37
		foreach ($this->robots as $row) {
38
			if (!isset($row['useragent'])) {
39
				$array[$row['useragent']] = array();
40
			}
41
42
			$array[$row['useragent']][] = $row;
43
		}
44
45
		return $array;
46
	}
47
48
	public function writeFile () {
49
		$array = $this->sortByUserAgent();
50
51
		if (!is_writable(ROOT_PATH . "robots.txt")) {
52
			throw new IllegalStateException(ROOT_PATH . "robots.txt is not writable, please correct file permissions!");
53
		}
54
55
		$handle = fopen(ROOT_PATH . "robots.txt", "w");
56
57
		foreach ($array as $useragent=>$value) {
58
59
			fwrite($handle, "User-agent: " . $useragent . "\r\n");
60
61
			foreach ($value as $line) {
62
				fwrite($handle, "" . $line['option'] . ": " . $line['value'] . "\r\n");
63
			}
64
65
			fwrite($handle, "\r\n");
66
67
		}
68
69
		fclose($handle);
70
	}
71
72
	public function getContent () {
73
		$array = $this->sortByUserAgent();
74
75
		$buffer = "";
76
77
		foreach ($array as $useragent=>$value) {
78
			$buffer .= "User-agent: " . $useragent . "\r\n";
79
80
			foreach ($value as $line) {
81
				$buffer .= "" . $line['option'] . ": " . $line['value'] . "\r\n";
82
			}
83
84
			$buffer .= "\r\n";
85
		}
86
87
		return $buffer;
88
	}
89
90
	public static function addRule (string $option, string $value, string $useragent = "*") {
91
		Database::getInstance()->execute("INSERT INTO `{praefix}robots` (
92
			`useragent`, `option`, `value`, `activated`
93
		) VALUES (
94
			:useragent, :option, :value, '1'
95
		) ON DUPLICATE KEY UPDATE `option` = :option, `activated` = '1'; ", array(
96
			'useragent' => $useragent,
97
			'option' => $option,
98
			'value' => $value
99
		));
100
	}
101
102
	public static function removeRule (string $option, string $value, string $useragent = "*") {
103
		Database::getInstance()->execute("DELETE FROM `{praefix}robots` WHERE `useragent` = :useragent AND `option` = :option AND `value` = :value; ", array(
104
			'useragent' => $useragent,
105
			'option' => $option,
106
			'value' => $value
107
		));
108
	}
109
110
	public static function listRules () : array {
111
		$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}robots`; ");
112
		return $rows;
113
	}
114
115
}
116
117
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
118