| Conditions | 8 |
| Paths | 1 |
| Total Lines | 65 |
| 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 |
||
| 25 | public static function mutateAndGetPayload() |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Updates an entity. |
||
| 29 | * |
||
| 30 | * @param array $input The input for the mutation |
||
| 31 | * @param AppContext $context The AppContext passed down to all resolvers |
||
| 32 | * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
||
| 33 | * @return array |
||
| 34 | * @throws UserError |
||
| 35 | * @throws ReflectionException |
||
| 36 | * @throws InvalidArgumentException |
||
| 37 | * @throws InvalidInterfaceException |
||
| 38 | * @throws InvalidDataTypeException |
||
| 39 | * @throws EE_Error |
||
| 40 | */ |
||
| 41 | return static function ($input, AppContext $context, ResolveInfo $info) { |
||
| 42 | /** |
||
| 43 | * Stop now if a user isn't allowed to delete. |
||
| 44 | */ |
||
| 45 | if (! current_user_can('ee_delete_events')) { |
||
| 46 | throw new UserError( |
||
| 47 | esc_html__('Sorry, you do not have the required permissions to delete entities', 'event_espresso') |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | $details = EntityReorder::prepareEntityDetailsFromInput($input); |
||
| 52 | |||
| 53 | $deletePermanently = ! empty($input['deletePermanently']); |
||
| 54 | |||
| 55 | $deletionMethod = __NAMESPACE__; |
||
| 56 | // if it's for datetimes. |
||
| 57 | if ($details['entityType'] === EEM_Datetime::instance()->item_name()) { |
||
| 58 | $deletionMethod .= '\DatetimeDelete::' . ($deletePermanently ? 'deleteDatetimeAndRelations' : 'trashDatetimeAndRelations'); |
||
| 59 | } elseif ($details['entityType'] === EEM_Ticket::instance()->item_name()) { |
||
| 60 | $deletionMethod .= '\TicketDelete::' . ($deletePermanently ? 'deleteTicketAndRelations' : 'trashTicket'); |
||
| 61 | } else { |
||
| 62 | throw new UserError( |
||
| 63 | esc_html__( |
||
| 64 | 'A valid data model could not be obtained. Did you supply a valid entity type?', |
||
| 65 | 'event_espresso' |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | $deleted = []; |
||
| 71 | $failed = []; |
||
| 72 | |||
| 73 | foreach ($details['entityDbids'] as $key => $entityDbid) { |
||
| 74 | $guid = $details['entityGuids'][ $key ]; |
||
| 75 | $entity = $details['entities'][ $entityDbid ]; |
||
| 76 | try { |
||
| 77 | $result = $deletionMethod($entity); |
||
| 78 | EntityMutator::validateResults($result); |
||
| 79 | // we are here it means the deletion was successful. |
||
| 80 | $deleted[] = $guid; |
||
| 81 | } catch (Exception $e) { |
||
| 82 | // sorry mate, couldn't help you :( |
||
| 83 | $failed[] = $guid; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return compact('deleted', 'failed'); |
||
| 88 | }; |
||
| 89 | } |
||
| 90 | } |
||
| 91 |