Completed
Branch EDTR/master (5278ab)
by
unknown
22:28 queued 11:14
created

DatetimeDelete::mutateAndGetPayload()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 2
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\mutators;
4
5
use EE_Datetime;
6
use EEM_Datetime;
7
use EE_Error;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use ReflectionException;
12
use Exception;
13
use EventEspresso\core\domain\services\graphql\types\Datetime;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
17
class DatetimeDelete extends EntityMutator
18
{
19
20
    /**
21
     * Defines the mutation data modification closure.
22
     *
23
     * @param EEM_Datetime $model
24
     * @param Datetime     $type
25
     * @return callable
26
     */
27
    public static function mutateAndGetPayload(EEM_Datetime $model, Datetime $type)
28
    {
29
        /**
30
         * Deletes an entity.
31
         *
32
         * @param array       $input   The input for the mutation
33
         * @param AppContext  $context The AppContext passed down to all resolvers
34
         * @param ResolveInfo $info    The ResolveInfo passed down to all resolvers
35
         * @return array|void
36
         */
37
        return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) {
38
            try {
39
                /** @var EE_Datetime $entity */
40
                $entity = EntityMutator::getEntityFromInputData($model, $input);
41
42
                // Delete the entity
43
                if (! empty($input['deletePermanently'])) {
44
                    $result = DatetimeDelete::deleteDatetimeAndRelations($entity);
45
                } else {
46
                    $result = DatetimeDelete::trashDatetimeAndRelations($entity);
47
                }
48
                EntityMutator::validateResults($result);
49
            } catch (Exception $exception) {
50
                EntityMutator::handleExceptions(
51
                    $exception,
52
                    esc_html__(
53
                        'The datetime could not be deleted because of the following error(s)',
54
                        'event_espresso'
55
                    )
56
                );
57
            }
58
59
            return [
60
                'deleted' => $entity,
61
            ];
62
        };
63
    }
64
65
    /**
66
     * Deletes a datetime permanently along with its relations.
67
     *
68
     * @param EE_Datetime $entity
69
     * @return bool | int
70
     * @throws ReflectionException
71
     * @throws InvalidArgumentException
72
     * @throws InvalidInterfaceException
73
     * @throws InvalidDataTypeException
74
     * @throws EE_Error
75
     */
76
    public static function deleteDatetimeAndRelations($entity)
77
    {
78
        // all related tickets
79
        $tickets = $entity->tickets();
80
        foreach ($tickets as $ticket) {
81
            // if the ticket is related to only one datetime
82
            if ($ticket->count_related('Datetime') === 1) {
83
                TicketDelete::deleteTicketAndRelations($ticket);
84
            }
85
        }
86
87
        // Remove relations with tickets
88
        $entity->_remove_relations('Ticket');
89
        // Now delete the datetime permanently
90
        $result = $entity->delete_permanently();
91
92
        return $result;
93
    }
94
95
    /**
96
     * Trashes a datetime along with its lone relations.
97
     *
98
     * @param EE_Datetime $entity
99
     * @return bool | int
100
     * @throws ReflectionException
101
     * @throws InvalidArgumentException
102
     * @throws InvalidInterfaceException
103
     * @throws InvalidDataTypeException
104
     * @throws EE_Error
105
     */
106
    public static function trashDatetimeAndRelations($entity)
107
    {
108
        // non trashed related tickets
109
        $tickets = $entity->tickets([[
110
            'TKT_deleted' => false,
111
        ]]);
112
        // loop though all tickets to check if we need to trash any
113
        foreach ($tickets as $ticket) {
114
            // if the ticket is related to only one datetime
115
            if ($ticket->count_related('Datetime') === 1) {
116
                // trash the ticket
117
                $ticket->delete();
118
            }
119
        }
120
        // trash the datetime
121
        $result = $entity->delete();
122
123
        return $result;
124
    }
125
}
126