PublicationsExtension   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemaFiles() 0 3 1
A getTypes() 0 7 2
A discoverFiles() 0 7 1
A generatePublicationListingPageForType() 0 7 2
A getPublicationFiles() 0 3 1
A getPageClasses() 0 3 1
A getPublicationFilesForType() 0 3 1
A generatePublicationTagPages() 0 3 1
A discoverPublicationPages() 0 8 1
A generatePublicationPaginatedListingPagesForType() 0 15 3
A parsePublicationTypes() 0 6 1
A shouldGeneratePublicationTagPages() 0 3 1
A discoverPages() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications;
6
7
use Hyde\Foundation\Concerns\HydeExtension;
8
use Hyde\Foundation\Facades\Files;
9
use Hyde\Foundation\Kernel\FileCollection;
10
use Hyde\Foundation\Kernel\PageCollection;
11
use Hyde\Hyde;
12
use Hyde\Pages\InMemoryPage;
13
use Hyde\Publications\Actions\GeneratesPublicationTagPages;
14
use Hyde\Publications\Models\PublicationType;
15
use Hyde\Publications\Pages\PublicationListPage;
16
use Hyde\Publications\Pages\PublicationPage;
17
use Hyde\Support\Filesystem\SourceFile;
18
use Illuminate\Support\Collection;
19
use Illuminate\Support\Str;
20
21
use function glob;
22
use function range;
23
use function str_ends_with;
24
25
/**
26
 * @see \Hyde\Publications\Testing\Feature\PublicationsExtensionTest
27
 */
28
class PublicationsExtension extends HydeExtension
29
{
30
    /** @var \Illuminate\Support\Collection<string, \Hyde\Publications\Models\PublicationType> */
31
    protected Collection $types;
32
33
    /** @return \Illuminate\Support\Collection<string, \Hyde\Publications\Models\PublicationType> */
34
    public function getTypes(): Collection
35
    {
36
        if (! isset($this->types)) {
37
            $this->types = $this->parsePublicationTypes();
38
        }
39
40
        return $this->types;
41
    }
42
43
    /** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Hyde...ges\Concerns\HydePage>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Hyde\Pages\Concerns\HydePage>>.
Loading history...
44
    public static function getPageClasses(): array
45
    {
46
        return [
47
            // Since our page classes are not auto-discoverable by Hyde due to the dynamic source directories,
48
            // we run our own discovery logic in the callbacks below.
49
        ];
50
    }
51
52
    public function discoverFiles(FileCollection $collection): void
53
    {
54
        $this->types = $this->parsePublicationTypes();
55
56
        $this->types->each(function (PublicationType $type) use ($collection): void {
57
            Collection::make($this->getPublicationFilesForType($type))->map(function (string $filepath) use ($collection): void {
0 ignored issues
show
Bug introduced by
$this->getPublicationFilesForType($type) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

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

57
            Collection::make(/** @scrutinizer ignore-type */ $this->getPublicationFilesForType($type))->map(function (string $filepath) use ($collection): void {
Loading history...
58
                $collection->put(Hyde::pathToRelative($filepath), SourceFile::make($filepath, PublicationPage::class));
0 ignored issues
show
Bug introduced by
The method pathToRelative() 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
                $collection->put(Hyde::/** @scrutinizer ignore-call */ pathToRelative($filepath), SourceFile::make($filepath, PublicationPage::class));
Loading history...
59
            });
60
        });
61
    }
62
63
    public function discoverPages(PageCollection $collection): void
64
    {
65
        $this->discoverPublicationPages($collection);
66
67
        if (self::shouldGeneratePublicationTagPages()) {
68
            $this->generatePublicationTagPages($collection);
69
        }
70
    }
71
72
    protected function discoverPublicationPages(PageCollection $instance): void
