SitemapXmlFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 21
dl 0
loc 38
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A createFromUrlSet() 0 30 5
1
<?php
2
3
namespace PODEntender\SitemapGenerator\Factory;
4
5
use PODEntender\SitemapGenerator\UrlSet;
6
7
use \DOMDocument;
8
9
class SitemapXmlFactory
10
{
11
    const DOM_VERSION = '1.0';
12
13
    const DOM_ENCODING = 'utf-8';
14
15
    const DATE_FORMAT = 'Y-m-d';
16
17 6
    public function createFromUrlSet(UrlSet $urlSet): DOMDocument
18
    {
19 6
        $urlIterator = $urlSet->getIterator();
20 6
        $document = new DOMDocument(self::DOM_VERSION, self::DOM_ENCODING);
21 6
        $root = $document->createElement('urlset');
22 6
        $root->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
23
24 6
        foreach ($urlIterator as $url) {
25 5
            $urlNode = $document->createElement('url');
26 5
            $urlNode->appendChild($document->createElement('loc', $url->location()));
27
28 5
            if ($url->lastModified() !== null) {
29 4
                $urlNode->appendChild(
30 4
                    $document->createElement('lastmod', $url->lastModified()->format(self::DATE_FORMAT))
31
                );
32
            }
33
34 5
            if ($url->changeFrequency() !== null) {
35 4
                $urlNode->appendChild($document->createElement('changefreq', $url->changeFrequency()));
36
            }
37
38 5
            if ($url->priority() !== null) {
39 4
                $urlNode->appendChild($document->createElement('priority', $url->priority()));
40
            }
41
42 5
            $root->appendChild($urlNode);
43
        }
44
45 6
        $document->appendChild($root);
46 6
        return $document;
47
    }
48
}
49