SiteMap::make()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon\Html;
4
5
class SiteMap
6
{
7
    private $urls = [];
8
    private $hostname;
9
10 1
    public function __construct(string $hostname)
11
    {
12 1
        $this->hostname = trim($hostname, '/');
13 1
    }
14
15
    public static function make(string $hostname): SiteMap
16
    {
17
        return new self($hostname);
18
    }
19
20 1
    public function addPath(string $path): SiteMap
21
    {
22 1
        $path = trim($path, '/');
23
24 1
        $this->urls[] = "{$this->hostname}/{$path}";
25
26 1
        return $this;
27
    }
28
29 1
    public function render(): string
30
    {
31 1
        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
32 1
        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
33 1
        $dateModified = date('c');
34
35 1
        foreach ($this->urls as $url) {
36 1
            $xml .= "\t<url>\n";
37 1
            $xml .= "\t\t<loc>{$url}</loc>\n";
38 1
            $xml .= "\t\t<lastmod>{$dateModified}</lastmod>\n";
39 1
            $xml .= "\t</url>\n";
40
        }
41
42 1
        $xml .= '</urlset>';
43
44 1
        return $xml;
45
    }
46
}
47