Conditions | 36 |
Paths | 19255 |
Total Lines | 185 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
34 | public function process(ContainerBuilder $container): void |
||
35 | { |
||
36 | // check if translator service exist |
||
37 | if (!$container->has('translator')) { |
||
38 | throw new \RuntimeException('The "translator" service is not yet enabled. |
||
39 | It\'s required by SonataAdmin to display all labels properly. |
||
40 | To learn how to enable the translator service please visit: |
||
41 | http://symfony.com/doc/current/translation.html#configuration |
||
42 | '); |
||
43 | } |
||
44 | |||
45 | $parameterBag = $container->getParameterBag(); |
||
46 | $groupDefaults = $admins = $classes = []; |
||
47 | |||
48 | $pool = $container->getDefinition('sonata.admin.pool'); |
||
49 | |||
50 | $defaultValues = [ |
||
51 | 'group' => $container->getParameter('sonata.admin.configuration.default_group'), |
||
52 | 'label_catalogue' => $container->getParameter('sonata.admin.configuration.default_label_catalogue'), |
||
53 | 'icon' => $container->getParameter('sonata.admin.configuration.default_icon'), |
||
54 | ]; |
||
55 | |||
56 | foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $tags) { |
||
57 | foreach ($tags as $attributes) { |
||
58 | $definition = $container->getDefinition($id); |
||
59 | $parentDefinition = null; |
||
60 | |||
61 | // Temporary fix until we can support service locators |
||
62 | $definition->setPublic(true); |
||
63 | |||
64 | if ($definition instanceof ChildDefinition) { |
||
65 | $parentDefinition = $container->getDefinition($definition->getParent()); |
||
66 | } |
||
67 | |||
68 | $this->replaceDefaultArguments([ |
||
69 | 0 => $id, |
||
70 | 2 => CRUDController::class, |
||
71 | ], $definition, $parentDefinition); |
||
72 | $this->applyConfigurationFromAttribute($definition, $attributes); |
||
73 | $this->applyDefaults($container, $id, $attributes); |
||
74 | |||
75 | $arguments = $parentDefinition ? |
||
76 | array_merge($parentDefinition->getArguments(), $definition->getArguments()) : |
||
77 | $definition->getArguments(); |
||
78 | |||
79 | $admins[] = $id; |
||
80 | |||
81 | if (!isset($classes[$arguments[1]])) { |
||
82 | $classes[$arguments[1]] = []; |
||
83 | } |
||
84 | |||
85 | $classes[$arguments[1]][] = $id; |
||
86 | |||
87 | $showInDashboard = (bool) (isset($attributes['show_in_dashboard']) ? $parameterBag->resolveValue($attributes['show_in_dashboard']) : true); |
||
88 | if (!$showInDashboard) { |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | $resolvedGroupName = isset($attributes['group']) ? |
||
93 | $parameterBag->resolveValue($attributes['group']) : |
||
94 | $defaultValues['group']; |
||
95 | $labelCatalogue = $attributes['label_catalogue'] ?? $defaultValues['label_catalogue']; |
||
96 | $icon = $attributes['icon'] ?? $defaultValues['icon']; |
||
97 | $onTop = $attributes['on_top'] ?? false; |
||
98 | $keepOpen = $attributes['keep_open'] ?? false; |
||
99 | |||
100 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
101 | $groupDefaults[$resolvedGroupName] = [ |
||
102 | 'label' => $resolvedGroupName, |
||
103 | 'label_catalogue' => $labelCatalogue, |
||
104 | 'icon' => $icon, |
||
105 | 'roles' => [], |
||
106 | 'on_top' => false, |
||
107 | 'keep_open' => false, |
||
108 | ]; |
||
109 | } |
||
110 | |||
111 | $groupDefaults[$resolvedGroupName]['items'][] = [ |
||
112 | 'admin' => $id, |
||
113 | 'label' => !empty($attributes['label']) ? $attributes['label'] : '', |
||
114 | 'route' => '', |
||
115 | 'route_params' => [], |
||
116 | 'route_absolute' => false, |
||
117 | ]; |
||
118 | |||
119 | if (isset($groupDefaults[$resolvedGroupName]['on_top']) && $groupDefaults[$resolvedGroupName]['on_top'] |
||
120 | || $onTop && (\count($groupDefaults[$resolvedGroupName]['items']) > 1)) { |
||
121 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
122 | } |
||
123 | $groupDefaults[$resolvedGroupName]['on_top'] = $onTop; |
||
124 | |||
125 | $groupDefaults[$resolvedGroupName]['keep_open'] = $keepOpen; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | $dashboardGroupsSettings = $container->getParameter('sonata.admin.configuration.dashboard_groups'); |
||
130 | if (!empty($dashboardGroupsSettings)) { |
||
131 | $groups = $dashboardGroupsSettings; |
||
132 | |||
133 | foreach ($dashboardGroupsSettings as $groupName => $group) { |
||
134 | $resolvedGroupName = $parameterBag->resolveValue($groupName); |
||
135 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
136 | $groupDefaults[$resolvedGroupName] = [ |
||
137 | 'items' => [], |
||
138 | 'label' => $resolvedGroupName, |
||
139 | 'label_catalogue' => $defaultValues['label_catalogue'], |
||
140 | 'icon' => $defaultValues['icon'], |
||
141 | 'roles' => [], |
||
142 | 'on_top' => false, |
||
143 | 'keep_open' => false, |
||
144 | ]; |
||
145 | } |
||
146 | |||
147 | if (empty($group['items'])) { |
||
148 | $groups[$resolvedGroupName]['items'] = $groupDefaults[$resolvedGroupName]['items']; |
||
149 | } |
||
150 | |||
151 | if (empty($group['label'])) { |
||
152 | $groups[$resolvedGroupName]['label'] = $groupDefaults[$resolvedGroupName]['label']; |
||
153 | } |
||
154 | |||
155 | if (empty($group['label_catalogue'])) { |
||
156 | $groups[$resolvedGroupName]['label_catalogue'] = $groupDefaults[$resolvedGroupName]['label_catalogue']; |
||
157 | } |
||
158 | |||
159 | if (empty($group['icon'])) { |
||
160 | $groups[$resolvedGroupName]['icon'] = $groupDefaults[$resolvedGroupName]['icon']; |
||
161 | } |
||
162 | |||
163 | if (!empty($group['item_adds'])) { |
||
164 | $groups[$resolvedGroupName]['items'] = array_merge($groups[$resolvedGroupName]['items'], $group['item_adds']); |
||
165 | } |
||
166 | |||
167 | if (empty($group['roles'])) { |
||
168 | $groups[$resolvedGroupName]['roles'] = $groupDefaults[$resolvedGroupName]['roles']; |
||
169 | } |
||
170 | |||
171 | if (isset($groups[$resolvedGroupName]['on_top']) && !empty($group['on_top']) && $group['on_top'] |
||
172 | && (\count($groups[$resolvedGroupName]['items']) > 1)) { |
||
173 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
174 | } |
||
175 | if (empty($group['on_top'])) { |
||
176 | $groups[$resolvedGroupName]['on_top'] = $groupDefaults[$resolvedGroupName]['on_top']; |
||
177 | } |
||
178 | |||
179 | if (empty($group['keep_open'])) { |
||
180 | $groups[$resolvedGroupName]['keep_open'] = $groupDefaults[$resolvedGroupName]['keep_open']; |
||
181 | } |
||
182 | } |
||
183 | } elseif ($container->getParameter('sonata.admin.configuration.sort_admins')) { |
||
184 | $groups = $groupDefaults; |
||
185 | |||
186 | $elementSort = static function (&$element): void { |
||
187 | usort( |
||
188 | $element['items'], |
||
189 | static function ($a, $b) { |
||
190 | $a = !empty($a['label']) ? $a['label'] : $a['admin']; |
||
191 | $b = !empty($b['label']) ? $b['label'] : $b['admin']; |
||
192 | |||
193 | if ($a === $b) { |
||
194 | return 0; |
||
195 | } |
||
196 | |||
197 | return $a < $b ? -1 : 1; |
||
198 | } |
||
199 | ); |
||
200 | }; |
||
201 | |||
202 | /* |
||
203 | * 1) sort the groups by their index |
||
204 | * 2) sort the elements within each group by label/admin |
||
205 | */ |
||
206 | ksort($groups); |
||
207 | array_walk($groups, $elementSort); |
||
208 | } else { |
||
209 | $groups = $groupDefaults; |
||
210 | } |
||
211 | |||
212 | $pool->addMethodCall('setAdminServiceIds', [$admins]); |
||
213 | $pool->addMethodCall('setAdminGroups', [$groups]); |
||
214 | $pool->addMethodCall('setAdminClasses', [$classes]); |
||
215 | |||
216 | $routeLoader = $container->getDefinition('sonata.admin.route_loader'); |
||
217 | $routeLoader->replaceArgument(1, $admins); |
||
218 | } |
||
219 | |||
448 |