Passed
Push — master ( 4a146d...3e1455 )
by Peter
02:17
created

PageCategory::getCategoryHtml()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 22
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
namespace AbterPhp\Website\Template\Builder;
4
5
use AbterPhp\Framework\Constant\Html5;
6
use AbterPhp\Framework\Html\Component;
7
use AbterPhp\Framework\Template\Data;
8
use AbterPhp\Framework\Template\IBuilder;
9
use AbterPhp\Framework\Template\IData;
10
use AbterPhp\Website\Constant\Event;
11
use AbterPhp\Website\Constant\Routes;
12
use AbterPhp\Website\Domain\Entities\Page as Entity;
13
use Opulence\Events\Dispatchers\IEventDispatcher;
14
use Opulence\Routing\Urls\UrlGenerator;
15
16
class PageCategory implements IBuilder
17
{
18
    const LIST_ITEM_TEMPLATE      = '<li><a href="%s">%s</a></li>';
19
    const LIST_TEMPLATE           = '<ul>%s</ul>';
20
    const LIST_CONTAINER_TEMPLATE = '<div class="page-categories"><h2>%s</h2>%s</div>';
21
22
    /** @var IEventDispatcher */
23
    protected $dispatcher;
24
25
    /** @var UrlGenerator */
26
    protected $urlGenerator;
27
28
    /**
29
     * PageCategory constructor.
30
     *
31
     * @param IEventDispatcher $dispatcher
32
     * @param UrlGenerator     $urlGenerator
33
     */
34
    public function __construct(IEventDispatcher $dispatcher, UrlGenerator $urlGenerator)
35
    {
36
        $this->dispatcher   = $dispatcher;
37
        $this->urlGenerator = $urlGenerator;
38
    }
39
40
    /**
41
     * @param Entity[] $pages
42
     *
43
     * @return Data
44
     */
45
    public function build(array $pages): IData
46
    {
47
        $category = $pages[0]->getCategory();
48
49
        $body = $this->getCategoryHtml($pages, $category->getName());
50
51
        $this->dispatcher->dispatch(Event::PAGE_CATEGORY_READY, $body);
52
53
        return new Data(
54
            $category->getIdentifier(),
55
            [],
56
            ['body' => (string)$body]
57
        );
58
    }
59
60
    /**
61
     * @param Entity[] $pages
62
     * @param string   $categoryName
63
     *
64
     * @return Component
65
     */
66
    protected function getCategoryHtml(array $pages, string $categoryName): Component
0 ignored issues
show
Unused Code introduced by
The parameter $categoryName is not used and could be removed. ( Ignorable by Annotation )

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

66
    protected function getCategoryHtml(array $pages, /** @scrutinizer ignore-unused */ string $categoryName): Component

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        $container = new Component(null, [], [Html5::ATTR_CLASS => 'page-category'], Html5::TAG_DIV);
69
70
        if (count($pages) === 0) {
71
            return $container;
72
        }
73
74
        $list = new Component(null, [], [], Html5::TAG_UL);
75
        foreach ($pages as $page) {
76
            $url   = $this->urlGenerator->createFromName(Routes::ROUTE_PAGE_OTHER, $page->getIdentifier());
77
            $title = $page->getTitle();
78
79
            $a = new Component($title, [], [Html5::ATTR_HREF => $url], Html5::TAG_A);
80
81
            $list[] = new Component($a, [], [], Html5::TAG_LI);
82
        }
83
84
        $container[] = new Component($pages[0]->getCategory()->getName(), [], [], Html5::TAG_H2);
85
        $container[] = $list;
86
87
        return $container;
88
    }
89
}
90