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

BaseFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 42
dl 0
loc 136
rs 10
c 0
b 0
f 0

6 Methods

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