| Conditions | 17 |
| Paths | 217 |
| Total Lines | 101 |
| Code Lines | 59 |
| Lines | 28 |
| Ratio | 27.72 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 103 | public function actionCreate(AdminEvent $event) |
||
| 104 | { |
||
| 105 | // add configuration only if extra configuration is enabled |
||
| 106 | if (!$this->enableExtraConfiguration) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | // action configuration array |
||
| 110 | $configuration = $event->getConfiguration(); |
||
| 111 | // current action admin |
||
| 112 | $admin = $event->getAdmin(); |
||
| 113 | // allowed actions according to the admin |
||
| 114 | $keys = $admin |
||
| 115 | ->getConfiguration() |
||
| 116 | ->getActions(); |
||
| 117 | $allowedActions = array_keys($keys); |
||
| 118 | |||
| 119 | // if no field was provided in configuration, we try to take fields from doctrine metadata |
||
| 120 | if (empty($configuration['fields']) || !count($configuration['fields'])) { |
||
| 121 | $fields = []; |
||
| 122 | $guesser = new FieldTypeGuesser(); |
||
| 123 | $metadata = $this |
||
| 124 | ->entityManager |
||
| 125 | ->getMetadataFactory() |
||
| 126 | ->getMetadataFor($admin->getConfiguration()->getEntityName()); |
||
| 127 | $fieldsName = $metadata->getFieldNames(); |
||
| 128 | |||
| 129 | foreach ($fieldsName as $name) { |
||
| 130 | $type = $metadata->getTypeOfField($name); |
||
| 131 | // get field type from doctrine type |
||
| 132 | $fieldConfiguration = $guesser->getTypeAndOptions($type); |
||
| 133 | |||
| 134 | // if a field configuration was found, we take it |
||
| 135 | if (count($fieldConfiguration)) { |
||
| 136 | $fields[$name] = $fieldConfiguration; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | if (count($fields)) { |
||
| 140 | // adding new fields to action configuration |
||
| 141 | $configuration['fields'] = $fields; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | // configured linked actions |
||
| 145 | if (array_key_exists('_actions', $configuration['fields']) |
||
| 146 | && !array_key_exists('type', $configuration['fields']['_actions']) |
||
| 147 | ) { |
||
| 148 | // in list view, we add by default and an edit and a delete button |
||
| 149 | if ($event->getActionName() == 'list') { |
||
| 150 | View Code Duplication | if (in_array('edit', $allowedActions)) { |
|
| 151 | $configuration['fields']['_actions']['type'] = 'collection'; |
||
| 152 | $configuration['fields']['_actions']['options']['_edit'] = [ |
||
| 153 | 'type' => 'action', |
||
| 154 | 'options' => [ |
||
| 155 | 'title' => $this->applicationConfiguration->getTranslationKey('edit', $event->getAdmin()->getName()), |
||
| 156 | 'route' => $admin->generateRouteName('edit'), |
||
| 157 | 'parameters' => [ |
||
| 158 | 'id' => false |
||
| 159 | ], |
||
| 160 | 'icon' => 'pencil' |
||
| 161 | ] |
||
| 162 | ]; |
||
| 163 | } |
||
| 164 | View Code Duplication | if (in_array('delete', $allowedActions)) { |
|
| 165 | $configuration['fields']['_actions']['type'] = 'collection'; |
||
| 166 | $configuration['fields']['_actions']['options']['_delete'] = [ |
||
| 167 | 'type' => 'action', |
||
| 168 | 'options' => [ |
||
| 169 | 'title' => $this->applicationConfiguration->getTranslationKey('delete', $event->getAdmin()->getName()), |
||
| 170 | 'route' => $admin->generateRouteName('delete'), |
||
| 171 | 'parameters' => [ |
||
| 172 | 'id' => false |
||
| 173 | ], |
||
| 174 | 'icon' => 'remove' |
||
| 175 | ] |
||
| 176 | ]; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | // add default menu actions if none was provided |
||
| 181 | if (empty($configuration['actions'])) { |
||
| 182 | // by default, in list action we add a create linked action |
||
| 183 | if ($event->getActionName() == 'list') { |
||
| 184 | if (in_array('create', $allowedActions)) { |
||
| 185 | $configuration['actions']['create'] = [ |
||
| 186 | 'title' => $this->applicationConfiguration->getTranslationKey('create', $event->getAdmin()->getName()), |
||
| 187 | 'route' => $admin->generateRouteName('create'), |
||
| 188 | 'icon' => 'pencil' |
||
| 189 | ]; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | // for list action, add the delete batch action by defaut |
||
| 194 | if (empty($configuration['batch'])) { |
||
| 195 | if ($event->getActionName() == 'list') { |
||
| 196 | $configuration['batch'] = [ |
||
| 197 | 'delete' => null |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | // reset action configuration |
||
| 202 | $event->setConfiguration($configuration); |
||
| 203 | } |
||
| 204 | } |
||
| 205 |