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

Rebuild   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 1
cbo 0
dl 0
loc 98
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A all() 0 7 2
A none() 0 8 2
A getResult() 0 4 1
A parse() 0 11 2
A noIndex() 0 7 2
A unavailableAfter() 0 10 4
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