| Total Complexity | 46 |
| Total Lines | 388 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ExpandProjectionParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExpandProjectionParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class ExpandProjectionParser |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * The wrapper of IMetadataProvider and IQueryProvider |
||
| 44 | * . |
||
| 45 | * |
||
| 46 | * @var ProvidersWrapper |
||
| 47 | */ |
||
| 48 | private $providerWrapper; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Holds reference to the root of 'Projection Tree'. |
||
| 52 | * |
||
| 53 | * @var RootProjectionNode |
||
| 54 | */ |
||
| 55 | private $rootProjectionNode; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Creates new instance of ExpandProjectionParser. |
||
| 59 | * |
||
| 60 | * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper |
||
| 61 | */ |
||
| 62 | private function __construct(ProvidersWrapper $providerWrapper) |
||
| 63 | { |
||
| 64 | $this->providerWrapper = $providerWrapper; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Parse the given expand and select clause, validate them |
||
| 69 | * and build 'Projection Tree'. |
||
| 70 | * |
||
| 71 | * @param ResourceSetWrapper $resourceSetWrapper The resource set identified by the resource path uri |
||
| 72 | * @param ResourceType $resourceType The resource type of entities identified by the resource path uri |
||
| 73 | * @param InternalOrderByInfo $internalOrderInfo The top level sort information, this will be set if the $skip, |
||
| 74 | * $top is specified in the |
||
| 75 | * request uri or Server side paging is |
||
| 76 | * enabled for top level resource |
||
| 77 | * @param int $skipCount The value of $skip option applied to the top level resource |
||
| 78 | * set identified by the |
||
| 79 | * resource path uri |
||
| 80 | * null means $skip |
||
| 81 | * option is not present |
||
| 82 | * @param int $takeCount The minimum among the value of $top option applied to and |
||
| 83 | * page size configured |
||
| 84 | * for the top level |
||
| 85 | * resource |
||
| 86 | * set identified |
||
| 87 | * by the resource |
||
| 88 | * path uri. |
||
| 89 | * null means $top option |
||
| 90 | * is not present and/or |
||
| 91 | * page size is not |
||
| 92 | * configured for top |
||
| 93 | * level resource set |
||
| 94 | * @param string $expand The value of $expand clause |
||
| 95 | * @param string $select The value of $select clause |
||
| 96 | * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper |
||
| 97 | * |
||
| 98 | * @throws ODataException If any error occur while parsing expand and/or select clause |
||
| 99 | * @throws InvalidOperationException |
||
| 100 | * @throws ReflectionException |
||
| 101 | * @return RootProjectionNode Returns root of the 'Projection Tree' |
||
| 102 | */ |
||
| 103 | public static function parseExpandAndSelectClause( |
||
| 104 | ResourceSetWrapper $resourceSetWrapper, |
||
| 105 | ResourceType $resourceType, |
||
| 106 | ?InternalOrderByInfo $internalOrderInfo, |
||
| 107 | ?int $skipCount, |
||
| 108 | ?int $takeCount, |
||
| 109 | ?string $expand, |
||
| 110 | ?string $select, |
||
| 111 | ProvidersWrapper $providerWrapper |
||
| 112 | ): RootProjectionNode { |
||
| 113 | $parser = new self($providerWrapper); |
||
| 114 | $parser->rootProjectionNode = new RootProjectionNode( |
||
| 115 | $resourceSetWrapper, |
||
| 116 | $internalOrderInfo, |
||
| 117 | $skipCount, |
||
| 118 | $takeCount, |
||
| 119 | null, |
||
| 120 | $resourceType |
||
| 121 | ); |
||
| 122 | $parser->parseExpand($expand); |
||
| 123 | $parser->parseSelect($select); |
||
| 124 | |||
| 125 | return $parser->rootProjectionNode; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Read the given expand clause and build 'Projection Tree', |
||
| 130 | * do nothing if the clause is null. |
||
| 131 | * |
||
| 132 | * @param string $expand Value of $expand clause |
||
| 133 | * |
||
| 134 | * @throws ODataException If any error occurs while reading expand clause |
||
| 135 | * or building the projection tree |
||
| 136 | * @throws InvalidOperationException |
||
| 137 | * @throws ReflectionException |
||
| 138 | */ |
||
| 139 | private function parseExpand(?string $expand): void |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Read expand or select clause. |
||
| 150 | * |
||
| 151 | * @param string $value expand or select clause to read |
||
| 152 | * @param bool $isSelect true means $value is value of select clause |
||
| 153 | * else value of expand clause |
||
| 154 | * |
||
| 155 | * @throws ODataException |
||
| 156 | * @return array<array> An array of 'PathSegment's, each of which is array of 'SubPathSegment's |
||
| 157 | */ |
||
| 158 | private function readExpandOrSelect(string $value, bool $isSelect): array |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Build 'Projection Tree' from the given expand path segments. |
||
| 196 | * |
||
| 197 | * @param array<array> $expandPathSegments Collection of expand paths |
||
| 198 | * |
||
| 199 | * @throws ODataException If any error occurs while processing the expand path segments |
||
| 200 | * @throws InvalidOperationException |
||
| 201 | * @throws ReflectionException |
||
| 202 | */ |
||
| 203 | private function buildProjectionTree(array $expandPathSegments): void |
||
| 204 | { |
||
| 205 | foreach ($expandPathSegments as $expandSubPathSegments) { |
||
| 206 | $currentNode = $this->rootProjectionNode; |
||
| 207 | foreach ($expandSubPathSegments as $expandSubPathSegment) { |
||
| 208 | $resourceSetWrapper = $currentNode->getResourceSetWrapper(); |
||
| 209 | $resourceType = $currentNode->getResourceType(); |
||
| 210 | assert($resourceType instanceof ResourceEntityType); |
||
| 211 | $resourceProperty = $resourceType->resolveProperty($expandSubPathSegment); |
||
| 212 | assert($resourceProperty instanceof ResourceProperty); |
||
| 213 | $keyType = ResourceAssociationSet::keyNameFromTypeAndProperty($resourceType, $resourceProperty); |
||
| 214 | $assoc = $this->getProviderWrapper()->getMetaProvider()->resolveAssociationSet($keyType); |
||
|
|
|||
| 215 | $concreteType = isset($assoc) ? $assoc->getEnd2()->getConcreteType() : $resourceType; |
||
| 216 | if (null === $resourceProperty) { |
||
| 217 | throw ODataException::createSyntaxError( |
||
| 218 | Messages::expandProjectionParserPropertyNotFound( |
||
| 219 | $resourceType->getFullName(), |
||
| 220 | $expandSubPathSegment, |
||
| 221 | false |
||
| 222 | ) |
||
| 223 | ); |
||
| 224 | } elseif ($resourceProperty->getTypeKind() != ResourceTypeKind::ENTITY()) { |
||
| 225 | throw ODataException::createBadRequestError( |
||
| 226 | Messages::expandProjectionParserExpandCanOnlyAppliedToEntity( |
||
| 227 | $resourceType->getFullName(), |
||
| 228 | $expandSubPathSegment |
||
| 229 | ) |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | assert($resourceType instanceof ResourceEntityType); |
||
| 233 | |||
| 234 | $resourceSetWrapper = $this->providerWrapper |
||
| 235 | ->getResourceSetWrapperForNavigationProperty( |
||
| 236 | $resourceSetWrapper, |
||
| 237 | $resourceType, |
||
| 238 | $resourceProperty |
||
| 239 | ); |
||
| 240 | |||
| 241 | if (null === $resourceSetWrapper) { |
||
| 242 | throw ODataException::createBadRequestError( |
||
| 243 | Messages::badRequestInvalidPropertyNameSpecified( |
||
| 244 | $resourceType->getFullName(), |
||
| 245 | $expandSubPathSegment |
||
| 246 | ) |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | $singleResult = $resourceProperty->isKindOf(ResourcePropertyKind::RESOURCE_REFERENCE()); |
||
| 251 | $resourceSetWrapper->checkResourceSetRightsForRead($singleResult); |
||
| 252 | $pageSize = $resourceSetWrapper->getResourceSetPageSize(); |
||
| 253 | $internalOrderByInfo = null; |
||
| 254 | if ($pageSize != 0 && !$singleResult) { |
||
| 255 | $this->rootProjectionNode->setPagedExpandedResult(true); |
||
| 256 | $rt = $resourceSetWrapper->getResourceType(); |
||
| 257 | $payloadType = $rt->isAbstract() ? $concreteType : $rt; |
||
| 258 | |||
| 259 | $keys = array_keys($rt->getKeyProperties()); |
||
| 260 | $orderBy = null; |
||
| 261 | foreach ($keys as $key) { |
||
| 262 | $orderBy = $orderBy . $key . ', '; |
||
| 263 | } |
||
| 264 | |||
| 265 | $orderBy = rtrim($orderBy, ', '); |
||
| 266 | $internalOrderByInfo = OrderByParser::parseOrderByClause( |
||
| 267 | $resourceSetWrapper, |
||
| 268 | $payloadType, |
||
| 269 | $orderBy, |
||
| 270 | $this->providerWrapper |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | |||
| 274 | $node = $currentNode->findNode($expandSubPathSegment); |
||
| 275 | if (null === $node) { |
||
| 276 | $maxResultCount = $this->providerWrapper |
||
| 277 | ->getConfiguration()->getMaxResultsPerCollection(); |
||
| 278 | $node = new ExpandedProjectionNode( |
||
| 279 | $expandSubPathSegment, |
||
| 280 | $resourceSetWrapper, |
||
| 281 | $internalOrderByInfo, |
||
| 282 | null, |
||
| 283 | $pageSize == 0 ? null : $pageSize, |
||
| 284 | $maxResultCount == PHP_INT_MAX ? null : $maxResultCount, |
||
| 285 | $resourceProperty |
||
| 286 | ); |
||
| 287 | $currentNode->addNode($node); |
||
| 288 | } |
||
| 289 | |||
| 290 | $currentNode = $node; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return ProvidersWrapper |
||
| 297 | */ |
||
| 298 | public function getProviderWrapper(): ProvidersWrapper |
||
| 299 | { |
||
| 300 | return $this->providerWrapper; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Read the given select clause and apply selection to the |
||
| 305 | * 'Projection Tree', mark the entire tree as selected if this |
||
| 306 | * clause is null |
||
| 307 | * Note: _parseExpand should to be called before the invocation |
||
| 308 | * of this function so that basic 'Projection Tree' with expand |
||
| 309 | * information will be ready. |
||
| 310 | * |
||
| 311 | * @param string $select Value of $select clause |
||
| 312 | * |
||
| 313 | * @throws ODataException If any error occurs while reading expand clause |
||
| 314 | * or applying selection to projection tree |
||
| 315 | */ |
||
| 316 | private function parseSelect(?string $select): void |
||
| 317 | { |
||
| 318 | if (null === $select) { |
||
| 319 | $this->rootProjectionNode->markSubtreeAsSelected(); |
||
| 320 | } else { |
||
| 321 | $pathSegments = $this->readExpandOrSelect($select, true); |
||
| 322 | $this->applySelectionToProjectionTree($pathSegments); |
||
| 323 | $this->rootProjectionNode->setSelectionSpecified(); |
||
| 324 | $this->rootProjectionNode->removeNonSelectedNodes(); |
||
| 325 | $this->rootProjectionNode->removeNodesAlreadyIncludedImplicitly(); |
||
| 326 | //TODO: Move sort to parseExpandAndSelectClause function |
||
| 327 | $this->rootProjectionNode->sortNodes(); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Modify the 'Projection Tree' to include selection details. |
||
| 333 | * |
||
| 334 | * @param array<array<string>> $selectPathSegments Collection of select |
||
| 335 | * paths |
||
| 336 | * |
||
| 337 | * @throws ODataException If any error occurs while processing select |
||
| 338 | * path segments |
||
| 339 | */ |
||
| 340 | private function applySelectionToProjectionTree(array $selectPathSegments): void |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 433 |