Completed
Push — master ( 1176d8...cace1c )
by Jan-Petter
08:44 queued 05:54
created

Rebuild::unavailableAfter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

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