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

PriceUpdate::mutateAndGetPayload()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 34

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 2
dl 34
loc 34
rs 9.376
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 EventEspresso\core\domain\services\graphql\data\mutations\PriceMutation;
9
use Exception;
10
use GraphQL\Type\Definition\ResolveInfo;
11
use WPGraphQL\AppContext;
12
13 View Code Duplication
class PriceUpdate extends EntityMutator
14
{
15
16
    /**
17
     * Defines the mutation data modification closure.
18
     *
19
     * @param EEM_Price $model
20
     * @param Price     $type
21
     * @return callable
22
     */
23
    public static function mutateAndGetPayload(EEM_Price $model, Price $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_Price $entity */
36
                $entity = EntityMutator::getEntityFromInputData($model, $input);
37
38
                $args = PriceMutation::prepareFields($input);
39
40
                // Update the entity
41
                $entity->save($args);
42
            } catch (Exception $exception) {
43
                EntityMutator::handleExceptions(
44
                    $exception,
45
                    esc_html__(
46
                        'The price could not be updated because of the following error(s)',
47
                        'event_espresso'
48
                    )
49
                );
50
            }
51
52
            return [
53
                'id' => $entity->ID(),
54
            ];
55
        };
56
    }
57
}
58