Conditions | 11 |
Paths | 1 |
Total Lines | 70 |
Lines | 70 |
Ratio | 100 % |
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 |
||
31 | public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type) |
||
32 | { |
||
33 | /** |
||
34 | * Updates an entity. |
||
35 | * |
||
36 | * @param array $input The input for the mutation |
||
37 | * @param AppContext $context The AppContext passed down to all resolvers |
||
38 | * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
||
39 | * @return array |
||
40 | * @throws UserError |
||
41 | * @throws ReflectionException |
||
42 | * @throws InvalidArgumentException |
||
43 | * @throws InvalidInterfaceException |
||
44 | * @throws InvalidDataTypeException |
||
45 | * @throws EE_Error |
||
46 | */ |
||
47 | return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) { |
||
48 | /** |
||
49 | * Stop now if a user isn't allowed to create a datetime. |
||
50 | */ |
||
51 | if (! current_user_can('ee_edit_events')) { |
||
52 | // translators: the %1$s is the name of the object being mutated |
||
53 | throw new UserError( |
||
54 | sprintf(__('Sorry, you are not allowed to edit %1$s', 'event_espresso'), $type->name()) |
||
55 | ); |
||
56 | } |
||
57 | $id_parts = ! empty($input['id']) ? Relay::fromGlobalId($input['id']) : null; |
||
58 | |||
59 | $id = ! empty($id_parts['id']) && is_int($id_parts['id']) ? $id_parts['id'] : 0; |
||
60 | $entity = null; |
||
61 | |||
62 | if ($id) { |
||
63 | $entity = $model->get_one_by_ID($id); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * If there's no existing datetime, throw an exception |
||
68 | */ |
||
69 | if (! $id || ! ($entity instanceof EE_Ticket)) { |
||
70 | // translators: the placeholder is the name of the type being updated |
||
71 | throw new UserError( |
||
72 | sprintf(__('No %1$s could be found to update', 'event_espresso'), $type->name()) |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | $datetimes = []; |
||
77 | |||
78 | $args = TicketMutation::prepareFields($input); |
||
79 | |||
80 | if (isset($args['datetimes'])) { |
||
81 | $datetimes = $args['datetimes']; |
||
82 | unset($args['datetimes']); |
||
83 | } |
||
84 | |||
85 | // Update the entity |
||
86 | $result = $entity->save($args); |
||
87 | |||
88 | if (empty($result)) { |
||
89 | throw new UserError(__('The object failed to update but no error was provided', 'event_espresso')); |
||
90 | } |
||
91 | |||
92 | if (! empty($datetimes)) { |
||
93 | TicketMutation::setRelatedDatetimes($entity, $datetimes); |
||
94 | } |
||
95 | |||
96 | return [ |
||
97 | 'id' => $id, |
||
98 | ]; |
||
99 | }; |
||
100 | } |
||
101 | } |
||
102 |