Passed
Push — master ( 7b7005...55ec05 )
by Brent
02:29
created

SiteMap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isEnabled() 0 3 2
A addPath() 0 7 1
A render() 0 16 2
1
<?php
2
3
namespace Brendt\Stitcher\Site\Seo;
4
5
class SiteMap
6
{
7
    private $urls = [];
8
    private $hostname = '';
9
10
    public function __construct($hostname) {
11
        $this->hostname = trim((string) $hostname, '/');
12
    }
13
14
    public function isEnabled() : bool {
15
        return $this->hostname !== null && $this->hostname !== '';
16
    }
17
18
    public function addPath(string $path) : SiteMap {
19
        $path = trim($path, '/');
20
21
        $this->urls[] = "{$this->hostname}/{$path}";
22
23
        return $this;
24
    }
25
26
    public function render() : string {
27
        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
28
        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
29
        $dateModified = date('c');
30
31
        foreach ($this->urls as $url) {
32
            $xml .= "\t<url>\n";
33
            $xml .= "\t\t<loc>{$url}</loc>\n";
34
            $xml .= "\t\t<lastmod>{$dateModified}</lastmod>\n";
35
            $xml .= "\t</url>\n";
36
        }
37
38
        $xml .= '</urlset>';
39
40
        return $xml;
41
    }
42
}
43