Completed
Pull Request — dev (#9)
by Arnaud
12:24
created

ActionConfiguration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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|null $admin
33
     */
34
    public function __construct($actionName, AdminInterface $admin = null)
35
    {
36
        parent::__construct();
37
38
        $this->actionName = $actionName;
39
        $this->admin = $admin;
40
    }
41
42
    /**
43
     * Define allowed parameters and values for this configuration, using optionsResolver component.
44
     *
45
     * @param OptionsResolver $resolver
46
     */
47
    public function configureOptions(OptionsResolver $resolver)
48
    {
49
        // action title. By default its the action name
50
        $resolver
51
            ->setDefault('title', Container::camelize($this->actionName))
52
            ->setAllowedTypes('title', 'string');
53
54
        // displayed fields for this action
55
        $resolver
56
            ->setDefault('fields', [
57
                'id' => []
58
            ])
59
            ->setAllowedTypes('fields', 'array');
60
61
        // action permissions. By default, only admin are allowed
62
        $resolver
63
            ->setDefault('permissions', [
64
                'ROLE_ADMIN'
65
            ]);
66
67
        // by default, all export type are allowed
68
        $resolver
69
            ->setDefault('export', [
70
                'json',
71
                'html',
72
                'csv',
73
                'xls'
74
            ]);
75
76
        // entity will be retrived with this order. It should be an array of field/order mapping
77
        $resolver
78
            ->setDefault('order', [])
79
            ->setAllowedTypes('order', 'array');
80
81
        // the action route should be a string
82
        $resolver
83
            ->setDefault('route', '')
84
            ->setAllowedTypes('route', 'string')
85
            ->setNormalizer('route', function (Options $options, $value) {
86
                if (!$value) {
87
                    // if no route was provided, it should be linked to an Admin
88
                    if (!$this->admin) {
89
                        throw new InvalidOptionsException('No route was provided for action : ' . $this->actionName);
90
                    }
91
92
                    // generate default route from admin
93
                    return $this
94
                        ->admin
95
                        ->generateRouteName($this->actionName);
96
                }
97
98
                return $value;
99
            });
100
101
        // action parameters should be an array
102
        $resolver
103
            ->setDefault('route_parameters', [])
104
            ->setAllowedTypes('route_parameters', 'array');
105
106
        // font awesome icons
107
        $resolver
108
            ->setDefault('icon', '')
109
            ->setAllowedTypes('icon', 'string');
110
111
        // load strategy : determine which method should be called in the data provider
112
        $resolver
113
            ->setDefault('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE)
114
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_NONE)
115
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE)
116
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_MULTIPLE)
117
            ->setNormalizer('load_strategy', function (Options $options, $value) {
118
119
                if (!$value) {
120
                    if ($this->actionName == 'create') {
121
                        $value = AdminInterface::LOAD_STRATEGY_NONE;
122
                    } else if ($this->actionName == 'list') {
123
                        $value = AdminInterface::LOAD_STRATEGY_MULTIPLE;
124
                    } else {
125
                        $value = AdminInterface::LOAD_STRATEGY_UNIQUE;
126
                    }
127
                }
128
129
                return $value;
130
            });
131
132
        // pagination configuration
133
        $resolver
134
            ->setDefault('pager', 'pagerfanta')
135
            ->addAllowedValues('pager', 'pagerfanta')
136
            ->addAllowedValues('pager', false)
137
        ;
138
139
        // criteria used to find entity in the data provider
140
        $resolver
141
            ->setDefault('criteria', [])
142
            ->setNormalizer('criteria', function (Options $options, $value) {
143
144
                if (!$value) {
145
                    $idActions = [
146
                        'edit',
147
                        'delete'
148
                    ];
149
150
                    if (in_array($this->actionName, $idActions)) {
151
                        $value = [
152
                            'id'
153
                        ];
154
                    }
155
                }
156
157
                return $value;
158
            })
159
        ;
160
161
        // filters
162
        $resolver->setDefault('filters', []);
163
    }
164
}
165