Completed
Push — master ( 24bb3c...b333c5 )
by Christopher
04:00
created

ResourcePathProcessor::process()   D

Complexity

Conditions 17
Paths 68

Size

Total Lines 81
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 4.984
c 0
b 0
f 0
cc 17
eloc 51
nc 68
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace POData\UriProcessor\ResourcePathProcessor;
4
5
use POData\Providers\Query\QueryType;
6
use POData\UriProcessor\ResourcePathProcessor\SegmentParser\SegmentParser;
7
use POData\UriProcessor\ResourcePathProcessor\SegmentParser\TargetKind;
8
use POData\UriProcessor\RequestDescription;
9
use POData\IService;
10
use POData\Common\ODataConstants;
11
use POData\Common\Messages;
12
use POData\Common\ODataException;
13
use POData\OperationContext\HTTPRequestMethod;
14
15
/**
16
 * Class ResourcePathProcessor.
17
 */
18
class ResourcePathProcessor
19
{
20
    /**
21
     * Process the request Uri and creates an instance of
22
     * RequestDescription from the processed uri.
23
     *
24
     * @param IService $service Reference to the data service instance
25
     *
26
     * @return RequestDescription
27
     *
28
     * @throws ODataException If any exception occurs while processing the segments
29
     *                        or in case of any version incompatibility
30
     */
31
    public static function process(IService $service)
32
    {
33
        $host = $service->getHost();
34
        $absoluteRequestUri = $host->getAbsoluteRequestUri();
35
        $absoluteServiceUri = $host->getAbsoluteServiceUri();
36
37
        $requestUriSegments = array_slice(
38
            $absoluteRequestUri->getSegments(),
39
            $absoluteServiceUri->getSegmentCount()
40
        );
41
        $segments = SegmentParser::parseRequestUriSegments(
42
            $requestUriSegments,
43
            $service->getProvidersWrapper(),
44
            true
45
        );
46
47
        $dataType = null;
48
        $operationContext = $service->getOperationContext();
49
        if ($operationContext && $operationContext->incomingRequest()->getMethod() != HTTPRequestMethod::GET()) {
50
            $dataType = $service->getHost()->getRequestContentType();
51
        }
52
        $incoming = isset($operationContext) ? $operationContext->incomingRequest() : null;
53
        $request = new RequestDescription(
54
            $segments,
55
            $absoluteRequestUri,
56
            $service->getConfiguration()->getMaxDataServiceVersion(),
57
            $host->getRequestVersion(),
58
            $host->getRequestMaxVersion(),
59
            $dataType,
60
            $incoming
61
        );
62
        $kind = $request->getTargetKind();
63
64
        if ($kind == TargetKind::METADATA()
65
            || $kind == TargetKind::BATCH()
66
            || $kind == TargetKind::SERVICE_DIRECTORY()) {
67
            return $request;
68
        }
69
70
        if ($kind == TargetKind::PRIMITIVE_VALUE() || $kind == TargetKind::MEDIA_RESOURCE()) {
71
            // http://odata/NW.svc/Orders/$count
72
            // http://odata/NW.svc/Orders(123)/Customer/CustomerID/$value
73
            // http://odata/NW.svc/Employees(1)/$value
74
            // http://odata/NW.svc/Employees(1)/ThumbNail_48X48/$value
75
            $request->setContainerName($segments[count($segments) - 2]->getIdentifier());
76
        } else {
77
            $request->setContainerName($request->getIdentifier());
78
        }
79
80
        if ($request->getIdentifier() === ODataConstants::URI_COUNT_SEGMENT) {
81
            if (!$service->getConfiguration()->getAcceptCountRequests()) {
82
                throw ODataException::createBadRequestError(Messages::configurationCountNotAccepted());
83
            }
84
85
            $request->queryType = QueryType::COUNT();
86
            // use of $count requires request DataServiceVersion
87
            // and MaxDataServiceVersion greater than or equal to 2.0
88
89
            $request->raiseResponseVersion(2, 0);
90
            $request->raiseMinVersionRequirement(2, 0);
91
        } elseif ($request->isNamedStream()) {
92
            $request->raiseMinVersionRequirement(3, 0);
93
        } elseif ($request->getTargetKind() == TargetKind::RESOURCE()) {
94
            if (!$request->isLinkUri()) {
95
                $resourceSetWrapper = $request->getTargetResourceSetWrapper();
96
                //assert($resourceSetWrapper != null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
                $hasNamedStream = $resourceSetWrapper->hasNamedStreams($service->getProvidersWrapper());
98
99
                $hasBagProperty = $resourceSetWrapper->hasBagProperty($service->getProvidersWrapper());
100
101
                if ($hasNamedStream || $hasBagProperty) {
102
                    $request->raiseResponseVersion(3, 0);
103
                }
104
            }
105
        } elseif ($request->getTargetKind() == TargetKind::BAG()
106
        ) {
107
            $request->raiseResponseVersion(3, 0);
108
        }
109
110
        return $request;
111
    }
112
}
113