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

SitemapObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 9.9 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 10
loc 101
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B __invoke() 10 28 3
A getXmlWriter() 0 4 1
A getSitemapPath() 0 4 1
A getBaseUrl() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}