Completed
Branch EDTR/master (5c5ec9)
by
unknown
18:46 queued 09:35
created

PriceDelete::mutateAndGetPayload()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 32

Duplication

Lines 32
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 2
dl 32
loc 32
rs 9.408
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\mutators;
4
5
use EE_Price;
6
use EEM_Price;
7
use EventEspresso\core\domain\services\graphql\types\Price;
8
use Exception;
9
use GraphQL\Type\Definition\ResolveInfo;
10
use WPGraphQL\AppContext;
11
12 View Code Duplication
class PriceDelete extends EntityMutator
13
{
14
15
    /**
16
     * Defines the mutation data modification closure.
17
     *
18
     * @param EEM_Price $model
19
     * @param Price     $type
20
     * @return callable
21
     */
22
    public static function mutateAndGetPayload(EEM_Price $model, Price $type)
23
    {
24
        /**
25
         * Deletes an entity.
26
         *
27
         * @param array       $input   The input for the mutation
28
         * @param AppContext  $context The AppContext passed down to all resolvers
29
         * @param ResolveInfo $info    The ResolveInfo passed down to all resolvers
30
         * @return array
31
         */
32
        return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) {
33
            try {
34
                /** @var EE_Price $entity */
35
                $entity = EntityMutator::getEntityFromInputData($model, $input);
36
                // Delete the entity
37
                $result = ! empty($input['deletePermanently']) ? $entity->delete_permanently() : $entity->delete();
38
                EntityMutator::validateResults($result);
39
            } catch (Exception $exception) {
40
                EntityMutator::handleExceptions(
41
                    $exception,
42
                    esc_html__(
43
                        'The price could not be deleted because of the following error(s)',
44
                        'event_espresso'
45
                    )
46
                );
47
            }
48
49
            return [
50
                'deleted' => $entity,
51
            ];
52
        };
53
    }
54
}
55