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

ResourcePathProcessor   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 13

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 13
dl 0
loc 95
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 0 81 17
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