73
    {
74
        Files::getFiles(PublicationPage::class)->each(function (SourceFile $file) use ($instance): void {
0 ignored issues
show
Bug introduced by
The method getFiles() does not exist on Hyde\Foundation\Facades\Files. 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

74
        Files::/** @scrutinizer ignore-call */ 
75
               getFiles(PublicationPage::class)->each(function (SourceFile $file) use ($instance): void {
Loading history...
75
            $instance->addPage(PublicationPage::parse(Str::before($file->getPath(), PublicationPage::fileExtension())));
76
        });
77
78
        $this->types->each(function (PublicationType $type) use ($instance): void {
79
            $this->generatePublicationListingPageForType($type, $instance);
80
        });
81
    }
82
83
    protected function generatePublicationListingPageForType(PublicationType $type, PageCollection $instance): void
84
    {
85
        $page = new PublicationListPage($type);
86
        $instance->put($page->getSourcePath(), $page);
0 ignored issues
show
Bug introduced by
$page->getSourcePath() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

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

86
        $instance->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
87
88
        if ($type->usesPagination()) {
89
            $this->generatePublicationPaginatedListingPagesForType($type, $instance);
90
        }
91
    }
92
93
    protected function generatePublicationPaginatedListingPagesForType(PublicationType $type, PageCollection $instance): void
94
    {
95
        $paginator = $type->getPaginator();
96
97
        foreach (range(1, $paginator->totalPages()) as $page) {
98
            $paginator->setCurrentPage($page);
99
            $listTemplate = $type->listTemplate;
100
            if (str_ends_with($listTemplate, '.blade.php')) {
101
                $listTemplate = "{$type->getDirectory()}/$listTemplate";
102
            }
103
            $listingPage = new InMemoryPage("{$type->getDirectory()}/page-$page", [
104
                'publicationType' => $type, 'paginatorPage' => $page,
105
                'title' => $type->name.' - Page '.$page,
106
            ], view: $listTemplate);
107
            $instance->put($listingPage->getSourcePath(), $listingPage);
0 ignored issues
show
Bug introduced by
$listingPage->getSourcePath() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

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

107
            $instance->put(/** @scrutinizer ignore-type */ $listingPage->getSourcePath(), $listingPage);
Loading history...
108
        }
109
    }
110
111
    protected function generatePublicationTagPages(PageCollection $collection): void
112
    {
113
        (new GeneratesPublicationTagPages($collection))->__invoke();
114
    }
115
116
    /** @return Collection<string, \Hyde\Publications\Pages\PublicationPage> */
117
    protected function parsePublicationTypes(): Collection
118
    {
119
        return Collection::make($this->getSchemaFiles())->mapWithKeys(function (string $schemaFile): array {
0 ignored issues
show
Bug introduced by
$this->getSchemaFiles() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

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

119
        return Collection::make(/** @scrutinizer ignore-type */ $this->getSchemaFiles())->mapWithKeys(function (string $schemaFile): array {
Loading history...
120
            $type = PublicationType::fromFile(Hyde::pathToRelative($schemaFile));
121
122
            return [$type->getDirectory() => $type];
123
        });
124
    }
125
126
    protected function getSchemaFiles(): array
127
    {
128
        return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json');
0 ignored issues
show
Bug introduced by
The method path() 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

128
        return glob(Hyde::/** @scrutinizer ignore-call */ path(Hyde::getSourceRoot()).'/*/schema.json');
Loading history...
Bug introduced by
The method getSourceRoot() 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

128
        return glob(Hyde::path(Hyde::/** @scrutinizer ignore-call */ getSourceRoot()).'/*/schema.json');
Loading history...
129
    }
130
131
    protected function getPublicationFiles(string $directory): array
132
    {
133
        return glob(Hyde::path("$directory/*.md"));
134
    }
135
136
    protected function getPublicationFilesForType(PublicationType $type): array
137
    {
138
        return $this->getPublicationFiles($type->getDirectory());
139
    }
140
141
    protected static function shouldGeneratePublicationTagPages(): bool
142
    {
143
        return count(Publications::getPublicationTags()) > 0;
144
    }
145
}
146