Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

RequestRateParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add() 0 17 4
A export() 0 4 2
A render() 0 15 4
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
5
6
/**
7
 * Class RequestRateParser
8
 *
9
 * @package vipnytt\RobotsTxtParser\Parser\Directives
10
 */
11
class RequestRateParser implements ParserInterface, RobotsTxtInterface
12
{
13
    use DirectiveParserCommons;
14
15
    /**
16
     * Directive
17
     */
18
    const DIRECTIVE = self::DIRECTIVE_REQUEST_RATE;
19
20
    /**
21
     * RequestRate array
22
     * @var array
23
     */
24
    private $array = [];
25
26
    /**
27
     * RequestRate constructor.
28
     */
29
    public function __construct()
30
    {
31
    }
32
33
    /**
34
     * Add
35
     *
36
     * @param string $line
37
     * @return bool
38
     */
39
    public function add($line)
40
    {
41
        $array = preg_split('/\s+/', $line, 2);
42
        $result = [
43
            'rate' => $this->draftParseRate($array[0]),
44
        ];
45
        if ($result['rate'] === false) {
46
            return false;
47
        } elseif (
48
            !empty($array[1]) &&
49
            ($times = $this->draftParseTime($array[1])) !== false
50
        ) {
51
            $result = array_merge($result, $times);
52
        }
53
        $this->array[] = $result;
54
        return true;
55
    }
56
57
    /**
58
     * Export rules
59
     *
60
     * @return string[][]
61
     */
62
    public function export()
63
    {
64
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
65
    }
66
67
    /**
68
     * Render
69
     *
70
     * @return string[]
71
     */
72
    public function render()
73
    {
74
        $result = [];
75
        foreach ($this->array as $array) {
76
            $suffix = 's';
77
            if (
78
                isset($array['from']) &&
79
                isset($array['to'])
80
            ) {
81
                $suffix .= ' ' . $array['from'] . '-' . $array['to'];
82
            }
83
            $result[] = self::DIRECTIVE . ':1/' . $array['rate'] . $suffix;
84
        }
85
        return $result;
86
    }
87
}
88