Publications::getPublicationsForType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications;
6
7
use Hyde\Hyde;
8
use Hyde\Publications\Concerns\PublicationFieldTypes;
9
use Hyde\Publications\Models\PublicationType;
10
use Hyde\Publications\Pages\PublicationPage;
11
use Hyde\Support\Filesystem\MediaFile;
12
use Illuminate\Support\Collection;
13
use Illuminate\Support\Str;
14
15
use function array_keys;
16
use function array_unique;
17
use function array_values;
18
use function collect;
19
20
/**
21
 * @see \Hyde\Publications\Testing\Feature\PublicationServiceTest
22
 */
23
class Publications
24
{
25
    /**
26
     * Return a collection of all defined publication types, indexed by the directory name.
27
     *
28
     * @return Collection<string, PublicationType>
29
     */
30
    public static function getPublicationTypes(): Collection
31
    {
32
        return Hyde::kernel()->getExtension(PublicationsExtension::class)->getTypes();
0 ignored issues
show
Bug introduced by
The method getTypes() does not exist on Hyde\Foundation\Concerns\HydeExtension. It seems like you code against a sub-type of Hyde\Foundation\Concerns\HydeExtension such as Hyde\Publications\PublicationsExtension. ( Ignorable by Annotation )

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

32
        return Hyde::kernel()->getExtension(PublicationsExtension::class)->/** @scrutinizer ignore-call */ getTypes();
Loading history...
33
    }
34
35
    /**
36
     * Return all publications for a given publication type.
37
     *
38
     * @return Collection<int, \Hyde\Publications\Pages\PublicationPage>
39
     */
40
    public static function getPublicationsForType(PublicationType $publicationType, ?string $sortField = null, ?bool $sortAscending = null): Collection
41
    {
42
        $publications = Hyde::pages()->getPages(PublicationPage::class);
0 ignored issues
show
Bug introduced by
The method pages() 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

42
        $publications = Hyde::/** @scrutinizer ignore-call */ pages()->getPages(PublicationPage::class);
Loading history...
43
44
        $sortAscending ??= $publicationType->sortAscending;
45
        $sortField ??= $publicationType->sortField;
46
47
        return $publications->sortBy(function (PublicationPage $page) use ($sortField): mixed {
48
            return $page->matter($sortField);
49
        }, descending: ! $sortAscending)->values()->toBase();
50
    }
51
52
    /**
53
     * Return all media items for a given publication type.
54
     */
55
    public static function getMediaForType(PublicationType $publicationType): Collection
56
    {
57
        return collect(MediaFile::all())->filter(function (MediaFile $file) use ($publicationType): bool {
58
            return Str::startsWith($file->getPath(), Hyde::getMediaDirectory().'/'.$publicationType->getDirectory());
0 ignored issues
show
Bug introduced by
The method getMediaDirectory() 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

58
            return Str::startsWith($file->getPath(), Hyde::/** @scrutinizer ignore-call */ getMediaDirectory().'/'.$publicationType->getDirectory());
Loading history...
59
        })->keys()->toBase();
60
    }
61
62
    /**
63
     * Get all available tags used in the publications.
64
     *
65
     * The tags are aggregated from the front matter of all publication pages, where the field type is "tag".
66
     *
67
     * @return array<string>
68
     */
69
    public static function getPublicationTags(): array
70
    {
71
        return array_values(array_unique(array_keys(self::getPublicationsGroupedByTags())));
72
    }
73
74
    /**
75
     * Get all pages grouped by their tags. Note that pages with multiple tags will appear multiple times.
76
     * It's also useful to count the number of times a tag is used by using `array_map('count', $pagesByTag)`.
77
     *
78
     * @experimental May be renamed to `getPublicationsGroupedByTag` before release.
79
     *
80
     * @return array<string, array<\Hyde\Publications\Pages\PublicationPage>>
81
     */
82
    public static function getPublicationsGroupedByTags(): array
83
    {
84
        $pagesByTag = [];
85
86
        /** @var PublicationPage $publication */
87
        foreach (PublicationPage::all() as $publication) {
88
            foreach (self::getPublicationTagFields($publication) as $field) {
89
                foreach ((array) $publication->matter($field->name) as $tag) {
90
                    $pagesByTag[$tag][] = $publication;
91
                }
92
            }
93
        }
94
95
        return $pagesByTag;
96
    }
97
98
    /**
99
     * Check whether a given publication type exists.
100
     */
101
    public static function publicationTypeExists(string $publicationTypeName): bool
102
    {
103
        return static::getPublicationTypes()->has(Str::slug($publicationTypeName));
104
    }
105
106
    protected static function getPublicationTagFields(PublicationPage $publication): Collection
107
    {
108
        return $publication->getType()->getFields()->whereStrict('type', PublicationFieldTypes::Tag);
0 ignored issues
show
Deprecated Code introduced by
The constant Hyde\Publications\Concer...licationFieldTypes::Tag has been deprecated: May be renamed to Tags to better fit usage ( Ignorable by Annotation )

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

108
        return $publication->getType()->getFields()->whereStrict('type', /** @scrutinizer ignore-deprecated */ PublicationFieldTypes::Tag);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
109
    }
110
}
111