Passed
Push — master ( db5997...3f8836 )
by Peter
02:31
created

Page::getViewAttributeCallbacks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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