Code Duplication    Length = 65-65 lines in 2 locations

core/domain/services/graphql/mutators/DatetimeUpdate.php 1 location

@@ 19-83 (lines=65) @@
16
use WPGraphQL\AppContext;
17
use GraphQL\Error\UserError;
18
19
class DatetimeUpdate {
20
21
	/**
22
	 * Defines the mutation data modification closure.
23
	 *
24
     * @param EEM_Datetime $model
25
     * @param Datetime $type
26
	 * @return callable
27
	 */
28
	public static function mutateAndGetPayload(EEM_Datetime $model, Datetime $type)
29
	{
30
		/**
31
		 * Updates an entity.
32
		 *
33
		 * @param array         $input   The input for the mutation
34
		 * @param AppContext    $context The AppContext passed down to all resolvers
35
		 * @param ResolveInfo   $info    The ResolveInfo passed down to all resolvers
36
		 *
37
		 * @throws UserError
38
		 * @throws ReflectionException
39
		 * @throws InvalidArgumentException
40
		 * @throws InvalidInterfaceException
41
		 * @throws InvalidDataTypeException
42
		 * @throws EE_Error
43
		 */
44
		return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type)
45
		{
46
			/**
47
			 * Stop now if a user isn't allowed to create a datetime.
48
			 */
49
			if (! current_user_can('ee_edit_events')) {
50
				// translators: the %1$s is the name of the object being mutated
51
				throw new UserError(sprintf(__('Sorry, you are not allowed to edit %1$s', 'event_espresso' ), $type->name()));
52
			}
53
54
			$id = ! empty($input['id']) ? absint($input['id']) : 0;
55
			$entity = null;
56
57
			if ($id) {
58
				$entity = $model->get_one_by_ID($id);
59
			}
60
61
			/**
62
			 * If there's no existing datetime, throw an exception
63
			 */
64
			if (! $id || ! ($entity instanceof EE_Datetime)) {
65
				// translators: the placeholder is the name of the type being updated
66
				throw new UserError(sprintf(__( 'No %1$s could be found to update', 'event_espresso' ), $type->name()));
67
			}
68
69
			$args = DatetimeMutation::prepare_fields($input);
70
71
			// Update the entity
72
			$result = $entity->save($args);
73
74
			if (empty($result)) {
75
				throw new UserError(__( 'The object failed to update but no error was provided', 'event_espresso'));
76
			}
77
78
			return [
79
				'id' => $id,
80
			];
81
		};
82
	}
83
}
84

core/domain/services/graphql/mutators/TicketUpdate.php 1 location

@@ 19-83 (lines=65) @@
16
use WPGraphQL\AppContext;
17
use GraphQL\Error\UserError;
18
19
class TicketUpdate {
20
21
	/**
22
	 * Defines the mutation data modification closure.
23
	 *
24
     * @param EEM_Ticket $model
25
     * @param Ticket $type
26
	 * @return callable
27
	 */
28
	public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type)
29
	{
30
		/**
31
		 * Updates an entity.
32
		 *
33
		 * @param array         $input   The input for the mutation
34
		 * @param AppContext    $context The AppContext passed down to all resolvers
35
		 * @param ResolveInfo   $info    The ResolveInfo passed down to all resolvers
36
		 *
37
		 * @throws UserError
38
		 * @throws ReflectionException
39
		 * @throws InvalidArgumentException
40
		 * @throws InvalidInterfaceException
41
		 * @throws InvalidDataTypeException
42
		 * @throws EE_Error
43
		 */
44
		return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type)
45
		{
46
			/**
47
			 * Stop now if a user isn't allowed to create a datetime.
48
			 */
49
			if (! current_user_can('ee_edit_events')) {
50
				// translators: the %1$s is the name of the object being mutated
51
				throw new UserError(sprintf(__('Sorry, you are not allowed to edit %1$s', 'event_espresso' ), $type->name()));
52
			}
53
54
			$id = ! empty($input['id']) ? absint($input['id']) : 0;
55
			$entity = null;
56
57
			if ($id) {
58
				$entity = $model->get_one_by_ID($id);
59
			}
60
61
			/**
62
			 * If there's no existing datetime, throw an exception
63
			 */
64
			if (! $id || ! ($entity instanceof EE_Ticket)) {
65
				// translators: the placeholder is the name of the type being updated
66
				throw new UserError(sprintf(__( 'No %1$s could be found to update', 'event_espresso' ), $type->name()));
67
			}
68
69
			$args = TicketMutation::prepare_fields($input);
70
71
			// Update the entity
72
			$result = $entity->save($args);
73
74
			if (empty($result)) {
75
				throw new UserError(__( 'The object failed to update but no error was provided', 'event_espresso'));
76
			}
77
78
			return [
79
				'id' => $id,
80
			];
81
		};
82
	}
83
}
84