Completed
Push — master ( f97b98...ddac18 )
by Alex
01:00
created

SegmentStack   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 116
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A pushSegment() 0 13 3
A popSegment() 0 15 3
A getSegmentNames() 0 4 1
A getSegmentWrappers() 0 4 1
A getRequest() 0 4 1
1
<?php
2
3
namespace POData\UriProcessor;
4
5
use POData\Common\InvalidOperationException;
6
use POData\Providers\Metadata\ResourceSetWrapper;
7
8
class SegmentStack
9
{
10
    /**
11
     * Collection of segment names.
12
     *
13
     * @var string[]
14
     */
15
    private $segmentNames;
16
17
    /**
18
     * Collection of segment ResourceSetWrapper instances.
19
     *
20
     * @var ResourceSetWrapper[]
21
     */
22
    private $segmentResourceSetWrappers;
23
24
    /**
25
     * Description of the OData request that a client has submitted.
26
     *
27
     * @var RequestDescription
28
     */
29
    private $request;
30
31
    private $mismatch = "Mismatch between size of names array and wrappers array";
32
33
    public function __construct(RequestDescription $request)
34
    {
35
        $this->request = $request;
36
        $this->segmentNames = [];
37
        $this->segmentResourceSetWrappers = [];
38
        assert(count($this->segmentNames) == count($this->segmentResourceSetWrappers), $this->mismatch);
39
    }
40
41
42
    /**
43
     * Pushes information about the segment whose instance is going to be
44
     * retrieved from the IDSQP implementation
45
     * Note: Calls to this method should be balanced with calls to popSegment.
46
     *
47
     * @param string             $segmentName         Name of segment to push
48
     * @param ResourceSetWrapper &$resourceSetWrapper The resource set wrapper
49
     *                                                to push
50
     *
51
     * @return bool true if the segment was push, false otherwise
52
     */
53
    public function pushSegment($segmentName, ResourceSetWrapper &$resourceSetWrapper)
54
    {
55
        $rootProjectionNode = $this->getRequest()->getRootProjectionNode();
56
        if (!is_null($rootProjectionNode) && $rootProjectionNode->isExpansionSpecified()) {
57
            array_push($this->segmentNames, $segmentName);
58
            array_push($this->segmentResourceSetWrappers, $resourceSetWrapper);
59
            assert(count($this->segmentNames) == count($this->segmentResourceSetWrappers), $this->mismatch);
60
61
            return true;
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Pops segment information from the 'Segment Stack'
69
     * Note: Calls to this method should be balanced with previous calls
70
     * to _pushSegment.
71
     *
72
     * @param bool $needPop Is a pop required. Only true if last push
73
     *                      was successful
74
     *
75
     * @throws InvalidOperationException If found un-balanced call
76
     *                                   with _pushSegment
77
     */
78
    public function popSegment($needPop)
79
    {
80
        if ($needPop) {
81
            if (!empty($this->segmentNames)) {
82
                array_pop($this->segmentNames);
83
                array_pop($this->segmentResourceSetWrappers);
84
85
                assert(count($this->segmentNames) == count($this->segmentResourceSetWrappers), $this->mismatch);
86
            } else {
87
                throw new InvalidOperationException(
88
                    'Found non-balanced call to pushSegment and popSegment'
89
                );
90
            }
91
        }
92
    }
93
94
    /**
95
     * Retrieve stored segment names
96
     *
97
     * @return \string[]
98
     */
99
    public function getSegmentNames()
100
    {
101
        return $this->segmentNames;
102
    }
103
104
    /**
105
     * Retrieve stored segment wrappers
106
     *
107
     * @return ResourceSetWrapper[]
108
     */
109
    public function getSegmentWrappers()
110
    {
111
        return $this->segmentResourceSetWrappers;
112
    }
113
114
    /**
115
     * Gets reference to the request submitted by client.
116
     *
117
     * @return RequestDescription
118
     */
119
    public function getRequest()
120
    {
121
        return $this->request;
122
    }
123
}
124