VisitTimeParser::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Client\Directives\VisitTimeClient;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
14
15
/**
16
 * Class VisitTimeParser
17
 *
18
 * @package vipnytt\RobotsTxtParser\Parser\Directives
19
 */
20
class VisitTimeParser implements ParserInterface, RobotsTxtInterface
21
{
22
    use DirectiveParserTrait;
23
24
    /**
25
     * VisitTime array
26
     * @var array
27
     */
28
    private $visitTimes = [];
29
30
    /**
31
     * Sorted
32
     * @var bool
33
     */
34
    private $sorted = false;
35
36
    /**
37
     * VisitTime constructor.
38
     */
39
    public function __construct()
40
    {
41
    }
42
43
    /**
44
     * Add
45
     *
46
     * @param string $line
47
     * @return bool
48
     */
49
    public function add($line)
50
    {
51
        $array = $this->draftParseTime($line);
52
        if ($array !== false) {
53
            $this->visitTimes[] = $array;
54
            return true;
55
        }
56
        return false;
57
    }
58
59
    /**
60
     * Client
61
     *
62
     * @return VisitTimeClient
63
     */
64
    public function client()
65
    {
66
        $this->sort();
67
        return new VisitTimeClient($this->visitTimes);
68
    }
69
70
    /**
71
     * Sort
72
     *
73
     * @return bool
74
     */
75
    private function sort()
76
    {
77
        if (!$this->sorted) {
78
            $this->sorted = true;
79
            return usort($this->visitTimes, function (array $visitTimeA, array $visitTimeB) {
80
                return $visitTimeA['from'] <=> $visitTimeB['from'];
81
            });
82
        }
83
        return $this->sorted;
84
    }
85
86
    /**
87
     * Render
88
     *
89
     * @param RenderHandler $handler
90
     * @return bool
91
     */
92
    public function render(RenderHandler $handler)
93
    {
94
        $this->sort();
95
        foreach ($this->visitTimes as $array) {
96
            $handler->add(self::DIRECTIVE_VISIT_TIME, $array['from'] . '-' . $array['to']);
97
        }
98
        return true;
99
    }
100
}
101