SitemapParser::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Client\Directives\SitemapClient;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\Parser\UriParser;
14
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
15
16
/**
17
 * Class SitemapParser
18
 *
19
 * @package vipnytt\RobotsTxtParser\Parser\Directives
20
 */
21
class SitemapParser implements ParserInterface, RobotsTxtInterface
22
{
23
    /**
24
     * Sitemap array
25
     * @var string[]
26
     */
27
    private $sitemaps = [];
28
29
    /**
30
     * Sitemap constructor.
31
     */
32
    public function __construct()
33
    {
34
    }
35
36
    /**
37
     * Add
38
     *
39
     * @param string $line
40
     * @return bool
41
     */
42
    public function add($line)
43
    {
44
        $uriParser = new UriParser($line);
45
        $uri = $uriParser->encode();
46
        if (!$uriParser->validate() ||
47
            in_array($uri, $this->sitemaps)
48
        ) {
49
            return false;
50
        }
51
        $this->sitemaps[] = $uri;
52
        return true;
53
    }
54
55
    /**
56
     * Client
57
     *
58
     * @return SitemapClient
59
     */
60
    public function client()
61
    {
62
        return new SitemapClient($this->sitemaps);
63
    }
64
65
    /**
66
     * Render
67
     *
68
     * @param RenderHandler $handler
69
     * @return bool
70
     */
71
    public function render(RenderHandler $handler)
72
    {
73
        sort($this->sitemaps);
74
        foreach ($this->sitemaps as $sitemap) {
75
            $handler->add(self::DIRECTIVE_SITEMAP, $sitemap);
76
        }
77
        return true;
78
    }
79
}
80