Passed
Push — master ( b6a4c4...137285 )
by Caen
03:01 queued 13s
created

SitemapGenerator::getPriority()   A

Complexity

Conditions 6
Paths 20

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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