Passed
Push — master ( 5399ee...fe113d )
by Caen
03:32 queued 12s
created

GlobalMetadataBag::make()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 16
nop 0
dl 0
loc 25
rs 9.5555
1
<?php
2
3
namespace Hyde\Framework\Modules\Metadata;
4
5
use Hyde\Framework\Concerns\HydePage;
6
use Hyde\Framework\Helpers\Features;
7
use Hyde\Framework\Helpers\Meta;
8
use Hyde\Framework\Hyde;
9
use Hyde\Framework\Services\RssFeedService;
10
use Illuminate\Support\Facades\View;
11
12
/**
13
 * @see \Hyde\Framework\Testing\Feature\GlobalMetadataBagTest
14
 */
15
class GlobalMetadataBag extends MetadataBag
16
{
17
    public static function make(): static
18
    {
19
        $metadataBag = new self();
20
21
        foreach (config('hyde.meta', []) as $item) {
22
            $metadataBag->add($item);
23
        }
24
25
        if (Features::sitemap()) {
26
            $metadataBag->add(Meta::link('sitemap', Hyde::url('sitemap.xml'), [
27
                'type' => 'application/xml', 'title' => 'Sitemap',
28
            ]));
29
        }
30
31
        if (Features::rss()) {
32
            $metadataBag->add(Meta::link('alternate', Hyde::url(RssFeedService::getDefaultOutputFilename()), [
33
                'type' => 'application/rss+xml', 'title' => RssFeedService::getDescription(),
34
            ]));
35
        }
36
37
        if (Hyde::currentPage() !== null) {
0 ignored issues
show
introduced by
The condition Hyde\Framework\Hyde::currentPage() !== null is always true.
Loading history...
38
            static::filterDuplicateMetadata($metadataBag, View::shared('page'));
39
        }
40
41
        return $metadataBag;
42
    }
43
44
    protected static function filterDuplicateMetadata(GlobalMetadataBag $global, HydePage $page): void
45
    {
46
        // Reject any metadata from the global metadata bag that is already present in the page metadata bag.
47
48
        foreach (['links', 'metadata', 'properties', 'generics'] as $type) {
49
            $global->$type = array_filter($global->$type, fn ($meta) => ! in_array($meta->uniqueKey(),
50
                array_map(fn ($meta) => $meta->uniqueKey(), $page->metadata->$type)
51
            ));
52
        }
53
    }
54
}
55