Completed
Push — dev ( e5a73e...d4e169 )
by Arnaud
03:54 queued 03:48
created

ActionConfiguration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Action\Configuration;
4
5
use LAG\AdminBundle\Admin\AdminInterface;
6
use LAG\AdminBundle\Configuration\Configuration;
7
use Symfony\Component\DependencyInjection\Container;
8
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
class ActionConfiguration extends Configuration
13
{
14
    /**
15
     * Related Action name.
16
     *
17
     * @var string
18
     */
19
    protected $actionName;
20
21
    /**
22
     * Related Admin (optional)
23
     *
24
     * @var AdminInterface
25
     */
26
    protected $admin = null;
27
28
    /**
29
     * ActionConfiguration constructor.
30
     *
31
     * @param $actionName
32
     * @param AdminInterface $admin
33
     */
34 8
    public function __construct($actionName, AdminInterface $admin)
35
    {
36 8
        parent::__construct();
37
38 8
        $this->actionName = $actionName;
39 8
        $this->admin = $admin;
40 8
    }
41
42
    /**
43
     * Define allowed parameters and values for this configuration, using optionsResolver component.
44
     *
45
     * @param OptionsResolver $resolver
46
     */
47 7
    public function configureOptions(OptionsResolver $resolver)
48
    {
49
        // action title, default to action's name
50
        $resolver
51 7
            ->setDefault('title', Container::camelize($this->actionName))
52 7
            ->setAllowedTypes('title', 'string');
53
54
        // displayed fields for this action
55
        $resolver
56 7
            ->setDefault('fields', [
57
                'id' => []
58 7
            ])
59 7
            ->setAllowedTypes('fields', 'array')
60
            ->setNormalizer('fields', function(Options $options, $fields) {
61 7
                $normalizedFields = [];
62
63 7
                foreach ($fields as $name => $field) {
64
65 7
                    if ($field === null) {
66
                        $field = [];
67
                    }
68
69 7
                    $normalizedFields[$name] = $field;
70
                }
71
72 7
                return $normalizedFields;
73 7
            })
74
        ;
75
76
        // action permissions. By default, only admin are allowed
77
        $resolver
78 7
            ->setDefault('permissions', [
79
                'ROLE_ADMIN'
80 7
            ]);
81
82
        // by default, all exports type are allowed
83
        $resolver
84 7
            ->setDefault('export', [
85 7
                'json',
86
                'html',
87
                'csv',
88
                'xls'
89
            ]);
90
91
        // entity will be retrived with this order. It should be an array of field/order mapping
92
        $resolver
93 7
            ->setDefault('order', [])
94 7
            ->setAllowedTypes('order', 'array');
95
96
        // the action route should be a string
97
        $resolver
98 7
            ->setDefault('route', '')
99 7
            ->setAllowedTypes('route', 'string')
100
            ->setNormalizer('route', function (Options $options, $value) {
101 7
                if (!$value) {
102
                    // if no route was provided, it should be linked to an Admin
103 7
                    if (!$this->admin) {
104
                        throw new InvalidOptionsException('No route was provided for action : ' . $this->actionName);
105
                    }
106
107
                    // generate default route from admin
108
                    return $this
109 7
                        ->admin
110 7
                        ->generateRouteName($this->actionName);
111
                }
112
113 1
                return $value;
114 7
            });
115
116
        // action parameters should be an array
117
        $resolver
118 7
            ->setDefault('route_parameters', [])
119 7
            ->setAllowedTypes('route_parameters', 'array');
120
121
        // font awesome icons
122
        $resolver
123 7
            ->setDefault('icon', '')
124 7
            ->setAllowedTypes('icon', 'string');
125
126
        // load strategy : determine which method should be called in the data provider
127
        $resolver
128 7
            ->setDefault('load_strategy', null)
129 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_NONE)
130 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE)
131 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_MULTIPLE)
132 7
            ->addAllowedValues('load_strategy', null)
133
            ->setNormalizer('load_strategy', function (Options $options, $value) {
134
135 7
                if (!$value) {
136 4
                    if ($this->actionName == 'create') {
137 1
                        $value = AdminInterface::LOAD_STRATEGY_NONE;
138 4
                    } else if ($this->actionName == 'list') {
139
                        $value = AdminInterface::LOAD_STRATEGY_MULTIPLE;
140
                    } else {
141 4
                        $value = AdminInterface::LOAD_STRATEGY_UNIQUE;
142
                    }
143
                }
144
145 7
                return $value;
146 7
            });
147
148
        // pagination configuration
149
        $resolver
150 7
            ->setDefault('pager', 'pagerfanta')
151 7
            ->addAllowedValues('pager', 'pagerfanta')
152 7
            ->addAllowedValues('pager', false)
153
        ;
154
155
        // criteria used to find entity in the data provider
156
        $resolver
157 7
            ->setDefault('criteria', [])
158
            ->setNormalizer('criteria', function (Options $options, $value) {
159
160 7
                if (!$value) {
161
                    $idActions = [
162 7
                        'edit',
163
                        'delete'
164
                    ];
165
166 7
                    if (in_array($this->actionName, $idActions)) {
167
                        $value = [
168
                            'id'
169 1
                        ];
170
                    }
171
                }
172
173 7
                return $value;
174 7
            })
175
        ;
176
177
        // filters
178 7
        $resolver->setDefault('filters', []);
179
180
        // menus
181
        $resolver
182 7
            ->setDefault('menus', [])
183 7
            ->setNormalizer('menus', function (Options $options, $menus) {
184
                // set default to an array
185 7
                if ($menus === false) {
186
                    $menus = [];
187
                }
188
189 7
                return $menus;
190 7
            })
191
        ;
192 7
    }
193
}
194