Completed
Push — master ( 4a603a...3189f8 )
by Jan-Petter
02:42
created

Rebuild::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace vipnytt\XRobotsTagParser;
4
5
final class Rebuild
6
{
7
    private $directiveArray;
8
9
    /**
10
     * Constructor
11
     *
12
     * @param string $directives
13
     */
14
    public function __construct($directives)
15
    {
16
        $this->directiveArray = $directives;
17
        $this->parse();
18
    }
19
20
    /**
21
     * parse
22
     *
23
     * @return void
24
     */
25
    private function parse()
26
    {
27
        $past = [];
28
        while ($past !== $this->directiveArray) {
29
            $past = $this->directiveArray;
30
            $this->all();
31
            $this->noindex();
32
            $this->none();
33
            $this->unavailable_after();
34
        }
35
    }
36
37
    /**
38
     * Directive all
39
     *
40
     * @return void
41
     */
42
    private function all()
43
    {
44
        if (!isset($this->directiveArray['all'])) {
45
            return;
46
        }
47
        unset($this->directiveArray['all']);
48
    }
49
50
    /**
51
     * Directive noindex
52
     *
53
     * @return void
54
     */
55
    private function noindex()
56
    {
57
        if (!isset($this->directiveArray['noindex'])) {
58
            return;
59
        }
60
        $this->directiveArray['noarchive'] = true;
61
    }
62
63
    /**
64
     * Directive none
65
     *
66
     * @return void
67
     */
68
    private function none()
69
    {
70
        if (!isset($this->directiveArray['none'])) {
71
            return;
72
        }
73
        $this->directiveArray['noindex'] = true;
74
        $this->directiveArray['nofollow'] = true;
75
    }
76
77
    /**
78
     * Directive unavailable_after
79
     *
80
     * @return void
81
     */
82
    private function unavailable_after()
83
    {
84
        if (!isset($this->directiveArray['unavailable_after'])) {
85
            return;
86
        }
87
        $dateTime = date_create_from_format(DATE_RFC850, $this->directiveArray['unavailable_after']);
88
        if ($dateTime !== false && time() >= $dateTime->getTimestamp()) {
89
            $this->directiveArray['noindex'] = true;
90
        }
91
    }
92
93
    /**
94
     * Result
95
     *
96
     * @return array
97
     */
98
    public function getResult()
99
    {
100
        return $this->directiveArray;
101
    }
102
}
103