Conditions | 8 |
Paths | 1 |
Total Lines | 117 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 |