GridAbstract   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 52
c 0
b 0
f 0
dl 0
loc 146
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 17 1
A getCreateUrl() 0 5 1
A addTypeAssets() 0 8 1
A list() 0 17 1
A __construct() 0 14 1
A addCustomAssets() 0 5 1
A getBaseUrl() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Controllers\Admin;
6
7
use AbterPhp\Framework\Assets\AssetManager;
8
use AbterPhp\Framework\Constant\Event;
9
use AbterPhp\Framework\Databases\Queries\FoundRows;
10
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
11
use AbterPhp\Framework\Events\GridReady;
12
use AbterPhp\Framework\Grid\Factory\IBase as GridFactory;
13
use AbterPhp\Framework\Grid\Pagination\Options as PaginationOptions;
14
use AbterPhp\Framework\Http\Service\RepoGrid\IRepoGrid;
15
use AbterPhp\Framework\I18n\ITranslator;
16
use AbterPhp\Framework\Orm\IGridRepo;
17
use AbterPhp\Framework\Session\FlashService;
18
use Opulence\Events\Dispatchers\IEventDispatcher;
19
use Opulence\Http\Responses\Response;
20
use Opulence\Routing\Urls\UrlGenerator;
21
use Psr\Log\LoggerInterface;
22
23
abstract class GridAbstract extends AdminAbstract
24
{
25
    public const ENTITY_PLURAL       = '';
26
    public const ENTITY_TITLE_PLURAL = '';
27
28
    public const VIEW_LIST = 'contents/backend/grid';
29
30
    public const VAR_GRID       = 'grid';
31
    public const VAR_CREATE_URL = 'createUrl';
32
33
    public const TITLE_SHOW = 'framework:titleList';
34
35
    public const URL_CREATE = '%s-create';
36
37
    public const RESOURCE_DEFAULT = '%s-grid';
38
    public const RESOURCE_HEADER  = '%s-header-grid';
39
    public const RESOURCE_FOOTER  = '%s-footer-grid';
40
    public const RESOURCE_TYPE    = 'grid';
41
42
    protected IGridRepo $gridRepo;
43
44
    protected FoundRows $foundRows;
45
46
    protected GridFactory $gridFactory;
47
48
    protected PaginationOptions $paginationOptions;
49
50
    protected AssetManager $assets;
51
52
    protected IRepoGrid $repoGrid;
53
54
    protected IEventDispatcher $eventDispatcher;
55
56
    /**
57
     * GridAbstract constructor.
58
     *
59
     * @param FlashService     $flashService
60
     * @param LoggerInterface  $logger
61
     * @param ITranslator      $translator
62
     * @param UrlGenerator     $urlGenerator
63
     * @param AssetManager     $assets
64
     * @param IRepoGrid        $repoGrid
65
     * @param IEventDispatcher $eventDispatcher
66
     */
67
    public function __construct(
68
        FlashService $flashService,
69
        LoggerInterface $logger,
70
        ITranslator $translator,
71
        UrlGenerator $urlGenerator,
72
        AssetManager $assets,
73
        IRepoGrid $repoGrid,
74
        IEventDispatcher $eventDispatcher
75
    ) {
76
        parent::__construct($flashService, $logger, $translator, $urlGenerator);
77
78
        $this->assets          = $assets;
79
        $this->repoGrid        = $repoGrid;
80
        $this->eventDispatcher = $eventDispatcher;
81
    }
82
83
    /**
84
     * @return Response
85
     * @throws \Casbin\Exceptions\CasbinException
86
     * @throws \Throwable
87
     */
88
    public function show(): Response
89
    {
90
        $grid = $this->repoGrid->createAndPopulate($this->request->getQuery(), $this->getBaseUrl());
91
92
        $this->eventDispatcher->dispatch(Event::GRID_READY, new GridReady($grid));
93
94
        $grid->setTranslator($this->translator);
95
96
        $title = $this->translator->translate(static::TITLE_SHOW, static::ENTITY_TITLE_PLURAL);
97
98
        $this->view = $this->viewFactory->createView(static::VIEW_LIST);
99
        $this->view->setVar(static::VAR_GRID, $grid);
100
        $this->view->setVar(static::VAR_CREATE_URL, $this->getCreateUrl());
101
102
        $this->addCustomAssets();
103
104
        return $this->createResponse($title);
105
    }
106
107
    /**
108
     * @return Response
109
     * @throws \Casbin\Exceptions\CasbinException
110
     * @throws \Throwable
111
     */
112
    public function list(): Response
113
    {
114
        $grid = $this->repoGrid->createAndPopulate($this->request->getQuery(), $this->getBaseUrl());
115
116
        $this->eventDispatcher->dispatch(Event::GRID_READY, new GridReady($grid));
117
118
        $grid->setTranslator($this->translator);
119
120
        $title = $this->translator->translate(static::TITLE_SHOW, static::ENTITY_TITLE_PLURAL);
121
122
        $this->view = $this->viewFactory->createView(static::VIEW_LIST);
123
        $this->view->setVar(static::VAR_GRID, $grid);
124
        $this->view->setVar(static::VAR_CREATE_URL, $this->getCreateUrl());
125
126
        $this->addCustomAssets();
127
128
        return $this->createResponse($title);
129
    }
130
131
    /**
132
     * @param IStringerEntity|null $entity
133
     */
134
    protected function addCustomAssets(?IStringerEntity $entity = null)
135
    {
136
        $this->prepareCustomAssets();
137
138
        $this->addTypeAssets();
139
    }
140
141
    protected function addTypeAssets()
142
    {
143
        $groupName = $this->getResourceTypeName(static::RESOURCE_FOOTER);
144
145
        $this->assets->addJs($groupName, '/admin-assets/js/hideable-container.js');
146
        $this->assets->addJs($groupName, '/admin-assets/js/filters.js');
147
        $this->assets->addJs($groupName, '/admin-assets/js/tooltips.js');
148
        $this->assets->addJs($groupName, '/admin-assets/js/pagination.js');
149
    }
150
151
    /**
152
     * @return string
153
     * @throws \Opulence\Routing\Urls\URLException
154
     */
155
    protected function getBaseUrl(): string
156
    {
157
        return $this->urlGenerator->createFromName(static::ROUTING_PATH) . '?';
158
    }
159
160
    /**
161
     * @return string
162
     * @throws \Opulence\Routing\Urls\URLException
163
     */
164
    protected function getCreateUrl(): string
165
    {
166
        $urlName = sprintf(static::URL_CREATE, static::ROUTING_PATH);
167
168
        return $this->urlGenerator->createFromName($urlName);
169
    }
170
}
171