Completed
Push — master ( 280ca2...4c75e7 )
by Johannes
02:18
created

SitemapObserver::getXmlWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Lichtenwallner  (https://lichtenwallner.at)
4
 *
5
 * @see https://github.com/jolicht/markdown-cms for the canonical source repository
6
 * @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT
7
 * @copyright Copyright (c) Johannes Lichtenwallner
8
 */
9
declare(strict_types = 1);
10
namespace Jolicht\MarkdownCms\Parser\Observer;
11
12
use Sabre\Xml\Writer as XmlWriter;
13
14
class SitemapObserver implements ParsedContentObserverInterface
15
{
16
    /**
17
     * Config writer
18
     *
19
     * @var XmlXmlWriter
20
     */
21
    private $xmlWriter;
22
23
    /**
24
     * Sitemap path
25
     *
26
     * @var string
27
     */
28
    private $sitemapPath;
29
30
    /**
31
     * Base url
32
     *
33
     * @var string
34
     */
35
    private $baseUrl;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param XmlWriter $xmlWriter
41
     * @param string $sitemapPath
42
     * @param string $baseUrl
43
     */
44
    public function __construct(XmlWriter $xmlWriter, string $sitemapPath, string $baseUrl)
45
    {
46
        $this->xmlWriter = $xmlWriter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xmlWriter of type object<Sabre\Xml\Writer> is incompatible with the declared type object<Jolicht\MarkdownC...\Observer\XmlXmlWriter> of property $xmlWriter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
        $this->sitemapPath = $sitemapPath;
48
        $this->baseUrl = rtrim($baseUrl, '/');
49
    }
50
51
    /**
52
     * Handle parsed config
53
     * @param array $config
54
     */
55
    public function __invoke(array $config)
56
    {
57
        $ns = 'http://www.sitemaps.org/schemas/sitemap/0.9';
58
59
        $baseUrl = $this->getBaseUrl();
60
61
        $writer = $this->getXmlWriter();
62
63
        $writer->openMemory();
64
        $writer->startDocument('1.0', 'UTF-8');
65
        $writer->namespaceMap[$ns] = '';
66
        $writer->setIndent(true);
67
        $writer->startElement('{' . $ns . '}' . 'urlset');
68
        foreach ($config['content_types']['blogEntry']['all'] as $id) {
69
            $writer->startElement('url');
70
            $writer->writeElement('loc', $baseUrl . '/blog.' . $id . '.html');
71
            $writer->endElement();
72
        }
73
        $writer->endElement();
74
        $content = $writer->outputMemory();
75
76
        file_put_contents($this->getSitemapPath(), $content);
77
    }
78
79
    /**
80
     * Get xml config writer
81
     *
82
     * @return XmlXmlWriter
83
     */
84
    public function getXmlWriter() : XmlWriter
85
    {
86
        return $this->xmlWriter;
87
    }
88
89
    /**
90
     * Get sitemap path
91
     *
92
     * @return string
93
     */
94
    public function getSitemapPath() : string
95
    {
96
        return $this->sitemapPath;
97
    }
98
99
    /**
100
     * Get base url
101
     *
102
     * @return string
103
     */
104
    public function getBaseUrl() : string
105
    {
106
        return $this->baseUrl;
107
    }
108
109
}