Page::getViewAttributeCallbacks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Grid\Factory;
6
7
use AbterPhp\Admin\Grid\Factory\BaseFactory;
8
use AbterPhp\Admin\Grid\Factory\GridFactory;
9
use AbterPhp\Admin\Grid\Factory\PaginationFactory;
10
use AbterPhp\Framework\Constant\Html5;
11
use AbterPhp\Framework\Grid\Action\Action;
12
use AbterPhp\Framework\Grid\Component\Actions;
13
use AbterPhp\Website\Constant\Route;
14
use AbterPhp\Website\Domain\Entities\Page as Entity;
15
use AbterPhp\Website\Grid\Factory\Table\Header\Page as HeaderFactory;
16
use AbterPhp\Website\Grid\Factory\Table\Page as TableFactory;
17
use AbterPhp\Website\Grid\Filters\Page as Filters;
18
use Opulence\Routing\Urls\UrlGenerator;
19
20
class Page extends BaseFactory
21
{
22
    public const LAYOUT_OKAY    = 'OK';
23
    public const LAYOUT_MISSING = '!';
24
25
    public const TARGET_PREVIEW = 'page-preview';
26
27
    private const GETTER_TITLE = 'getTitle';
28
29
    /**
30
     * Page constructor.
31
     *
32
     * @param UrlGenerator      $urlGenerator
33
     * @param PaginationFactory $paginationFactory
34
     * @param TableFactory      $tableFactory
35
     * @param GridFactory       $gridFactory
36
     * @param Filters           $filters
37
     */
38
    public function __construct(
39
        UrlGenerator $urlGenerator,
40
        PaginationFactory $paginationFactory,
41
        TableFactory $tableFactory,
42
        GridFactory $gridFactory,
43
        Filters $filters
44
    ) {
45
        parent::__construct($urlGenerator, $paginationFactory, $tableFactory, $gridFactory, $filters);
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getGetters(): array
52
    {
53
        return [
54
            HeaderFactory::GROUP_TITLE        => static::GETTER_TITLE,
55
            HeaderFactory::GROUP_CATEGORY     => [$this, 'getCategoryName'],
56
            HeaderFactory::GROUP_LAYOUT       => [$this, 'getLayout'],
57
            HeaderFactory::GROUP_IS_PUBLISHED => [$this, 'isPublished'],
58
        ];
59
    }
60
61
    /**
62
     * @param Entity $entity
63
     *
64
     * @return string
65
     */
66
    public function getCategoryName(Entity $entity): string
67
    {
68
        if ($entity->getCategory()) {
69
            return $entity->getCategory()->getName();
70
        }
71
72
        return '<i class="material-icons pmd-sm">remove</i>';
73
    }
74
75
    /**
76
     * @param Entity $entity
77
     *
78
     * @return string
79
     */
80
    public function getLayout(Entity $entity): string
81
    {
82
        if ($entity->getLayoutId()) {
83
            return $entity->getLayout();
84
        }
85
86
        if ($entity->getLayout()) {
87
            return '<i class="material-icons pmd-sm">remove</i>';
88
        }
89
90
        return '<i class="material-icons is-danger pmd-sm">warning</i>';
91
    }
92
93
    /**
94
     * @param Entity $entity
95
     *
96
     * @return string
97
     */
98
    public function isPublished(Entity $entity): string
99
    {
100
        if ($entity->isDraft()) {
101
            return '<i class="material-icons is-danger pmd-sm">warning</i>';
102
        }
103
104
        return '<i class="material-icons is-success pmd-sm">check circle</i>';
105
    }
106
107
    /**
108
     * @return Actions
109
     */
110
    protected function getRowActions(): Actions
111
    {
112
        $attributeCallbacks = $this->getAttributeCallbacks();
113
114
        $editAttributes   = [
115
            Html5::ATTR_HREF => Route::PAGES_EDIT,
116
        ];
117
        $deleteAttributes = [
118
            Html5::ATTR_HREF => Route::PAGES_DELETE,
119
        ];
120
121
        $cellActions   = new Actions();
122
        $cellActions[] = new Action(
123
            static::LABEL_EDIT,
124
            $this->editIntents,
125
            $editAttributes,
126
            $attributeCallbacks,
127
            Html5::TAG_A
128
        );
129
        $cellActions[] = new Action(
130
            static::LABEL_DELETE,
131
            $this->deleteIntents,
132
            $deleteAttributes,
133
            $attributeCallbacks,
134
            Html5::TAG_A
135
        );
136
        $cellActions[] = new Action(
137
            static::LABEL_VIEW,
138
            $this->viewIntents,
139
            [Html5::ATTR_TARGET => static::TARGET_PREVIEW],
140
            $this->getViewAttributeCallbacks(),
141
            Html5::TAG_A
142
        );
143
144
        return $cellActions;
145
    }
146
147
    /**
148
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
149
     *
150
     * @return callable[]
151
     */
152
    protected function getViewAttributeCallbacks(): array
153
    {
154
        $urlGenerator = $this->urlGenerator;
155
156
        // @suppress PhanUnusedVariable
157
        $hrefClosure = function ($attribute, Entity $entity) use ($urlGenerator) {
158
            // @suppress PhanTypeMismatchArgument
159
            return $urlGenerator->createFromName(Route::FALLBACK, $entity->getIdentifier());
160
        };
161
162
        $attributeCallbacks = [
163
            Html5::ATTR_HREF => $hrefClosure,
164
        ];
165
166
        return $attributeCallbacks;
167
    }
168
}
169