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

TicketDelete::trashTicket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 5
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\mutators;
4
5
use EE_Ticket;
6
use EEM_Ticket;
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\Ticket;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
17 View Code Duplication
class TicketDelete extends EntityMutator
18
{
19
20
    /**
21
     * Defines the mutation data modification closure.
22
     *
23
     * @param EEM_Ticket $model
24
     * @param Ticket     $type
25
     * @return callable
26
     */
27
    public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $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
36
         */
37
        return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) {
38
            try {
39
                /** @var EE_Ticket $entity */
40
                $entity = EntityMutator::getEntityFromInputData($model, $input);
41
42
                // Delete the entity
43
                if (! empty($input['deletePermanently'])) {
44
                    $result = TicketDelete::deleteTicketAndRelations($entity);
45
                } else {
46
                    // trash the ticket
47
                    $result = TicketDelete::trashTicket($entity);
48
                }
49
                EntityMutator::validateResults($result);
50
            } catch (Exception $exception) {
51
                EntityMutator::handleExceptions(
52
                    $exception,
53
                    esc_html__(
54
                        'The ticket could not be deleted because of the following error(s)',
55
                        'event_espresso'
56
                    )
57
                );
58
            }
59
60
            return [
61
                'deleted' => $entity,
62
            ];
63
        };
64
    }
65
66
    /**
67
     * Deletes a ticket permanently along with its relations.
68
     *
69
     * @param EE_Ticket $entity
70
     * @return bool | int
71
     * @throws ReflectionException
72
     * @throws InvalidArgumentException
73
     * @throws InvalidInterfaceException
74
     * @throws InvalidDataTypeException
75
     * @throws EE_Error
76
     */
77
    public static function deleteTicketAndRelations($entity)
78
    {
79
        // Remove related prices for the ticket
80
        $entity->delete_related_permanently('Price');
81
        // Remove relation with datetimes
82
        $entity->_remove_relations('Datetime');
83
        // Now delete the ticket permanently
84
        $result = $entity->delete_permanently();
85
86
        return $result;
87
    }
88
89
    /**
90
     * Trashes a ticket.
91
     *
92
     * @param EE_Ticket $entity
93
     * @return bool | int
94
     * @throws ReflectionException
95
     * @throws InvalidArgumentException
96
     * @throws InvalidInterfaceException
97
     * @throws InvalidDataTypeException
98
     * @throws EE_Error
99
     */
100
    public static function trashTicket($entity)
101
    {
102
        // trash the ticket
103
        return $entity->delete();
104
    }
105
}
106