Passed
Push — master ( 84d96f...d1fdf5 )
by Caen
07:11 queued 03:05
created

SitemapGenerator::resolveRouteLink()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
/** @noinspection PhpComposerExtensionStubsInspection */
4
5
declare(strict_types=1);
6
7
namespace Hyde\Framework\Features\XmlGenerators;
8
9
use Hyde\Hyde;
10
use SimpleXMLElement;
11
use Hyde\Facades\Config;
12
use Hyde\Pages\BladePage;
13
use Hyde\Pages\MarkdownPage;
14
use Hyde\Pages\MarkdownPost;
15
use Hyde\Support\Models\Route;
16
use Hyde\Pages\DocumentationPage;
17
use Hyde\Foundation\Facades\Routes;
18
use Hyde\Framework\Concerns\TracksExecutionTime;
19
20
use function filemtime;
21
use function in_array;
22
use function date;
23
use function time;
24
25
/**
26
 * @see https://www.sitemaps.org/protocol.html
27
 */
28
class SitemapGenerator extends BaseXmlGenerator
29
{
30
    use TracksExecutionTime;
31
32
    public function generate(): static
33
    {
34
        Routes::all()->each(function (Route $route): void {
35
            $this->addRoute($route);
36
        });
37
38
        return $this;
39
    }
40
41
    public function getXml(): string
42
    {
43
        $this->xmlElement->addAttribute('processing_time_ms', $this->getFormattedProcessingTime());
44
45
        return parent::getXml();
46
    }
47
48
    protected function constructBaseElement(): void
49
    {
50
        $this->startClock();
51
52
        $this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
53
        $this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version());
54
    }
55
56
    protected function addRoute(Route $route): void
57
    {
58
        $urlItem = $this->xmlElement->addChild('url');
59
60
        $this->addChild($urlItem, 'loc', Hyde::url($route->getOutputPath()));
61
        $this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath()));
62
        $this->addChild($urlItem, 'changefreq', 'daily');
63
64
        if (Config::getBool('hyde.sitemap.dynamic_priority', true)) {
65
            $this->addChild($urlItem, 'priority', $this->getPriority(
66
                $route->getPageClass(), $route->getPage()->getIdentifier()
67
            ));
68
        }
69
    }
70
71
    protected function getLastModDate(string $file): string
72
    {
73
        return date('c', @filemtime($file) ?: time());
74
    }
75
76
    protected function getPriority(string $pageClass, string $slug): string
77
    {
78
        $priority = 0.5;
79
80
        if (in_array($pageClass, [BladePage::class, MarkdownPage::class])) {
81
            $priority = 0.9;
82
            if ($slug === 'index') {
83
                $priority = 1;
84
            }
85
            if ($slug === '404') {
86
                $priority = 0.5;
87
            }
88
        }
89
90
        if ($pageClass === DocumentationPage::class) {
91
            $priority = 0.9;
92
        }
93
94
        if ($pageClass === MarkdownPost::class) {
95
            $priority = 0.75;
96
        }
97
98
        return (string) $priority;
99
    }
100
101
    /** @return numeric-string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment numeric-string at position 0 could not be parsed: Unknown type name 'numeric-string' at position 0 in numeric-string.
Loading history...
102
    protected function getFormattedProcessingTime(): string
103
    {
104
        return (string) $this->getExecutionTimeInMs();
105
    }
106
}
107