| Total Complexity | 43 |
| Total Lines | 389 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 35 | class ExpandProjectionParser |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * The wrapper of IMetadataProvider and IQueryProvider |
||
| 39 | * . |
||
| 40 | * |
||
| 41 | * @var ProvidersWrapper |
||
| 42 | */ |
||
| 43 | private $_providerWrapper; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Holds reference to the root of 'Projection Tree' |
||
| 47 | * |
||
| 48 | * @var RootProjectionNode |
||
| 49 | */ |
||
| 50 | private $_rootProjectionNode; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates new instance of ExpandProjectionParser |
||
| 54 | * |
||
| 55 | * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper |
||
| 56 | * . |
||
| 57 | */ |
||
| 58 | private function __construct(ProvidersWrapper $providerWrapper) |
||
| 59 | { |
||
| 60 | $this->_providerWrapper = $providerWrapper; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Parse the given expand and select clause, validate them |
||
| 65 | * and build 'Projection Tree' |
||
| 66 | * |
||
| 67 | * @param ResourceSetWrapper $resourceSetWrapper The resource set identified by the resource path uri. |
||
| 68 | * @param ResourceType $resourceType The resource type of entities identified by the resource path uri. |
||
| 69 | * @param InternalOrderByInfo $internalOrderInfo The top level sort information, this will be set if the $skip, $top is |
||
| 70 | * specified in the |
||
| 71 | * request uri or Server |
||
| 72 | * side paging is |
||
| 73 | * enabled for top level |
||
| 74 | * resource |
||
| 75 | * @param int $skipCount The value of $skip option applied to the top level resource |
||
| 76 | * set identified by the |
||
| 77 | * resource path uri |
||
| 78 | * null means $skip |
||
| 79 | * option is not present. |
||
| 80 | * @param int $takeCount The minimum among the value of $top option applied to and |
||
| 81 | * page size configured |
||
| 82 | * for the top level |
||
| 83 | * resource |
||
| 84 | * set identified |
||
| 85 | * by the resource |
||
| 86 | * path uri. |
||
| 87 | * null means $top option |
||
| 88 | * is not present and/or |
||
| 89 | * page size is not |
||
| 90 | * configured for top |
||
| 91 | * level resource set. |
||
| 92 | * @param string $expand The value of $expand clause |
||
| 93 | * @param string $select The value of $select clause |
||
| 94 | * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper |
||
| 95 | * |
||
| 96 | * @return RootProjectionNode Returns root of the 'Projection Tree' |
||
| 97 | * |
||
| 98 | * @throws ODataException If any error occur while parsing expand and/or select clause |
||
| 99 | * |
||
| 100 | */ |
||
| 101 | public static function parseExpandAndSelectClause( |
||
| 102 | ResourceSetWrapper $resourceSetWrapper, |
||
| 103 | ResourceType $resourceType, |
||
| 104 | $internalOrderInfo, |
||
| 105 | $skipCount, |
||
| 106 | $takeCount, |
||
| 107 | $expand, |
||
| 108 | $select, |
||
| 109 | ProvidersWrapper $providerWrapper |
||
| 110 | ) { |
||
| 111 | $parser = new ExpandProjectionParser($providerWrapper); |
||
| 112 | $parser->_rootProjectionNode = new RootProjectionNode( |
||
| 113 | $resourceSetWrapper, |
||
| 114 | $internalOrderInfo, |
||
| 115 | $skipCount, |
||
| 116 | $takeCount, |
||
| 117 | null, |
||
| 118 | $resourceType |
||
| 119 | ); |
||
| 120 | $parser->_parseExpand($expand); |
||
| 121 | $parser->_parseSelect($select); |
||
| 122 | return $parser->_rootProjectionNode; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Read the given expand clause and build 'Projection Tree', |
||
| 127 | * do nothing if the clause is null |
||
| 128 | * |
||
| 129 | * @param string $expand Value of $expand clause. |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | * |
||
| 133 | * @throws ODataException If any error occurs while reading expand clause |
||
| 134 | * or building the projection tree |
||
| 135 | */ |
||
| 136 | private function _parseExpand($expand) |
||
| 137 | { |
||
| 138 | if (!is_null($expand)) { |
||
|
|
|||
| 139 | $pathSegments = $this->_readExpandOrSelect($expand, false); |
||
| 140 | $this->_buildProjectionTree($pathSegments); |
||
| 141 | $this->_rootProjectionNode->setExpansionSpecified(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Read the given select clause and apply selection to the |
||
| 147 | * 'Projection Tree', mark the entire tree as selected if this |
||
| 148 | * clause is null |
||
| 149 | * Note: _parseExpand should to be called before the invocation |
||
| 150 | * of this function so that basic 'Projection Tree' with expand |
||
| 151 | * information will be ready. |
||
| 152 | * |
||
| 153 | * @param string $select Value of $select clause. |
||
| 154 | * |
||
| 155 | * |
||
| 156 | * @throws ODataException If any error occurs while reading expand clause |
||
| 157 | * or applying selection to projection tree |
||
| 158 | */ |
||
| 159 | private function _parseSelect($select) |
||
| 160 | { |
||
| 161 | if (is_null($select)) { |
||
| 162 | $this->_rootProjectionNode->markSubtreeAsSelected(); |
||
| 163 | } else { |
||
| 164 | $pathSegments = $this->_readExpandOrSelect($select, true); |
||
| 165 | $this->_applySelectionToProjectionTree($pathSegments); |
||
| 166 | $this->_rootProjectionNode->setSelectionSpecified(); |
||
| 167 | $this->_rootProjectionNode->removeNonSelectedNodes(); |
||
| 168 | $this->_rootProjectionNode->removeNodesAlreadyIncludedImplicitly(); |
||
| 169 | //TODO: Move sort to parseExpandAndSelectClause function |
||
| 170 | $this->_rootProjectionNode->sortNodes(); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Build 'Projection Tree' from the given expand path segments |
||
| 176 | * |
||
| 177 | * @param array(array(string)) $expandPathSegments Collection of expand paths. |
||
| 178 | * |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | * |
||
| 182 | * @throws ODataException If any error occurs while processing the expand path segments |
||
| 183 | * . |
||
| 184 | */ |
||
| 185 | private function _buildProjectionTree($expandPathSegments) |
||
| 186 | { |
||
| 187 | foreach ($expandPathSegments as $expandSubPathSegments) { |
||
| 188 | $currentNode = $this->_rootProjectionNode; |
||
| 189 | foreach ($expandSubPathSegments as $expandSubPathSegment) { |
||
| 190 | $resourceSetWrapper = $currentNode->getResourceSetWrapper(); |
||
| 191 | $resourceType = $currentNode->getResourceType(); |
||
| 192 | $resourceProperty |
||
| 193 | = $resourceType->resolveProperty( |
||
| 194 | $expandSubPathSegment |
||
| 195 | ); |
||
| 196 | if (is_null($resourceProperty)) { |
||
| 197 | throw ODataException::createSyntaxError( |
||
| 198 | Messages::expandProjectionParserPropertyNotFound( |
||
| 199 | $resourceType->getFullName(), |
||
| 200 | $expandSubPathSegment, |
||
| 201 | false |
||
| 202 | ) |
||
| 203 | ); |
||
| 204 | } else if ($resourceProperty->getTypeKind() != ResourceTypeKind::ENTITY) { |
||
| 205 | throw ODataException::createBadRequestError( |
||
| 206 | Messages::expandProjectionParserExpandCanOnlyAppliedToEntity( |
||
| 207 | $resourceType->getFullName(), |
||
| 208 | $expandSubPathSegment |
||
| 209 | ) |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | $resourceSetWrapper = $this->_providerWrapper |
||
| 214 | ->getResourceSetWrapperForNavigationProperty( |
||
| 215 | $resourceSetWrapper, |
||
| 216 | $resourceType, |
||
| 217 | $resourceProperty |
||
| 218 | ); |
||
| 219 | if (is_null($resourceSetWrapper)) { |
||
| 220 | throw ODataException::createBadRequestError( |
||
| 221 | Messages::badRequestInvalidPropertyNameSpecified( |
||
| 222 | $resourceType->getFullName(), |
||
| 223 | $expandSubPathSegment |
||
| 224 | ) |
||
| 225 | ); |
||
| 226 | } |
||
| 227 | |||
| 228 | $singleResult |
||
| 229 | = $resourceProperty->isKindOf( |
||
| 230 | ResourcePropertyKind::RESOURCE_REFERENCE |
||
| 231 | ); |
||
| 232 | $resourceSetWrapper->checkResourceSetRightsForRead($singleResult); |
||
| 233 | $pageSize = $resourceSetWrapper->getResourceSetPageSize(); |
||
| 234 | $internalOrderByInfo = null; |
||
| 235 | if ($pageSize != 0 && !$singleResult) { |
||
| 236 | $this->_rootProjectionNode->setPagedExpandedResult(true); |
||
| 237 | $rt = $resourceSetWrapper->getResourceType(); |
||
| 238 | //assert($rt != null) |
||
| 239 | $keys = array_keys($rt->getKeyProperties()); |
||
| 240 | //assert(!empty($keys)) |
||
| 241 | $orderBy = null; |
||
| 242 | foreach ($keys as $key) { |
||
| 243 | $orderBy = $orderBy . $key . ', '; |
||
| 244 | } |
||
| 245 | |||
| 246 | $orderBy = rtrim($orderBy, ', '); |
||
| 247 | $internalOrderByInfo = OrderByParser::parseOrderByClause( |
||
| 248 | $resourceSetWrapper, |
||
| 249 | $rt, |
||
| 250 | $orderBy, |
||
| 251 | $this->_providerWrapper |
||
| 252 | ); |
||
| 253 | |||
| 254 | } |
||
| 255 | |||
| 256 | $node = $currentNode->findNode($expandSubPathSegment); |
||
| 257 | if (is_null($node)) { |
||
| 258 | $maxResultCount = $this->_providerWrapper |
||
| 259 | ->getConfiguration()->getMaxResultsPerCollection(); |
||
| 260 | $node = new ExpandedProjectionNode( |
||
| 261 | $expandSubPathSegment, |
||
| 262 | $resourceProperty, |
||
| 263 | $resourceSetWrapper, |
||
| 264 | $internalOrderByInfo, |
||
| 265 | null, |
||
| 266 | $pageSize == 0 ? null : $pageSize, |
||
| 267 | $maxResultCount == PHP_INT_MAX ? null : $maxResultCount |
||
| 268 | ); |
||
| 269 | $currentNode->addNode($node); |
||
| 270 | } |
||
| 271 | |||
| 272 | $currentNode = $node; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Modify the 'Projection Tree' to include selection details |
||
| 279 | * |
||
| 280 | * @param array(array(string)) $selectPathSegments Collection of select |
||
| 281 | * paths. |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | * |
||
| 285 | * @throws ODataException If any error occurs while processing select |
||
| 286 | * path segments |
||
| 287 | */ |
||
| 288 | private function _applySelectionToProjectionTree($selectPathSegments) |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Read expand or select clause. |
||
| 382 | * |
||
| 383 | * @param string $value expand or select clause to read. |
||
| 384 | * @param boolean $isSelect true means $value is value of select clause |
||
| 385 | * else value of expand clause. |
||
| 386 | * |
||
| 387 | * @return array(array) An array of 'PathSegment's, each of which is array |
||
| 388 | * of 'SubPathSegment's |
||
| 389 | */ |
||
| 390 | private function _readExpandOrSelect($value, $isSelect) |
||
| 424 | } |
||
| 425 | } |