Passed
Push — master ( 47ba5b...e49f3e )
by Marc
04:12 queued 11s
created

Robots::composeFromParameters()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 22
rs 6.9666
c 0
b 0
f 0
cc 12
nc 9
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mguinea\Robots;
4
5
use Mguinea\Robots\Contracts\Robots as RobotsContract;
6
7
class Robots implements RobotsContract
8
{
9
    const ALLOW = 'Allow';
10
    const DISALLOW = 'Disallow';
11
    const HOST = 'Host';
12
    const SITEMAP = 'Sitemap';
13
    const USER_AGENT = 'User-agent';
14
    const COMMENT = '#';
15
16
    /**
17
     * The rows of for the robots.
18
     *
19
     * @var array
20
     */
21
    protected $rows = [];
22
23
    /**
24
     * Array with configuration set in constructor.
25
     *
26
     * @var array
27
     */
28
    protected $parameters = [];
29
30
    /**
31
     * Robots constructor.
32
     *
33
     * @param array $parameters
34
     */
35
    public function __construct(array $parameters = [])
36
    {
37
        $this->parameters = $parameters;
38
        $this->composeFromParameters();
39
    }
40
41
    /**
42
     * Create Robots object from array.
43
     */
44
    private function composeFromParameters(): void
45
    {
46
        foreach ($this->parameters as $key => $values) {
47
            if ($key === 'allows') {
48
                foreach ($values as $value) {
49
                    $this->addAllow($value);
50
                }
51
            } elseif ($key === 'disallows') {
52
                foreach ($values as $value) {
53
                    $this->addDisallow($value);
54
                }
55
            } elseif ($key === 'hosts') {
56
                foreach ($values as $value) {
57
                    $this->addHost($value);
58
                }
59
            } elseif ($key === 'sitemaps') {
60
                foreach ($values as $value) {
61
                    $this->addSitemap($value);
62
                }
63
            } elseif ($key === 'userAgents') {
64
                foreach ($values as $value) {
65
                    $this->addUserAgent($value);
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * Add a allow rule to the robots.
73
     *
74
     * @param string|array $directories
75
     * @return \Mguinea\Robots\Contracts\Robots;
76
     */
77
    public function addAllow($directories): RobotsContract
78
    {
79
        $this->addRuleLine($directories, self::ALLOW);
80
81
        return $this;
82
    }
83
84
    /**
85
     * Add a comment to the robots.
86
     *
87
     * @param string $comment
88
     * @return \Mguinea\Robots\Contracts\Robots;
89
     */
90
    public function addComment(string $comment): RobotsContract
91
    {
92
        $this->addLine(self::COMMENT." $comment");
93
94
        return $this;
95
    }
96
97
    /**
98
     * Add a disallow rule to the robots.
99
     *
100
     * @param string|array $directories
101
     * @return \Mguinea\Robots\Contracts\Robots;
102
     */
103
    public function addDisallow($directories): RobotsContract
104
    {
105
        $this->addRuleLine($directories, self::DISALLOW);
106
107
        return $this;
108
    }
109
110
    /**
111
     * Add a Host to the robots.
112
     *
113
     * @param string|array $host
114
     * @return \Mguinea\Robots\Contracts\Robots;
115
     */
116
    public function addHost($hosts): RobotsContract
117
    {
118
        $this->addRuleLine($hosts, self::HOST);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Add a row to the robots.
125
     *
126
     * @param string $row
127
     */
128
    protected function addLine($row)
129
    {
130
        $this->rows[] = (string) $row;
131
    }
132
133
    /**
134
     * Add multiple rows to the robots.
135
     *
136
     * @param string|array $rows
137
     */
138
    protected function addRows($rows)
139
    {
140
        foreach ((array) $rows as $row) {
141
            $this->addLine($row);
142
        }
143
    }
144
145
    /**
146
     * Add a rule to the robots.
147
     *
148
     * @param string|array $directories
149
     * @param string       $rule
150
     */
151
    protected function addRuleLine($directories, $rule)
152
    {
153
        foreach ((array) $directories as $directory) {
154
            $this->addLine("$rule: $directory");
155
        }
156
    }
157
158
    /**
159
     * Add a Sitemap to the robots.
160
     *
161
     * @param string|array $sitemap
162
     * @return \Mguinea\Robots\Contracts\Robots;
163
     */
164
    public function addSitemap($sitemaps): RobotsContract
165
    {
166
        $this->addRuleLine($sitemaps, self::SITEMAP);
167
168
        return $this;
169
    }
170
171
    /**
172
     * Add a spacer to the robots.
173
     * @return \Mguinea\Robots\Contracts\Robots;
174
     */
175
    public function addSpacer(): RobotsContract
176
    {
177
        $this->addLine('');
178
179
        return $this;
180
    }
181
182
    /**
183
     * Add a User-agent to the robots.
184
     *
185
     * @param string|array $userAgent
186
     * @return \Mguinea\Robots\Contracts\Robots;
187
     */
188
    public function addUserAgent($userAgents): RobotsContract
189
    {
190
        $this->addRuleLine($userAgents, self::USER_AGENT);
191
192
        return $this;
193
    }
194
195
    /**
196
     * Generate the robots data.
197
     *
198
     * @return string
199
     */
200
    public function generate(): string
201
    {
202
        return implode(PHP_EOL, $this->rows);
203
    }
204
205
    /**
206
     * Reset the rows.
207
     *
208
     * @return \Mguinea\Robots\Contracts\Robots;
209
     */
210
    public function reset(): RobotsContract
211
    {
212
        $this->rows = [];
213
214
        return $this;
215
    }
216
}
217