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

TicketUpdate::mutateAndGetPayload()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 1
nop 2
dl 0
loc 52
rs 8.425
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 EE_Ticket;
6
use EEM_Ticket;
7
use EventEspresso\core\domain\services\graphql\types\Ticket;
8
use EventEspresso\core\domain\services\graphql\data\mutations\TicketMutation;
9
use Exception;
10
use GraphQL\Type\Definition\ResolveInfo;
11
use WPGraphQL\AppContext;
12
13
class TicketUpdate extends EntityMutator
14
{
15
16
    /**
17
     * Defines the mutation data modification closure.
18
     *
19
     * @param EEM_Ticket $model
20
     * @param Ticket     $type
21
     * @return callable
22
     */
23
    public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type)
24
    {
25
        /**
26
         * Updates an entity.
27
         *
28
         * @param array       $input   The input for the mutation
29
         * @param AppContext  $context The AppContext passed down to all resolvers
30
         * @param ResolveInfo $info    The ResolveInfo passed down to all resolvers
31
         * @return array
32
         */
33
        return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) {
34
            try {
35
                /** @var EE_Ticket $entity */
36
                $entity = EntityMutator::getEntityFromInputData($model, $input);
37
38
                $datetimes = [];
39
                $prices = [];
40
41
                $args = TicketMutation::prepareFields($input);
42
43
                if (isset($args['datetimes'])) {
44
                    $datetimes = $args['datetimes'];
45
                    unset($args['datetimes']);
46
                }
47
                if (isset($args['prices'])) {
48
                    $prices = $args['prices'];
49
                    unset($args['prices']);
50
                }
51
52
                $entity->save($args);
53
54
                if (! empty($datetimes)) {
55
                    TicketMutation::setRelatedDatetimes($entity, $datetimes);
56
                }
57
                if (! empty($prices)) {
58
                    TicketMutation::setRelatedPrices($entity, $prices);
59
                }
60
            } catch (Exception $exception) {
61
                EntityMutator::handleExceptions(
62
                    $exception,
63
                    esc_html__(
64
                        'The ticket could not be deleted because of the following error(s)',
65
                        'event_espresso'
66
                    )
67
                );
68
            }
69
70
            return [
71
                'id' => $entity->ID(),
72
            ];
73
        };
74
    }
75
}
76