Completed
Branch FET/reg-form-builder/main (a66e69)
by
unknown
09:49 queued 19s
created

DatetimeCreate::mutateAndGetPayload()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 1
nop 2
dl 0
loc 45
rs 8.8888
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
    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 (array $input, AppContext $context, ResolveInfo $info) use ($model, $type): array {
34
            $id = null;
35
            try {
36
                EntityMutator::checkPermissions($model);
37
38
                $tickets = [];
39
                $args = DatetimeMutation::prepareFields($input);
40
41
                if (isset($args['tickets'])) {
42
                    $tickets = $args['tickets'];
43
                    unset($args['tickets']);
44
                }
45
46
                $entity = EE_Datetime::new_instance($args);
47
                $id = $entity->save();
48
                EntityMutator::validateResults($id);
49
50
                if (! empty($tickets)) {
51
                    DatetimeMutation::setRelatedTickets($entity, $tickets);
52
                }
53
54
                do_action('AHEE__EventEspresso_core_domain_services_graphql_mutators_datetime_create', $entity, $input);
55
            } catch (Exception $exception) {
56
                EntityMutator::handleExceptions(
57
                    $exception,
58
                    esc_html__(
59
                        'The datetime could not be created because of the following error(s)',
60
                        'event_espresso'
61
                    )
62
                );
63
            }
64
65
            return $id ? [ 'id' => $id ] : [];
66
        };
67
    }
68
}
69