Completed
Branch deps-array-update (1dbcd6)
by
unknown
52:00 queued 42:11
created

BulkEntityDelete::mutateAndGetPayload()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 1
nop 0
dl 0
loc 65
rs 7.5191
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_Datetime;
6
use EEM_Ticket;
7
use EE_Error;
8
use Exception;
9
use InvalidArgumentException;
10
use ReflectionException;
11
use EventEspresso\core\exceptions\InvalidDataTypeException;
12
use EventEspresso\core\exceptions\InvalidInterfaceException;
13
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
use GraphQL\Error\UserError;
17
18
class BulkEntityDelete extends EntityMutator
19
{
20
    /**
21
     * Defines the mutation data modification closure.
22
     *
23
     * @return callable
24
     */
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