UnavailableAfter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
c 4
b 1
f 0
lcom 1
cbo 0
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDirective() 0 4 1
A getValue() 0 14 4
1
<?php
2
namespace vipnytt\XRobotsTagParser\Directives;
3
4
/**
5
 * Class UnavailableAfter
6
 *
7
 * @package vipnytt\XRobotsTagParser\Directives
8
 */
9
final class UnavailableAfter implements DirectiveInterface
10
{
11
    /**
12
     * Google date format
13
     * @link https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
14
     */
15
    const DATE_GOOGLE = 'd M Y H:i:s T';
16
17
    /**
18
     * Supported date formats
19
     */
20
    const SUPPORTED_DATE_FORMATS = [
21
        DATE_RFC850,
22
        self::DATE_GOOGLE,
23
    ];
24
25
    /**
26
     * Current directive
27
     * @var string
28
     */
29
    protected $directive;
30
31
    /**
32
     * Current rule string
33
     * @var string
34
     */
35
    private $rule;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param string $directive
41
     * @param string $rule
42
     */
43
    public function __construct($directive, $rule)
44
    {
45
        $this->directive = $directive;
46
        $this->rule = $rule;
47
    }
48
49
    /**
50
     * Get directive name
51
     *
52
     * @return string
53
     */
54
    public function getDirective()
55
    {
56
        return $this->directive;
57
    }
58
59
    /**
60
     * Get value
61
     *
62
     * @return string|null
63
     */
64
    public function getValue()
65
    {
66
        $parts = mb_split(',', trim(mb_substr($this->rule, mb_stripos($this->rule, $this->directive) + mb_strlen($this->directive) + 1)));
67
        $count = count($parts);
68
        for ($num = 1; $num <= $count; $num++) {
69
            foreach (self::SUPPORTED_DATE_FORMATS as $format) {
70
                $dateTime = date_create_from_format($format, trim(implode(',', array_slice($parts, 0, $num))));
71
                if ($dateTime !== false) {
72
                    return date_format($dateTime, DATE_RFC850);
73
                }
74
            }
75
        }
76
        return null;
77
    }
78
}
79