Passed
Pull Request — master (#259)
by Arnaud
07:11
created

AdminConfiguration::getPageParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Configuration;
6
7
use Closure;
8
use JK\Configuration\Configuration;
9
use LAG\AdminBundle\Admin\Action;
10
use LAG\AdminBundle\Admin\Admin;
11
use LAG\AdminBundle\Controller\AdminAction;
12
use LAG\AdminBundle\Exception\Exception;
13
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
14
use Symfony\Component\OptionsResolver\Options;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use function Symfony\Component\String\u;
17
18
/**
19
 * Ease Admin configuration manipulation.
20
 */
21 12
class AdminConfiguration extends Configuration
22
{
23
    protected function configureOptions(OptionsResolver $resolver): void
24 12
    {
25 12
        $resolver
26 12
            ->setRequired('entity')
27 12
            ->setAllowedTypes('entity', 'string')
28
            ->setRequired('name')
29 12
            ->setAllowedTypes('name', 'string')
30 12
31
            ->setDefault('actions', [
32
                'list' => [],
33
                'create' => [],
34
                'edit' => [],
35 12
                'delete' => [],
36 12
            ])
37
            ->setAllowedTypes('actions', 'array')
38 12
            ->setNormalizer('actions', $this->getActionNormalizer())
39 12
40
            ->setDefault('controller', AdminAction::class)
41 12
            ->setAllowedTypes('controller', 'string')
42 12
43
            ->setDefault('batch', [])
44 12
            ->setAllowedTypes('batch', 'array')
45 12
46 12
            ->setDefault('admin_class', Admin::class)
47 12
            ->setAllowedTypes('admin_class', 'string')
48
            ->setDefault('action_class', Action::class)
49 12
            ->setAllowedTypes('action_class', 'string')
50 12
51 12
            ->setDefault('routes_pattern', 'lag_admin.{admin}.{action}')
52
            ->setAllowedTypes('routes_pattern', 'string')
53 12
            ->setNormalizer('routes_pattern', $this->getRoutesPatternNormalizer())
54 12
55 12
            ->setDefault('pager', 'pagerfanta')
56 12
            ->setAllowedValues('pager', ['pagerfanta', false])
57 12
            ->setDefault('max_per_page', 25)
58 12
            ->setAllowedTypes('max_per_page', 'integer')
59
            ->setDefault('page_parameter', 'page')
60 12
            ->setAllowedTypes('page_parameter', 'string')
61 12
62
            ->setDefault('permissions', 'ROLE_ADMIN')
63 12
            ->setAllowedTypes('permissions', 'string')
64 12
65
            ->setDefault('date_format', 'Y-m-d')
66 12
            ->setAllowedTypes('date_format', 'string')
67 12
68 12
            ->setDefault('data_provider', 'doctrine')
69 12
            ->setAllowedTypes('data_provider', 'string')
70
            ->setDefault('data_persister', 'doctrine')
71 12
            ->setAllowedTypes('data_persister', 'string')
72 12
73 12
            ->setDefault('create_template', '@LAGAdmin/crud/create.html.twig')
74 12
            ->setAllowedTypes('create_template', 'string')
75 12
            ->setDefault('edit_template', '@LAGAdmin/crud/edit.html.twig')
76 12
            ->setAllowedTypes('edit_template', 'string')
77 12
            ->setDefault('list_template', '@LAGAdmin/crud/list.html.twig')
78 12
            ->setAllowedTypes('list_template', 'string')
79
            ->setDefault('delete_template', '@LAGAdmin/crud/delete.html.twig')
80 12
            ->setAllowedTypes('delete_template', 'string')
81 12
82
            ->setDefault('menus', [])
83 12
            ->setAllowedTypes('menus', 'array')
84
        ;
85 9
    }
86 9
87 9
    public function getName(): string
88 9
    {
89 9
        return $this->getString('name');
90 9
    }
91
92 12
    public function getAdminClass(): string
93
    {
94 12
        return $this->getString('admin_class');
95
    }
96 1
97
    public function getActionClass(): string
98 1
    {
99
        return $this->getString('action_class');
100
    }
101 2
102
    public function getActions(): array
103 2
    {
104
        return $this->get('actions');
105
    }
106 1
107
    public function hasAction(string $actionName): bool
108 1
    {
109
        return \array_key_exists($actionName, $this->getActions());
110
    }
111 2
112
    public function getAction(string $actionName): array
113 2
    {
114
        return $this->getActions()[$actionName];
115
    }
116 1
117
    public function getEntityClass(): string
118 1
    {
119
        return $this->getString('entity');
120
    }
121 2
122
    public function getController(): string
123 2
    {
124
        return $this->getString('controller');
125
    }
126 1
127
    public function getBatch(): array
128 1
    {
129
        return $this->get('batch');
130
    }
131 1
132
    public function getRoutesPattern(): string
133 1
    {
134
        return $this->getString('routes_pattern');
135
    }
136 1
137
    public function isPaginationEnabled(): bool
138 1
    {
139
        $pager = $this->get('pager');
140
141 1
        return !($pager === false);
142
    }
143 1
144
    public function getPager(): string
145
    {
146 2
        if (!$this->isPaginationEnabled()) {
147
            throw new Exception(sprintf('The pagination is not enabled for the admin "%s"', $this->getString('name')));
148 2
        }
149
150 2
        return $this->get('pager');
151
    }
152
153 2
    public function getMaxPerPage(): int
154
    {
155 2
        return $this->getInt('max_per_page');
156 1
    }
157
158
    public function getPageParameter(): string
159 1
    {
160
        return $this->get('page_parameter');
161
    }
162 1
163
    public function getPermissions(): array
164 1
    {
165
        $roles = explode(',', $this->get('permissions'));
166
167 1
        foreach ($roles as $index => $role) {
168
            $roles[$index] = trim($role);
169 1
        }
170
171
        return $roles;
172 1
    }
173
174 1
    public function getDateFormat(): string
175
    {
176 1
        return $this->getString('date_format');
177 1
    }
178
179
    public function getActionRouteParameters(string $actionName): array
180 1
    {
181
        $actionConfiguration = $this->getAction($actionName);
182
183 1
        if (empty($actionConfiguration['route_parameters'])) {
184
            return [];
185 1
        }
186
187
        return $actionConfiguration['route_parameters'];
188 1
    }
189
190 1
    public function getDataProvider(): string
191
    {
192 1
        return $this->getString('data_provider');
193 1
    }
194
195
    public function getDataPersister(): string
196 1
    {
197
        return $this->getString('data_persister');
198
    }
199 3
200
    public function getCreateTemplate(): string
201 3
    {
202
        return $this->getString('create_template');
203
    }
204 1
205
    public function getEditTemplate(): string
206 1
    {
207
        return $this->getString('edit_template');
208
    }
209 1
210
    public function getListTemplate(): string
211 1
    {
212
        return $this->getString('list_template');
213
    }
214 1
215
    public function getDeleteTemplate(): string
216 1
    {
217
        return $this->getString('delete_template');
218
    }
219 1
220
    public function getMenus(): array
221 1
    {
222
        return $this->get('menus');
223
    }
224 1
225
    private function getActionNormalizer(): Closure
226 1
    {
227
        return function (Options $options, $actions) {
228
            $normalizedActions = [];
229 1
//            $addBatchAction = false;
230
231 1
            foreach ($actions as $name => $action) {
232
                // action configuration is an array by default
233
                if (null === $action) {
234 3
                    $action = [];
235
                }
236 3
237
                if (!\array_key_exists('route_parameters', $action)) {
238
                    if ($name === 'edit' || $name === 'delete') {
239 2
                        $action['route_parameters'] = ['id' => null];
240
                    }
241 2
                }
242 1
                $normalizedActions[$name] = $action;
243
244
                // in list action, if no batch was configured or disabled, we add a batch action
245 1
//                if ('list' == $name && (!\array_key_exists('batch', $action) || null === $action['batch'])) {
246
//                    $addBatchAction = true;
247
//                }
248 2
            }
249
250 2
            // add empty default batch action
251 1
//            if ($addBatchAction) {
252
//             TODO enable mass action
253
//            $normalizedActions['batch'] = [];
254 1
//            }
255
256
            return $normalizedActions;
257 12
        };
258
    }
259 12
260 11
    private function getRoutesPatternNormalizer(): Closure
261
    {
262
        return function (Options $options, $value) {
263 11
            if (!u($value)->containsAny('{action}')) {
264
                throw new InvalidOptionsException(sprintf('The "%s" parameters in admin "%s" should contains the "%s" parameters', 'routes_pattern', $options->offsetGet('name'), '{action}'));
265 11
            }
266 1
267
            if (!u($value)->containsAny('{admin}')) {
268
                throw new InvalidOptionsException(sprintf('The "%s" parameters in admin "%s" should contains the "%s" parameters', 'routes_pattern', $options->offsetGet('name'), '{admin}'));
269 11
            }
270 11
271 10
            return $value;
272
        };
273
    }
274
}
275