Passed
Push — master ( 335181...c02fce )
by Johannes
03:11
created

SitemapObserver::__invoke()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 20

Duplication

Lines 10
Ratio 35.71 %

Importance

Changes 0
Metric Value
dl 10
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 4
nop 1
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
        $namespace = '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[$namespace] = '';
66
        $writer->setIndent(true);
67
        $writer->startElement('{' . $namespace . '}' . 'urlset');
68 View Code Duplication
        foreach ($config['content_types']['blogEntry']['all'] as $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $writer->startElement('url');
70
            $writer->writeElement('loc', $baseUrl . '/blog.' . $id . '.html');
71
            $writer->endElement();
72
        }
73 View Code Duplication
        foreach ($config['content_types']['page']['all'] as $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $writer->startElement('url');
75
            $writer->writeElement('loc', $baseUrl . '/' . $id . '.html');
76
            $writer->endElement();
77
        }
78
        $writer->endElement();
79
        $content = $writer->outputMemory();
80
81
        file_put_contents($this->getSitemapPath(), $content);
82
    }
83
84
    /**
85
     * Get xml config writer
86
     *
87
     * @return XmlXmlWriter
88
     */
89
    public function getXmlWriter() : XmlWriter
90
    {
91
        return $this->xmlWriter;
92
    }
93
94
    /**
95
     * Get sitemap path
96
     *
97
     * @return string
98
     */
99
    public function getSitemapPath() : string
100
    {
101
        return $this->sitemapPath;
102
    }
103
104
    /**
105
     * Get base url
106
     *
107
     * @return string
108
     */
109
    public function getBaseUrl() : string
110
    {
111
        return $this->baseUrl;
112
    }
113
114
}