Passed
Push — master ( d0e934...b4bd91 )
by Caen
04:29 queued 12s
created

SitemapService::canGenerateSitemap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/** @noinspection PhpComposerExtensionStubsInspection */
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Framework\Contracts\RouteContract;
8
use Hyde\Framework\Hyde;
9
use Hyde\Framework\Models\Pages\BladePage;
10
use Hyde\Framework\Models\Pages\DocumentationPage;
11
use Hyde\Framework\Models\Pages\MarkdownPage;
12
use Hyde\Framework\Models\Pages\MarkdownPost;
13
use Hyde\Framework\Models\Route;
14
use SimpleXMLElement;
15
16
/**
17
 * @see \Hyde\Framework\Testing\Feature\Services\SitemapServiceTest
18
 * @see https://www.sitemaps.org/protocol.html
19
 * @phpstan-consistent-constructor
20
 */
21
class SitemapService
22
{
23
    public SimpleXMLElement $xmlElement;
24
    protected float $time_start;
25
26
    public function __construct()
27
    {
28
        if (! extension_loaded('simplexml') || config('testing.mock_disabled_extensions', false) === true) {
29
            throw new \Exception('The ext-simplexml extension is not installed, but is required to generate RSS feeds.');
30
        }
31
32
        $this->time_start = microtime(true);
33
34
        $this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
35
        $this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version());
36
    }
37
38
    public function generate(): static
39
    {
40
        Route::all()->each(function ($route) {
41
            $this->addRoute($route);
42
        });
43
44
        return $this;
45
    }
46
47
    public function getXML(): string|false
48
    {
49
        $this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2));
50
51
        return $this->xmlElement->asXML();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xmlElement->asXML() could return the type true which is incompatible with the type-hinted return false|string. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    public function addRoute(RouteContract $route): void
55
    {
56
        $urlItem = $this->xmlElement->addChild('url');
57
        $urlItem->addChild('loc', htmlentities($route->getQualifiedUrl()));
58
        $urlItem->addChild('lastmod', htmlentities($this->getLastModDate($route->getSourceFilePath())));
59
        $urlItem->addChild('changefreq', 'daily');
60
        if (config('hyde.sitemap.dynamic_priority', true)) {
61
            $urlItem->addChild('priority', $this->getPriority($route->getPageType(), $route->getSourceModel()->slug));
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Hyde\Framework\Contracts\PageContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62
        }
63
    }
64
65
    protected function getLastModDate(string $file): string
66
    {
67
        return date('c', filemtime(
68
            $file
69
        ));
70
    }
71
72
    protected function getPriority(string $pageClass, string $slug): string
73
    {
74
        $priority = 0.5;
75
76
        if (in_array($pageClass, [BladePage::class, MarkdownPage::class])) {
77
            $priority = 0.9;
78
            if ($slug === 'index') {
79
                $priority = 1;
80
            }
81
            if ($slug === '404') {
82
                $priority = 0.5;
83
            }
84
        }
85
86
        if ($pageClass === DocumentationPage::class) {
87
            $priority = 0.9;
88
        }
89
90
        if ($pageClass === MarkdownPost::class) {
91
            $priority = 0.75;
92
        }
93
94
        return (string) $priority;
95
    }
96
97
    public static function generateSitemap(): string
98
    {
99
        return (new static)->generate()->getXML();
100
    }
101
}
102