Passed
Push — master ( 7a1773...280ca2 )
by Johannes
04:59
created

SitemapObserver::getSitemapPath()   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
     * Constructor
32
     *
33
     * @param XmlWriter $xmlWriter
34
     * @param string $sitemapPath
35
     */
36
    public function __construct(XmlWriter $xmlWriter, string $sitemapPath)
37
    {
38
        $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...
39
        $this->sitemapPath = $sitemapPath;
40
    }
41
42
    /**
43
     * Handle parsed config
44
     * @param array $config
45
     */
46
    public function __invoke(array $config)
47
    {
48
        $ns = 'http://www.sitemaps.org/schemas/sitemap/0.9';
49
        $entries = [
50
        ];
51
        foreach ($config['content']['content_types']['blogEntry']['all'] as $id) {
52
            $entries['urlset']['url'][] = [
53
                'loc' => 'http://test.at/blog.' . $id . '.html'
54
            ];
55
        }
56
        $writer = $this->getXmlWriter();
57
58
        $writer->openMemory();
59
        $writer->startDocument('1.0', 'UTF-8');
60
        $writer->namespaceMap['http://www.sitemaps.org/schemas/sitemap/0.9'] = '';
61
        $writer->setIndent(true);
62
        $writer->startElement('{' . $ns . '}' . 'urlset');
63
        foreach ($config['content']['content_types']['blogEntry']['all'] as $id) {
64
            $entries['urlset']['url'][] = [
65
                'loc' => 'http://test.at/blog.' . $id . '.html'
66
            ];
67
            $writer->startElement('url');
68
            $writer->writeElement('loc', 'http://test.at/blog.' . $id . '.html');
69
            $writer->endElement();
70
        }
71
        $writer->endElement();
72
        $content = $writer->outputMemory();
73
74
        file_put_contents($this->getSitemapPath(), $content);
75
    }
76
77
    /**
78
     * Get xml config writer
79
     *
80
     * @return XmlXmlWriter
81
     */
82
    public function getXmlWriter()
83
    {
84
        return $this->xmlWriter;
85
    }
86
87
    /**
88
     * Get sitemap path
89
     *
90
     * @return string
91
     */
92
    public function getSitemapPath()
93
    {
94
        return $this->sitemapPath;
95
    }
96
97
}