Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

SitemapGenerator::getRouteInformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Pages\HtmlPage;
12
use Hyde\Pages\BladePage;
13
use Hyde\Pages\MarkdownPage;
14
use Hyde\Pages\MarkdownPost;
15
use Hyde\Facades\Filesystem;
16
use Hyde\Pages\InMemoryPage;
17
use Hyde\Support\Models\Route;
18
use Illuminate\Support\Carbon;
19
use Hyde\Pages\DocumentationPage;
20
use Hyde\Foundation\Facades\Routes;
21
22
use function in_array;
23
use function date;
24
25
/**
26
 * @see https://www.sitemaps.org/protocol.html
27
 */
28
class SitemapGenerator extends BaseXmlGenerator
29
{
30
    public function generate(): static
31
    {
32
        Routes::all()->each(function (Route $route): void {
33
            $this->addRoute($route);
34
        });
35
36
        return $this;
37
    }
38
39
    protected function constructBaseElement(): void
40
    {
41
        $this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
42
        $this->xmlElement->addAttribute('generator', 'HydePHP v'.Hyde::version());
43
    }
44
45
    protected function addRoute(Route $route): void
46
    {
47
        $urlItem = $this->xmlElement->addChild('url');
48
49
        $this->addChild($urlItem, 'loc', $this->resolveRouteLink($route));
50
        $this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath()));
51
        $this->addChild($urlItem, 'changefreq', $this->generateChangeFrequency(...$this->getRouteInformation($route)));
52
        $this->addChild($urlItem, 'priority', $this->generatePriority(...$this->getRouteInformation($route)));
53
    }
54
55
    protected function resolveRouteLink(Route $route): string
56
    {
57
        return Hyde::url($route->getOutputPath());
0 ignored issues
show
Bug introduced by
The method url() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        return Hyde::/** @scrutinizer ignore-call */ url($route->getOutputPath());
Loading history...
58
    }
59
60
    protected function getLastModDate(string $file): string
61
    {
62
        return date('c', @Filesystem::lastModified($file) ?: Carbon::now()->timestamp);
63
    }
64
65
    /**
66
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
67
     * @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...
68
     */
69
    protected function generatePriority(string $pageClass, string $identifier): string
70
    {
71
        $priority = 0.5;
72
73
        if (in_array($pageClass, [BladePage::class, MarkdownPage::class, DocumentationPage::class])) {
74
            $priority = 0.9;
75
76
            if ($identifier === 'index') {
77
                $priority = 1;
78
            }
79
        }
80
81
        if (in_array($pageClass, [MarkdownPost::class, InMemoryPage::class, HtmlPage::class])) {
82
            $priority = 0.75;
83
        }
84
85
        if ($identifier === '404') {
86
            $priority = 0.25;
87
        }
88
89
        return (string) $priority;
90
    }
91
92
    /**
93
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
94
     * @return 'always'|'hourly'|'daily '|'weekly'|'monthly'|'yearly'|'never'
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'always'|'hourly'|'daily at position 0 could not be parsed: Unknown type name ''always'' at position 0 in 'always'|'hourly'|'daily.
Loading history...
95
     */
96
    protected function generateChangeFrequency(string $pageClass, string $identifier): string
97
    {
98
        $frequency = 'weekly';
99
100
        if (in_array($pageClass, [BladePage::class, MarkdownPage::class, DocumentationPage::class])) {
101
            $frequency = 'daily';
102
        }
103
104
        if ($identifier === '404') {
105
            $frequency = 'monthly';
106
        }
107
108
        return $frequency;
109
    }
110
111
    /** @return array{class-string<\Hyde\Pages\Concerns\HydePage>, string} */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{class-string<\Hyde...erns\HydePage>, string} at position 2 could not be parsed: Expected ':' at position 2, but found 'class-string'.
Loading history...
112
    protected function getRouteInformation(Route $route): array
113
    {
114
        return [$route->getPageClass(), $route->getPage()->getIdentifier()];
115
    }
116
}
117