Passed
Push — master ( bdd28a...368ed7 )
by Basenko
02:10
created

Robots   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 89.13%

Importance

Changes 0
Metric Value
wmc 19
dl 0
loc 161
ccs 41
cts 46
cp 0.8913
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A addSpacer() 0 3 1
A addComment() 0 3 1
A addSitemap() 0 3 1
A generate() 0 3 1
A addLine() 0 3 1
A addRuleLine() 0 4 2
A reset() 0 3 1
A addDisallow() 0 3 1
A addUserAgent() 0 3 1
A addAllow() 0 3 1
A addLines() 0 4 2
A addHost() 0 3 1
A metaTag() 0 3 2
A shouldIndex() 0 7 2
A setShouldIndexCallback() 0 3 1
1
<?php
2
3
namespace MadWeb\Robots;
4
5
class Robots
6
{
7
    /**
8
     * The lines of for the robots.txt.
9
     *
10
     * @var array
11
     */
12
    protected $lines = [];
13
14
    /** @var callable|bool */
15
    protected static $shouldIndex = true;
16
17
    /**
18
     * Generate the robots.txt data.
19
     *
20
     * @return string
21
     */
22 27
    public function generate()
23
    {
24 27
        return implode(PHP_EOL, $this->lines);
25
    }
26
27
    /**
28
     * Add a Sitemap to the robots.txt.
29
     *
30
     * @param string $sitemap
31
     */
32 6
    public function addSitemap($sitemap)
33
    {
34 6
        $this->addLine("Sitemap: $sitemap");
35 6
    }
36
37
    /**
38
     * Add a User-agent to the robots.txt.
39
     *
40
     * @param string $userAgent
41
     */
42 3
    public function addUserAgent($userAgent)
43
    {
44 3
        $this->addLine("User-agent: $userAgent");
45 3
    }
46
47
    /**
48
     * Add a Host to the robots.txt.
49
     *
50
     * @param string $host
51
     */
52 6
    public function addHost($host)
53
    {
54 6
        $this->addLine("Host: $host");
55 6
    }
56
57
    /**
58
     * Add a disallow rule to the robots.txt.
59
     *
60
     * @param string|array $directories
61
     */
62 3
    public function addDisallow($directories)
63
    {
64 3
        $this->addRuleLine($directories, 'Disallow');
65 3
    }
66
67
    /**
68
     * Add a allow rule to the robots.txt.
69
     *
70
     * @param string|array $directories
71
     */
72 3
    public function addAllow($directories)
73
    {
74 3
        $this->addRuleLine($directories, 'Allow');
75 3
    }
76
77
    /**
78
     * Add a rule to the robots.txt.
79
     *
80
     * @param string|array $directories
81
     * @param string       $rule
82
     */
83 6
    public function addRuleLine($directories, string $rule)
84
    {
85 6
        foreach ((array) $directories as $directory) {
86 6
            $this->addLine("$rule: $directory");
87
        }
88 6
    }
89
90
    /**
91
     * Add a comment to the robots.txt.
92
     *
93
     * @param string $comment
94
     */
95 6
    public function addComment($comment)
96
    {
97 6
        $this->addLine("# $comment");
98 6
    }
99
100
    /**
101
     * Add a spacer to the robots.txt.
102
     */
103 3
    public function addSpacer()
104
    {
105 3
        $this->addLine('');
106 3
    }
107
108
    /**
109
     * Add a line to the robots.txt.
110
     *
111
     * @param string $line
112
     */
113 24
    public function addLine(string $line)
114
    {
115 24
        $this->lines[] = $line;
116 24
    }
117
118
    /**
119
     * Add multiple lines to the robots.txt.
120
     *
121
     * @param string|array $lines
122
     */
123
    protected function addLines($lines)
124
    {
125
        foreach ((array) $lines as $line) {
126
            $this->addLine($line);
127
        }
128
    }
129
130
    /**
131
     * Reset the lines.
132
     *
133
     * @return void
134
     */
135 3
    public function reset()
136
    {
137 3
        $this->lines = [];
138 3
    }
139
140
    /**
141
     * Set callback with should index condition.
142
     */
143 9
    public function setShouldIndexCallback(callable $callback)
144
    {
145 9
        self::$shouldIndex = $callback;
146 9
    }
147
148
    /**
149
     * Check is application should be indexed.
150
     */
151 9
    public function shouldIndex(): bool
152
    {
153 9
        if (is_callable(self::$shouldIndex)) {
154 9
            return (bool) call_user_func(self::$shouldIndex);
0 ignored issues
show
Bug introduced by
It seems like self::shouldIndex can also be of type boolean; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
            return (bool) call_user_func(/** @scrutinizer ignore-type */ self::$shouldIndex);
Loading history...
155
        }
156
157
        return self::$shouldIndex;
158
    }
159
160
    /**
161
     * Render robots meta tag.
162
     */
163 3
    public function metaTag(): string
164
    {
165 3
        return '<meta name="robots" content="'.($this->shouldIndex() ? 'index, follow' : 'noindex, nofollow').'">';
166
    }
167
}
168