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

DatetimeCreate::mutateAndGetPayload()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 44

Duplication

Lines 44
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 1
nop 2
dl 44
loc 44
rs 9.216
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 EventEspresso\core\domain\services\graphql\types\Datetime;
8
use EventEspresso\core\domain\services\graphql\data\mutations\DatetimeMutation;
9
use Exception;
10
use GraphQL\Type\Definition\ResolveInfo;
11
use WPGraphQL\AppContext;
12
13
class DatetimeCreate extends EntityMutator
14
{
15
16
    /**
17
     * Defines the mutation data modification closure.
18
     *
19
     * @param EEM_Datetime $model
20
     * @param Datetime     $type
21
     * @return callable
22
     */
23 View Code Duplication
    public static function mutateAndGetPayload(EEM_Datetime $model, Datetime $type)
24
    {
25
        /**
26
         * Creates 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
                EntityMutator::checkPermissions($model);
36
37
                $tickets = [];
38
                $args = DatetimeMutation::prepareFields($input);
39
40
                if (isset($args['tickets'])) {
41
                    $tickets = $args['tickets'];
42
                    unset($args['tickets']);
43
                }
44
45
                $entity = EE_Datetime::new_instance($args);
46
                $id = $entity->save();
47
                EntityMutator::validateResults($id);
48
49
                if (! empty($tickets)) {
50
                    DatetimeMutation::setRelatedTickets($entity, $tickets);
51
                }
52
            } catch (Exception $exception) {
53
                EntityMutator::handleExceptions(
54
                    $exception,
55
                    esc_html__(
56
                        'The datetime could not be created because of the following error(s)',
57
                        'event_espresso'
58
                    )
59
                );
60
            }
61
62
            return [
63
                'id' => $id,
64
            ];
65
        };
66
    }
67
}
68