Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GeneratesPublicationTagPages
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Publications\Actions;
6
7use Hyde\Foundation\Kernel\PageCollection;
8use Hyde\Pages\InMemoryPage;
9use Hyde\Publications\Publications;
10
11use 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 */
22class 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}