Conditions | 19 |
Paths | 1 |
Total Lines | 83 |
Code Lines | 54 |
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 |
||
65 | public function __invoke(string $resourceClass = null, string $rootClass = null, string $operationName = null): callable |
||
66 | { |
||
67 | return function ($source, $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operationName) { |
||
68 | if (null === $resourceClass) { |
||
69 | return null; |
||
70 | } |
||
71 | |||
72 | if ($this->requestStack && null !== $request = $this->requestStack->getCurrentRequest()) { |
||
73 | $request->attributes->set( |
||
74 | '_graphql_collections_args', |
||
75 | [$resourceClass => $args] + $request->attributes->get('_graphql_collections_args', []) |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
80 | $dataProviderContext = $resourceMetadata->getGraphqlAttribute($operationName ?? 'query', 'normalization_context', [], true); |
||
81 | $dataProviderContext['attributes'] = $this->fieldsToAttributes($info); |
||
82 | $dataProviderContext['filters'] = $this->getNormalizedFilters($args); |
||
83 | $dataProviderContext['graphql'] = true; |
||
84 | |||
85 | if (isset($rootClass, $source[$rootProperty = $info->fieldName], $source[ItemNormalizer::ITEM_KEY])) { |
||
86 | $rootResolvedFields = $this->identifiersExtractor->getIdentifiersFromItem(unserialize($source[ItemNormalizer::ITEM_KEY])); |
||
87 | $subresource = $this->getSubresource($rootClass, $rootResolvedFields, array_keys($rootResolvedFields), $rootProperty, $resourceClass, true, $dataProviderContext); |
||
88 | $collection = $subresource ?? []; |
||
89 | } else { |
||
90 | $collection = $this->collectionDataProvider->getCollection($resourceClass, null, $dataProviderContext); |
||
91 | } |
||
92 | |||
93 | $this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $collection, $operationName ?? 'query'); |
||
94 | |||
95 | if (!$this->paginationEnabled) { |
||
96 | $data = []; |
||
97 | foreach ($collection as $index => $object) { |
||
98 | $data[$index] = $this->normalizer->normalize($object, ItemNormalizer::FORMAT, $dataProviderContext); |
||
99 | } |
||
100 | |||
101 | return $data; |
||
102 | } |
||
103 | |||
104 | if (!($collection instanceof PaginatorInterface)) { |
||
105 | throw Error::createLocatedError(sprintf('Collection returned by the collection data provider must implement %s', PaginatorInterface::class), $info->fieldNodes, $info->path); |
||
106 | } |
||
107 | |||
108 | $offset = 0; |
||
109 | $totalItems = $collection->getTotalItems(); |
||
110 | $nbPageItems = $collection->count(); |
||
111 | if (isset($args['after'])) { |
||
112 | $after = base64_decode($args['after'], true); |
||
113 | if (false === $after) { |
||
114 | throw Error::createLocatedError(sprintf('Cursor %s is invalid', $args['after']), $info->fieldNodes, $info->path); |
||
115 | } |
||
116 | $offset = 1 + (int) $after; |
||
117 | } |
||
118 | if (isset($args['before'])) { |
||
119 | $before = base64_decode($args['before'], true); |
||
120 | if (false === $before) { |
||
121 | throw Error::createLocatedError(sprintf('Cursor %s is invalid', $args['before']), $info->fieldNodes, $info->path); |
||
122 | } |
||
123 | $offset = (int) $before - $nbPageItems; |
||
124 | } |
||
125 | if (isset($args['last']) && !isset($args['before'])) { |
||
126 | $offset = $totalItems - $args['last']; |
||
127 | } |
||
128 | $offset = 0 > $offset ? 0 : $offset; |
||
129 | |||
130 | $data = ['totalCount' => 0., 'edges' => [], 'pageInfo' => ['startCursor' => null, 'endCursor' => null, 'hasNextPage' => false, 'hasPreviousPage' => false]]; |
||
131 | |||
132 | if ($totalItems > 0) { |
||
133 | $data['totalCount'] = $totalItems; |
||
134 | $data['pageInfo']['startCursor'] = base64_encode((string) $offset); |
||
135 | $data['pageInfo']['endCursor'] = base64_encode((string) ($offset + $nbPageItems - 1)); |
||
136 | $data['pageInfo']['hasNextPage'] = $collection->getCurrentPage() !== $collection->getLastPage() && (float) $nbPageItems === $collection->getItemsPerPage(); |
||
137 | $data['pageInfo']['hasPreviousPage'] = $collection->getCurrentPage() > 1 && (float) $nbPageItems === $collection->getItemsPerPage(); |
||
138 | } |
||
139 | |||
140 | foreach ($collection as $index => $object) { |
||
141 | $data['edges'][$index] = [ |
||
142 | 'node' => $this->normalizer->normalize($object, ItemNormalizer::FORMAT, $dataProviderContext), |
||
143 | 'cursor' => base64_encode((string) ($index + $offset)), |
||
144 | ]; |
||
145 | } |
||
146 | |||
147 | return $data; |
||
148 | }; |
||
196 |