Passed
Push — master ( f85738...3d85c1 )
by Peter
04:52
created

GridAbstract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 43
dl 0
loc 130
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 17 1
A getCreateUrl() 0 6 1
A addTypeAssets() 0 8 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
    const ENTITY_PLURAL       = '';
26
    const ENTITY_TITLE_PLURAL = '';
27
28
    const VIEW_LIST = 'contents/backend/grid';
29
30
    const VAR_GRID       = 'grid';
31
    const VAR_CREATE_URL = 'createUrl';
32
33
    const TITLE_SHOW = 'framework:titleList';
34
35
    const URL_CREATE = '%s-create';
36
37
    const RESOURCE_DEFAULT = '%s-grid';
38
    const RESOURCE_HEADER  = '%s-header-grid';
39
    const RESOURCE_FOOTER  = '%s-footer-grid';
40
    const RESOURCE_TYPE    = 'grid';
41
42
    /** @var IGridRepo */
43
    protected $gridRepo;
44
45
    /** @var FoundRows */
46
    protected $foundRows;
47
48
    /** @var GridFactory */
49
    protected $gridFactory;
50
51
    /** @var PaginationOptions */
52
    protected $paginationOptions;
53
54
    /** @var AssetManager */
55
    protected $assets;
56
57
    /** @var IRepoGrid */
58
    protected $repoGrid;
59
60
    /** @var IEventDispatcher */
61
    protected $eventDispatcher;
62
63
    /**
64
     * GridAbstract constructor.
65
     *
66
     * @param FlashService     $flashService
67
     * @param ITranslator      $translator
68
     * @param UrlGenerator     $urlGenerator
69
     * @param LoggerInterface  $logger
70
     * @param AssetManager     $assets
71
     * @param IRepoGrid        $repoGrid
72
     * @param IEventDispatcher $eventDispatcher
73
     */
74
    public function __construct(
75
        FlashService $flashService,
76
        ITranslator $translator,
77
        UrlGenerator $urlGenerator,
78
        LoggerInterface $logger,
79
        AssetManager $assets,
80
        IRepoGrid $repoGrid,
81
        IEventDispatcher $eventDispatcher
82
    ) {
83
        parent::__construct($flashService, $translator, $urlGenerator, $logger);
84
85
        $this->assets          = $assets;
86
        $this->repoGrid        = $repoGrid;
87
        $this->eventDispatcher = $eventDispatcher;
88
    }
89
90
    /**
91
     * @return Response
92
     * @throws \Casbin\Exceptions\CasbinException
93
     * @throws \Throwable
94
     */
95
    public function show(): Response
96
    {
97
        $grid = $this->repoGrid->createAndPopulate($this->request->getQuery(), $this->getBaseUrl());
98
99
        $this->eventDispatcher->dispatch(Event::GRID_READY, new GridReady($grid));
100
101
        $grid->setTranslator($this->translator);
102
103
        $title = $this->translator->translate(static::TITLE_SHOW, static::ENTITY_TITLE_PLURAL);
104
105
        $this->view = $this->viewFactory->createView(static::VIEW_LIST);
106
        $this->view->setVar(static::VAR_GRID, $grid);
107
        $this->view->setVar(static::VAR_CREATE_URL, $this->getCreateUrl());
108
109
        $this->addCustomAssets();
110
111
        return $this->createResponse($title);
112
    }
113
114
    /**
115
     * @param IStringerEntity|null $entity
116
     */
117
    protected function addCustomAssets(?IStringerEntity $entity = null)
118
    {
119
        $this->prepareCustomAssets();
120
121
        $this->addTypeAssets();
122
    }
123
124
    protected function addTypeAssets()
125
    {
126
        $groupName = $this->getResourceTypeName(static::RESOURCE_FOOTER);
127
128
        $this->assets->addJs($groupName, '/admin-assets/js/hideable-container.js');
129
        $this->assets->addJs($groupName, '/admin-assets/js/filters.js');
130
        $this->assets->addJs($groupName, '/admin-assets/js/tooltips.js');
131
        $this->assets->addJs($groupName, '/admin-assets/js/pagination.js');
132
    }
133
134
    /**
135
     * @return string
136
     * @throws \Opulence\Routing\Urls\URLException
137
     */
138
    protected function getBaseUrl(): string
139
    {
140
        return $this->urlGenerator->createFromName(static::ENTITY_PLURAL) . '?';
141
    }
142
143
    /**
144
     * @return string
145
     * @throws \Opulence\Routing\Urls\URLException
146
     */
147
    protected function getCreateUrl(): string
148
    {
149
        $urlName = strtolower(sprintf(static::URL_CREATE, static::ENTITY_PLURAL));
150
        $url     = $this->urlGenerator->createFromName($urlName);
151
152
        return $url;
153
    }
154
}
155