Completed
Branch EDTR/master (333a8d)
by
unknown
18:08 queued 09:50
created

TicketDelete::mutateAndGetPayload()   B

Complexity

Conditions 9
Paths 1

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 1
nop 2
dl 0
loc 63
rs 7.2517
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\mutators;
4
5
use EEM_Ticket;
6
use EE_Ticket;
7
use EventEspresso\core\domain\services\graphql\types\Ticket;
8
9
use EE_Error;
10
use InvalidArgumentException;
11
use ReflectionException;
12
use EventEspresso\core\exceptions\InvalidDataTypeException;
13
use EventEspresso\core\exceptions\InvalidInterfaceException;
14
15
use GraphQL\Type\Definition\ResolveInfo;
16
use WPGraphQL\AppContext;
17
use GraphQL\Error\UserError;
18
use GraphQLRelay\Relay;
19
20
class TicketDelete
21
{
22
23
    /**
24
     * Defines the mutation data modification closure.
25
     *
26
     * @param EEM_Ticket $model
27
     * @param Ticket     $type
28
     * @return callable
29
     */
30
    public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type)
31
    {
32
        /**
33
         * Deletes an entity.
34
         *
35
         * @param array       $input   The input for the mutation
36
         * @param AppContext  $context The AppContext passed down to all resolvers
37
         * @param ResolveInfo $info    The ResolveInfo passed down to all resolvers
38
         * @return array
39
         * @throws UserError
40
         * @throws ReflectionException
41
         * @throws InvalidArgumentException
42
         * @throws InvalidInterfaceException
43
         * @throws InvalidDataTypeException
44
         * @throws EE_Error
45
         */
46
        return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) {
47
            /**
48
             * Stop now if a user isn't allowed to create a datetime.
49
             */
50
            if (! current_user_can('ee_edit_events')) {
51
                // translators: the %1$s is the name of the object being mutated
52
                throw new UserError(
53
                    sprintf(
54
                        __('Sorry, you are not allowed to edit %1$s', 'event_espresso'),
55
                        $type->name()
56
                    )
57
                );
58
            }
59
            $id_parts = ! empty($input['id']) ? Relay::fromGlobalId($input['id']) : null;
60
61
            $id = ! empty($id_parts['id']) ? absint($id_parts['id']) : 0;
62
            $entity = null;
63
64
            if ($id) {
65
                $entity = $model->get_one_by_ID($id);
66
            }
67
68
            /**
69
             * If there's no existing datetime, throw an exception
70
             */
71
            if (! $id || ! ($entity instanceof EE_Ticket)) {
72
                // translators: the placeholder is the name of the type being updated
73
                throw new UserError(
74
                    sprintf(
75
                        __('No %1$s could be found to delete', 'event_espresso'),
76
                        $type->name()
77
                    )
78
                );
79
            }
80
81
            // Delete the entity
82
            $result = ! empty($input['deletePermanently']) ? $entity->delete_permanently() : $entity->delete();
83
84
            if (empty($result)) {
85
                throw new UserError(__('The object failed to delete but no error was provided', 'event_espresso'));
86
            }
87
88
            return [
89
                'deleted' => $entity,
90
            ];
91
        };
92
    }
93
}
94