| Conditions | 41 |
| Paths | 54 |
| Total Lines | 189 |
| Code Lines | 129 |
| 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 |
||
| 170 | private function createNextSegment(SegmentDescriptor $previous, $segment, $checkRights) |
||
| 171 | { |
||
| 172 | $previousKind = $previous->getTargetKind(); |
||
| 173 | if ($previousKind == TargetKind::METADATA() |
||
| 174 | || $previousKind == TargetKind::BATCH() |
||
| 175 | || $previousKind == TargetKind::PRIMITIVE_VALUE() |
||
| 176 | || $previousKind == TargetKind::BAG() |
||
| 177 | || $previousKind == TargetKind::MEDIA_RESOURCE() |
||
| 178 | ) { |
||
| 179 | //All these targets are terminal segments, there cannot be anything after them. |
||
| 180 | throw ODataException::resourceNotFoundError( |
||
| 181 | Messages::segmentParserMustBeLeafSegment($previous->getIdentifier()) |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | $identifier = $keyPredicate = null; |
||
| 186 | $this->extractSegmentIdentifierAndKeyPredicate($segment, $identifier, $keyPredicate); |
||
| 187 | $hasPredicate = null !== $keyPredicate; |
||
| 188 | |||
| 189 | $singleton = $this->providerWrapper->resolveSingleton($identifier); |
||
| 190 | if (null !== $singleton) { |
||
| 191 | throw ODataException::createSyntaxError('Singleton must be first element'); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($previousKind == TargetKind::PRIMITIVE()) { |
||
| 195 | if ($identifier !== ODataConstants::URI_VALUE_SEGMENT) { |
||
|
|
|||
| 196 | throw ODataException::resourceNotFoundError( |
||
| 197 | Messages::segmentParserOnlyValueSegmentAllowedAfterPrimitivePropertySegment( |
||
| 198 | $identifier, |
||
| 199 | $previous->getIdentifier() |
||
| 200 | ) |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | $this->assertion(!$hasPredicate); |
||
| 205 | $current = SegmentDescriptor::createFrom($previous); |
||
| 206 | $current->setIdentifier(ODataConstants::URI_VALUE_SEGMENT); |
||
| 207 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); |
||
| 208 | $current->setSingleResult(true); |
||
| 209 | } elseif (null !== $previous->getPrevious() |
||
| 210 | && $previous->getPrevious()->getIdentifier() === ODataConstants::URI_LINK_SEGMENT |
||
| 211 | && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { |
||
| 212 | throw ODataException::createBadRequestError( |
||
| 213 | Messages::segmentParserNoSegmentAllowedAfterPostLinkSegment($identifier) |
||
| 214 | ); |
||
| 215 | } elseif ($previousKind == TargetKind::RESOURCE() |
||
| 216 | && $previous->isSingleResult() |
||
| 217 | && $identifier === ODataConstants::URI_LINK_SEGMENT |
||
| 218 | ) { |
||
| 219 | $this->assertion(!$hasPredicate); |
||
| 220 | $current = SegmentDescriptor::createFrom($previous); |
||
| 221 | $current->setIdentifier(ODataConstants::URI_LINK_SEGMENT); |
||
| 222 | $current->setTargetKind(TargetKind::LINK()); |
||
| 223 | } else { |
||
| 224 | //Do a sanity check here |
||
| 225 | if ($previousKind != TargetKind::COMPLEX_OBJECT() |
||
| 226 | && $previousKind != TargetKind::RESOURCE() |
||
| 227 | && $previousKind != TargetKind::LINK() |
||
| 228 | ) { |
||
| 229 | throw ODataException::createInternalServerError( |
||
| 230 | Messages::segmentParserInconsistentTargetKindState() |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | if (!$previous->isSingleResult() && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { |
||
| 235 | throw ODataException::createBadRequestError( |
||
| 236 | Messages::segmentParserCannotQueryCollection($previous->getIdentifier()) |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | $current = new SegmentDescriptor(); |
||
| 241 | $current->setIdentifier($identifier); |
||
| 242 | $current->setTargetSource(TargetSource::PROPERTY); |
||
| 243 | $previousType = $previous->getTargetResourceType(); |
||
| 244 | $projectedProperty = $previousType->resolveProperty($identifier); |
||
| 245 | $current->setProjectedProperty($projectedProperty); |
||
| 246 | |||
| 247 | if ($identifier === ODataConstants::URI_COUNT_SEGMENT) { |
||
| 248 | if ($previousKind != TargetKind::RESOURCE()) { |
||
| 249 | throw ODataException::createBadRequestError( |
||
| 250 | Messages::segmentParserCountCannotBeApplied($previous->getIdentifier()) |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | if ($previous->isSingleResult()) { |
||
| 255 | throw ODataException::createBadRequestError( |
||
| 256 | Messages::segmentParserCountCannotFollowSingleton($previous->getIdentifier()) |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); |
||
| 261 | $current->setSingleResult(true); |
||
| 262 | $current->setTargetResourceSetWrapper( |
||
| 263 | $previous->getTargetResourceSetWrapper() |
||
| 264 | ); |
||
| 265 | $current->setTargetResourceType( |
||
| 266 | $previous->getTargetResourceType() |
||
| 267 | ); |
||
| 268 | } elseif ($identifier === ODataConstants::URI_VALUE_SEGMENT |
||
| 269 | && $previousKind == TargetKind::RESOURCE() |
||
| 270 | ) { |
||
| 271 | $current->setSingleResult(true); |
||
| 272 | $current->setTargetResourceType( |
||
| 273 | $previous->getTargetResourceType() |
||
| 274 | ); |
||
| 275 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); |
||
| 276 | } elseif (null === $projectedProperty) { |
||
| 277 | if (null !== $previous->getTargetResourceType() |
||
| 278 | && null !== $previous->getTargetResourceType()->tryResolveNamedStreamByName($identifier) |
||
| 279 | ) { |
||
| 280 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); |
||
| 281 | $current->setSingleResult(true); |
||
| 282 | $current->setTargetResourceType( |
||
| 283 | $previous->getTargetResourceType() |
||
| 284 | ); |
||
| 285 | } else { |
||
| 286 | throw ODataException::createResourceNotFoundError($identifier); |
||
| 287 | } |
||
| 288 | } else { |
||
| 289 | $current->setTargetResourceType($projectedProperty->getResourceType()); |
||
| 290 | $current->setSingleResult($projectedProperty->getKind() != ResourcePropertyKind::RESOURCESET_REFERENCE); |
||
| 291 | if ($previousKind == TargetKind::LINK() |
||
| 292 | && $projectedProperty->getTypeKind() != ResourceTypeKind::ENTITY() |
||
| 293 | ) { |
||
| 294 | throw ODataException::createBadRequestError( |
||
| 295 | Messages::segmentParserLinkSegmentMustBeFollowedByEntitySegment( |
||
| 296 | $identifier |
||
| 297 | ) |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | switch ($projectedProperty->getKind()) { |
||
| 302 | case ResourcePropertyKind::COMPLEX_TYPE: |
||
| 303 | $current->setTargetKind(TargetKind::COMPLEX_OBJECT()); |
||
| 304 | break; |
||
| 305 | case ResourcePropertyKind::BAG | ResourcePropertyKind::PRIMITIVE: |
||
| 306 | case ResourcePropertyKind::BAG | ResourcePropertyKind::COMPLEX_TYPE: |
||
| 307 | $current->setTargetKind(TargetKind::BAG()); |
||
| 308 | break; |
||
| 309 | case ResourcePropertyKind::RESOURCE_REFERENCE: |
||
| 310 | case ResourcePropertyKind::RESOURCESET_REFERENCE: |
||
| 311 | $current->setTargetKind(TargetKind::RESOURCE()); |
||
| 312 | $prevResource = $previous->getTargetResourceType(); |
||
| 313 | assert($prevResource instanceof ResourceEntityType); |
||
| 314 | $resourceSetWrapper = $this->providerWrapper->getResourceSetWrapperForNavigationProperty( |
||
| 315 | $previous->getTargetResourceSetWrapper(), |
||
| 316 | $prevResource, |
||
| 317 | $projectedProperty |
||
| 318 | ); |
||
| 319 | if (null === $resourceSetWrapper) { |
||
| 320 | throw ODataException::createResourceNotFoundError($projectedProperty->getName()); |
||
| 321 | } |
||
| 322 | |||
| 323 | $current->setTargetResourceSetWrapper($resourceSetWrapper); |
||
| 324 | break; |
||
| 325 | default: |
||
| 326 | if (!$projectedProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) { |
||
| 327 | throw ODataException::createInternalServerError( |
||
| 328 | Messages::segmentParserUnExpectedPropertyKind('Primitive') |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | |||
| 332 | $current->setTargetKind(TargetKind::PRIMITIVE()); |
||
| 333 | break; |
||
| 334 | } |
||
| 335 | |||
| 336 | if ($hasPredicate) { |
||
| 337 | $this->assertion(!$current->isSingleResult()); |
||
| 338 | $keyDescriptor = $this->createKeyDescriptor( |
||
| 339 | $identifier . '(' . $keyPredicate . ')', |
||
| 340 | $projectedProperty->getResourceType(), |
||
| 341 | $keyPredicate |
||
| 342 | ); |
||
| 343 | $current->setKeyDescriptor($keyDescriptor); |
||
| 344 | if (!$keyDescriptor->isEmpty()) { |
||
| 345 | $current->setSingleResult(true); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | if ($checkRights && null !== $current->getTargetResourceSetWrapper()) { |
||
| 350 | $current->getTargetResourceSetWrapper() |
||
| 351 | ->checkResourceSetRightsForRead( |
||
| 352 | $current->isSingleResult() |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | return $current; |
||
| 359 | } |
||
| 510 |