Completed
Pull Request — dev (#9)
by Arnaud
02:46
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|null $admin
33
     */
34 7
    public function __construct($actionName, AdminInterface $admin = null)
35
    {
36 7
        parent::__construct();
37
38 7
        $this->actionName = $actionName;
39 7
        $this->admin = $admin;
40 7
    }
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 7
                'id' => []
58 7
            ])
59 7
            ->setAllowedTypes('fields', 'array');
60
61
        // action permissions. By default, only admin are allowed
62
        $resolver
63 7
            ->setDefault('permissions', [
64
                'ROLE_ADMIN'
65 7
            ]);
66
67
        // by default, all exports type are allowed
68
        $resolver
69 7
            ->setDefault('export', [
70 7
                'json',
71 7
                'html',
72 7
                'csv',
73
                'xls'
74 7
            ]);
75
76
        // entity will be retrived with this order. It should be an array of field/order mapping
77
        $resolver
78 7
            ->setDefault('order', [])
79 7
            ->setAllowedTypes('order', 'array');
80
81
        // the action route should be a string
82
        $resolver
83 7
            ->setDefault('route', '')
84 7
            ->setAllowedTypes('route', 'string')
85
            ->setNormalizer('route', function (Options $options, $value) {
86 7
                if (!$value) {
87
                    // if no route was provided, it should be linked to an Admin
88 7
                    if (!$this->admin) {
89
                        throw new InvalidOptionsException('No route was provided for action : ' . $this->actionName);
90
                    }
91
92
                    // generate default route from admin
93 7
                    return $this
94
                        ->admin
95 7
                        ->generateRouteName($this->actionName);
96
                }
97
98 1
                return $value;
99 7
            });
100
101
        // action parameters should be an array
102
        $resolver
103 7
            ->setDefault('route_parameters', [])
104 7
            ->setAllowedTypes('route_parameters', 'array');
105
106
        // font awesome icons
107
        $resolver
108 7
            ->setDefault('icon', '')
109 7
            ->setAllowedTypes('icon', 'string');
110
111
        // load strategy : determine which method should be called in the data provider
112
        $resolver
113 7
            ->setDefault('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE)
114 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_NONE)
115 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE)
116 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_MULTIPLE)
117
            ->setNormalizer('load_strategy', function (Options $options, $value) {
118
119 7
                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 7
                return $value;
130 7
            });
131
132
        // pagination configuration
133
        $resolver
134 7
            ->setDefault('pager', 'pagerfanta')
135 7
            ->addAllowedValues('pager', 'pagerfanta')
136 7
            ->addAllowedValues('pager', false)
137
        ;
138
139
        // criteria used to find entity in the data provider
140
        $resolver
141 7
            ->setDefault('criteria', [])
142 7
            ->setNormalizer('criteria', function (Options $options, $value) {
143
144 7
                if (!$value) {
145
                    $idActions = [
146 7
                        'edit',
147
                        'delete'
148 7
                    ];
149
150 7
                    if (in_array($this->actionName, $idActions)) {
151
                        $value = [
152
                            'id'
153 1
                        ];
154 1
                    }
155 7
                }
156
157 7
                return $value;
158 7
            })
159
        ;
160
161
        // filters
162 7
        $resolver->setDefault('filters', []);
163 7
    }
164
}
165