Passed
Push — master ( 6af672...0107a4 )
by Peter
07:37
created

Detailed   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 50
c 3
b 0
f 0
dl 0
loc 164
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A buildCategory() 0 17 2
A buildPageButtons() 0 10 1
A buildPage() 0 12 1
A build() 0 20 3
A getIdentifier() 0 3 1
A buildPageTitle() 0 5 1
A __construct() 0 5 1
A buildPageLede() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Template\Builder\PageCategory;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Component;
9
use AbterPhp\Framework\I18n\ITranslator;
10
use AbterPhp\Framework\Template\Data;
11
use AbterPhp\Framework\Template\IBuilder;
12
use AbterPhp\Framework\Template\IData;
13
use AbterPhp\Framework\Template\ParsedTemplate;
14
use AbterPhp\Website\Constant\Event;
15
use AbterPhp\Website\Constant\Route;
16
use AbterPhp\Website\Domain\Entities\Page as Entity;
17
use Opulence\Events\Dispatchers\IEventDispatcher;
18
use Opulence\Routing\Urls\UrlGenerator;
19
20
class Detailed implements IBuilder
21
{
22
    const IDENTIFIER = 'detailed';
23
24
    const MORE_BTN_CONTAINER_CLASS = 'more-btn-container';
25
26
    const CLASS_LEDE = 'detailed-lede';
27
28
    /** @var IEventDispatcher */
29
    protected $dispatcher;
30
31
    /** @var UrlGenerator */
32
    protected $urlGenerator;
33
34
    /** @var ITranslator */
35
    protected $translator;
36
37
    /**
38
     * PageCategory constructor.
39
     *
40
     * @param IEventDispatcher $dispatcher
41
     * @param UrlGenerator     $urlGenerator
42
     * @param ITranslator      $translator
43
     */
44
    public function __construct(IEventDispatcher $dispatcher, UrlGenerator $urlGenerator, ITranslator $translator)
45
    {
46
        $this->dispatcher   = $dispatcher;
47
        $this->urlGenerator = $urlGenerator;
48
        $this->translator   = $translator;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getIdentifier(): string
55
    {
56
        return static::IDENTIFIER;
57
    }
58
59
    /**
60
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
61
     *
62
     * @param mixed               $pages
63
     * @param ParsedTemplate|null $template
64
     *
65
     * @return IData
66
     */
67
    public function build($pages, ?ParsedTemplate $template = null): IData
68
    {
69
        if (count($pages) === 0) {
70
            throw new \InvalidArgumentException();
71
        }
72
73
        if (!$pages[0]->getCategory()) {
74
            throw new \LogicException();
75
        }
76
77
        $category = $pages[0]->getCategory();
78
79
        $body = $this->buildCategory($pages, $category->getName(), $category->getIdentifier());
80
81
        $this->dispatcher->dispatch(Event::PAGE_CATEGORY_READY, $body);
82
83
        return new Data(
84
            $category->getIdentifier(),
85
            [],
86
            ['body' => (string)$body]
87
        );
88
    }
89
90
    /**
91
     * @param Entity[] $pages
92
     * @param string   $categoryName
93
     * @param string   $categoryIdentifier
94
     *
95
     * @return Component
96
     * @throws \Opulence\Routing\Urls\URLException
97
     */
98
    protected function buildCategory(array $pages, string $categoryName, string $categoryIdentifier): Component
99
    {
100
        $container = new Component(null, [], [Html5::ATTR_CLASS => 'page-category'], Html5::TAG_SECTION);
101
102
        $list = new Component(null, [], [Html5::ATTR_CLASS => 'page-container'], Html5::TAG_DIV);
103
        foreach ($pages as $page) {
104
            $list[] = $this->buildPage($page);
105
        }
106
107
        // @phan-suppress-next-line PhanTypeMismatchArgument
108
        $url = $this->urlGenerator->createFromName(Route::FALLBACK, $categoryIdentifier);
109
        $a   = new Component($categoryName, [], [Html5::ATTR_HREF => $url], Html5::TAG_A);
110
111
        $container[] = new Component($a, [], [], Html5::TAG_H2);
112
        $container[] = $list;
113
114
        return $container;
115
    }
116
117
    /**
118
     * @param Entity $page
119
     *
120
     * @return Component
121
     * @throws \Opulence\Routing\Urls\URLException
122
     */
123
    protected function buildPage(Entity $page): Component
124
    {
125
        $item = new Component(null, [], [], Html5::TAG_ARTICLE);
126
127
        // @phan-suppress-next-line PhanTypeMismatchArgument
128
        $url = $this->urlGenerator->createFromName(Route::FALLBACK, $page->getIdentifier());
129
130
        $item[] = $this->buildPageTitle($page, $url);
131
        $item[] = $this->buildPageLede($page);
132
        $item[] = $this->buildPageButtons($url);
133
134
        return $item;
135
    }
136
137
    /**
138
     * @param Entity $page
139
     * @param string $url
140
     *
141
     * @return Component
142
     */
143
    protected function buildPageTitle(Entity $page, string $url): Component
144
    {
145
        $a = new Component($page->getTitle(), [], [Html5::ATTR_HREF => $url], Html5::TAG_A);
146
147
        return new Component($a, [], [], Html5::TAG_H3);
148
    }
149
150
    /**
151
     * @param Entity $page
152
     *
153
     * @return Component
154
     */
155
    protected function buildPageLede(Entity $page): Component
156
    {
157
        $lede = new Component(null, [], [Html5::ATTR_CLASS => static::CLASS_LEDE], Html5::TAG_DIV);
158
        foreach (explode("\n", $page->getLede()) as $paragraph) {
159
            if (trim($paragraph) === '') {
160
                continue;
161
            }
162
163
            $lede[] = new Component($paragraph, [], [], Html5::TAG_P);
164
        }
165
166
        return $lede;
167
    }
168
169
    /**
170
     * @param string $url
171
     *
172
     * @return Component
173
     */
174
    protected function buildPageButtons(string $url): Component
175
    {
176
        $iconHtml = '<i class="fas fa-angle-right"></i>';
177
        $aContent = sprintf('%s&nbsp;%s', $this->translator->translate('website:more'), $iconHtml);
178
179
        $a       = new Component($aContent, [], [Html5::ATTR_HREF => $url], Html5::TAG_A);
180
        $p       = new Component($a, [], [], Html5::TAG_P);
181
        $buttons = new Component($p, [], [Html5::ATTR_CLASS => static::MORE_BTN_CONTAINER_CLASS], Html5::TAG_DIV);
182
183
        return $buttons;
184
    }
185
}
186