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