BaseFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 40
c 1
b 0
f 0
dl 0
loc 127
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeCallbacks() 0 11 1
A getRowActions() 0 3 1
A getGridActions() 0 3 1
A createGrid() 0 18 1
A __construct() 0 12 2
A setFilters() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Grid\Factory;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Grid\Action\Action;
9
use AbterPhp\Framework\Grid\Component\Actions;
10
use AbterPhp\Framework\Grid\Component\Filters;
11
use AbterPhp\Framework\Grid\Factory\IBase;
12
use AbterPhp\Framework\Grid\IGrid;
13
use Opulence\Orm\IEntity;
14
use Opulence\Routing\Urls\UrlGenerator;
15
16
abstract class BaseFactory implements IBase
17
{
18
    protected const LABEL_EDIT   = 'framework:editItem';
19
    protected const LABEL_DELETE = 'framework:deleteItem';
20
    protected const LABEL_VIEW   = 'framework:viewItem';
21
22
    protected UrlGenerator $urlGenerator;
23
24
    protected PaginationFactory $paginationFactory;
25
26
    protected TableFactory $tableFactory;
27
28
    protected GridFactory $gridFactory;
29
30
    protected Filters $filters;
31
32
    /** @var array */
33
    protected array $pageSizeOptions = [];
34
35
    /** @var string */
36
    protected string $url;
37
38
    /** @var string[] */
39
    protected array $downloadIntents = [Action::INTENT_WARNING];
40
41
    /** @var string[] */
42
    protected array $editIntents = [Action::INTENT_PRIMARY];
43
44
    /** @var string[] */
45
    protected array $deleteIntents = [Action::INTENT_DANGER];
46
47
    /** @var string[] */
48
    protected array $viewIntents = [Action::INTENT_DEFAULT];
49
50
    /**
51
     * Base constructor.
52
     *
53
     * @param UrlGenerator      $urlGenerator
54
     * @param PaginationFactory $paginationFactory
55
     * @param TableFactory      $tableFactory
56
     * @param GridFactory       $gridFactory
57
     * @param Filters|null      $filters
58
     */
59
    public function __construct(
60
        UrlGenerator $urlGenerator,
61
        PaginationFactory $paginationFactory,
62
        TableFactory $tableFactory,
63
        GridFactory $gridFactory,
64
        Filters $filters = null
65
    ) {
66
        $this->urlGenerator      = $urlGenerator;
67
        $this->paginationFactory = $paginationFactory;
68
        $this->tableFactory      = $tableFactory;
69
        $this->gridFactory       = $gridFactory;
70
        $this->filters           = $filters ?: new Filters();
71
    }
72
73
    /**
74
     * @param Filters $filters
75
     *
76
     * @return IBase
77
     */
78
    public function setFilters(Filters $filters): IBase
79
    {
80
        $this->filters = $filters;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param array  $params
87
     * @param string $baseUrl
88
     *
89
     * @return IGrid
90
     */
91
    public function createGrid(array $params, string $baseUrl): IGrid
92
    {
93
        $this->filters->setParams($params);
94
95
        $filterUrl = $this->filters->getUrl($baseUrl);
96
97
        $rowActions  = $this->getRowActions();
98
        $gridActions = $this->getGridActions();
99
        $getters     = $this->getGetters();
100
101
        $pagination   = $this->paginationFactory->create($params, $filterUrl);
102
        $paginatedUrl = $pagination->getPageSizeUrl($filterUrl);
103
104
        $table     = $this->tableFactory->create($getters, $rowActions, $params, $paginatedUrl);
105
        $sortedUrl = $table->getSortedUrl($paginatedUrl);
106
        $pagination->setSortedUrl($sortedUrl);
107
108
        return $this->gridFactory->create($table, $pagination, $this->filters, $gridActions);
109
    }
110
111
    abstract protected function getGetters(): array;
112
113
    /**
114
     * @return Actions
115
     */
116
    protected function getRowActions(): Actions
117
    {
118
        return new Actions();
119
    }
120
121
    /**
122
     * @return Actions
123
     */
124
    protected function getGridActions(): Actions
125
    {
126
        return new Actions();
127
    }
128
129
    /**
130
     * @return array<string,callable>
131
     */
132
    protected function getAttributeCallbacks(): array
133
    {
134
        $urlGenerator = $this->urlGenerator;
135
136
        $hrefClosure = function ($attribute, IEntity $entity) use ($urlGenerator) {
137
            // @phan-suppress-next-line PhanTypeMismatchArgument
138
            return $urlGenerator->createFromName($attribute, $entity->getId());
139
        };
140
141
        return [
142
            Html5::ATTR_HREF => $hrefClosure,
143
        ];
144
    }
145
}
146