Conditions | 12 |
Paths | 1 |
Total Lines | 56 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
68 | public function __invoke(string $resourceClass = null, string $rootClass = null, string $operationName = null): callable |
||
69 | { |
||
70 | return function ($root, $args, $context, ResolveInfo $info) use ($resourceClass, $operationName) { |
||
71 | if (null === $resourceClass) { |
||
72 | return null; |
||
73 | } |
||
74 | |||
75 | $data = ['clientMutationId' => $args['input']['clientMutationId'] ?? null]; |
||
76 | $item = null; |
||
77 | |||
78 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
79 | $normalizationContext = $resourceMetadata->getGraphqlAttribute($operationName ?? '', 'normalization_context', [], true); |
||
80 | $normalizationContext['attributes'] = $this->fieldsToAttributes($info); |
||
81 | |||
82 | if (isset($args['input']['id'])) { |
||
83 | try { |
||
84 | $item = $this->iriConverter->getItemFromIri($args['input']['id'], $normalizationContext); |
||
85 | } catch (ItemNotFoundException $e) { |
||
86 | throw Error::createLocatedError(sprintf('Item "%s" not found.', $args['input']['id']), $info->fieldNodes, $info->path); |
||
87 | } |
||
88 | |||
89 | if ($resourceClass !== $this->getObjectClass($item)) { |
||
90 | throw Error::createLocatedError(sprintf('Item "%s" did not match expected type "%s".', $args['input']['id'], $resourceClass), $info->fieldNodes, $info->path); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
95 | $this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item, $operationName); |
||
96 | if (false === $resourceClass = $resourceMetadata->getAttribute('input_class', $resourceClass)) { |
||
97 | return null; |
||
98 | } |
||
99 | |||
100 | switch ($operationName) { |
||
101 | case 'create': |
||
102 | case 'update': |
||
103 | $context = null === $item ? ['resource_class' => $resourceClass] : ['resource_class' => $resourceClass, 'object_to_populate' => $item]; |
||
104 | $context += $resourceMetadata->getGraphqlAttribute($operationName, 'denormalization_context', [], true); |
||
105 | $item = $this->normalizer->denormalize($args['input'], $resourceClass, ItemNormalizer::FORMAT, $context); |
||
106 | $this->validate($item, $info, $resourceMetadata, $operationName); |
||
107 | $persistResult = $this->dataPersister->persist($item); |
||
108 | |||
109 | if (null === $persistResult) { |
||
110 | @trigger_error(sprintf('Returning void from %s::persist() is deprecated since API Platform 2.3 and will not be supported in API Platform 3, an object should always be returned.', DataPersisterInterface::class), E_USER_DEPRECATED); |
||
111 | } |
||
112 | |||
113 | return $this->normalizer->normalize($persistResult ?? $item, ItemNormalizer::FORMAT, $normalizationContext) + $data; |
||
114 | case 'delete': |
||
115 | if ($item) { |
||
116 | $this->dataPersister->remove($item); |
||
117 | $data['id'] = $args['input']['id']; |
||
118 | } else { |
||
119 | $data['id'] = null; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return $data; |
||
124 | }; |
||
146 |