GeneratesPublicationTagPages::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Actions;
6
7
use Hyde\Foundation\Kernel\PageCollection;
8
use Hyde\Pages\InMemoryPage;
9
use Hyde\Publications\Publications;
10
11
use function array_map;
12
13
/**
14
 * Called by the PublicationsExtension::discoverPages method,
15
 * during the HydePHP autodiscovery boot process.
16
 *
17
 * @todo: Refactor to use page models, or at the very least, view components
18
 *
19
 * @see \Hyde\Publications\PublicationsExtension::discoverPages()
20
 * @see \Hyde\Publications\Testing\Feature\GeneratesPublicationTagPagesTest
21
 */
22
class GeneratesPublicationTagPages
23
{
24
    protected PageCollection $pageCollection;
25
26
    public function __construct(PageCollection $collection)
27
    {
28
        $this->pageCollection = $collection;
29
    }
30
31
    public function __invoke(): void
32
    {
33
        // Set the basename for the tags route (generated pages will be located at /tags/{tag})
34
        $tagsRouteBasename = 'tags';
35
36
        $pagesByTag = Publications::getPublicationsGroupedByTags();
37
38
        // Build the index tags page
39
        $this->pageCollection->addPage(new InMemoryPage("$tagsRouteBasename/index", [
40
            /** @var array<string, int> $tags */
41
            'tags' => array_map('count', $pagesByTag),
42
        ], view: 'hyde-publications::tags_list'));
43
44
        // Build individual page lists for each tag
45
        foreach ($pagesByTag as $tag => $pages) {
46
            $this->pageCollection->addPage(new InMemoryPage(
47
                "$tagsRouteBasename/$tag",
48
                ['tag' => $tag, 'publications' => $pages],
49
                view: 'hyde-publications::tags_detail'
50
            ));
51
        }
52
    }
53
}
54