Sitemap   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 53
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A getField() 0 4 1
A getValue() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the bisarca/robots-txt package.
5
 *
6
 * (c) Emanuele Minotto <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bisarca\RobotsTxt\Directive;
13
14
use Bisarca\RobotsTxt\Exception\InvalidDirectiveException;
15
16
/**
17
 * "Sitemap" directive element.
18
 */
19
class Sitemap implements NonGroupInterface
20
{
21
    /**
22
     * Directive value.
23
     *
24
     * @var string
25
     */
26
    private $value = '';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct(string $raw)
32
    {
33
        if (!preg_match('/^sitemap:\s*([^# ]+).*/i', $raw, $matches)) {
34
            throw InvalidDirectiveException::create($raw);
35
        }
36
37
        $url = trim($matches[1]);
38
39
        $options = FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED;
40
41
        if (!filter_var($url, FILTER_VALIDATE_URL, $options)) {
42
            throw InvalidDirectiveException::create($raw);
43
        }
44
45
        $this->value = $url;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public static function getField(): string
52
    {
53
        return 'sitemap';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getValue(): string
60
    {
61
        return $this->value;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function __toString(): string
68
    {
69
        return sprintf('Sitemap: %s', $this->value);
70
    }
71
}
72