SerialiseDepWrapperTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
eloc 34
c 3
b 0
f 0
dl 0
loc 176
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadata() 0 6 2
A setRequest() 0 4 1
A updateLightStack() 0 5 2
A getStack() 0 3 1
A getModelSerialiser() 0 3 1
A getService() 0 3 1
A getLightStack() 0 3 1
A getCurrentResourceSetWrapper() 0 6 2
A setService() 0 5 1
A loadStackIfEmpty() 0 7 2
A getRequest() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Created by PhpStorm.
6
 * User: alex
7
 * Date: 15/02/20
8
 * Time: 6:00 PM.
9
 */
10
namespace AlgoWeb\PODataLaravel\Serialisers;
11
12
use Illuminate\Support\Facades\App;
13
use POData\Common\InvalidOperationException;
14
use POData\IService;
15
use POData\Providers\Metadata\IMetadataProvider;
16
use POData\Providers\Metadata\ResourceEntityType;
17
use POData\Providers\Metadata\ResourceSetWrapper;
18
use POData\UriProcessor\RequestDescription;
19
use POData\UriProcessor\SegmentStack;
20
21
trait SerialiseDepWrapperTrait
22
{
23
    /**
24
     * The service implementation.
25
     *
26
     * @var IService
27
     */
28
    protected $service;
29
30
    /**
31
     * Holds reference to segment stack being processed.
32
     *
33
     * @var SegmentStack
34
     */
35
    protected $stack;
36
37
    /**
38
     * Lightweight stack tracking for recursive descent fill.
39
     * @var array[]
40
     */
41
    protected $lightStack = [];
42
43
    /**
44
     * Request description instance describes OData request the
45
     * the client has submitted and result of the request.
46
     *
47
     * @var RequestDescription|null
48
     */
49
    protected $request;
50
51
52
    /**
53
     * @var ModelSerialiser
54
     */
55
    protected $modelSerialiser;
56
57
    /**
58
     * @var IMetadataProvider
59
     */
60
    protected $metaProvider;
61
62
    /**
63
     * Absolute service Uri.
64
     *
65
     * @var string
66
     */
67
    protected $absoluteServiceUri;
68
69
    /**
70
     * Absolute service Uri with slash.
71
     *
72
     * @var string
73
     */
74
    protected $absoluteServiceUriWithSlash;
75
76
    /**
77
     * Gets the data service instance.
78
     *
79
     * @return IService
80
     */
81
    public function getService()
82
    {
83
        return $this->service;
84
    }
85
86
    /**
87
     * Sets the data service instance.
88
     *
89
     * @param  IService $service
90
     * @return void
91
     */
92
    public function setService(IService $service): void
93
    {
94
        $this->service                     = $service;
95
        $this->absoluteServiceUri          = $service->getHost()->getAbsoluteServiceUri()->getUrlAsString();
96
        $this->absoluteServiceUriWithSlash = rtrim($this->absoluteServiceUri, '/') . '/';
97
    }
98
99
    /**
100
     * Gets the segment stack instance.
101
     *
102
     * @return SegmentStack
103
     */
104
    public function getStack()
105
    {
106
        return $this->stack;
107
    }
108
109
    /**
110
     * Gets reference to the request submitted by client.
111
     *
112
     * @throws InvalidOperationException
113
     * @return RequestDescription
114
     */
115
    public function getRequest()
116
    {
117
        if (null == $this->request) {
118
            throw new InvalidOperationException('Request not yet set');
119
        }
120
121
        return $this->request;
122
    }
123
124
    /**
125
     * Sets reference to the request submitted by client.
126
     *
127
     * @param RequestDescription $request
128
     */
129
    public function setRequest(RequestDescription $request): void
130
    {
131
        $this->request = $request;
132
        $this->stack->setRequest($request);
133
    }
134
135
    /**
136
     * @return ModelSerialiser
137
     */
138
    public function getModelSerialiser()
139
    {
140
        return $this->modelSerialiser;
141
    }
142
143
    /**
144
     * @return IMetadataProvider
145
     */
146
    protected function getMetadata(): IMetadataProvider
147
    {
148
        if (null == $this->metaProvider) {
149
            $this->metaProvider = App::make('metadata');
150
        }
151
        return $this->metaProvider;
152
    }
153
154
    /**
155
     * @return array[]
156
     */
157
    protected function getLightStack()
158
    {
159
        return $this->lightStack;
160
    }
161
162
    /**
163
     * @throws InvalidOperationException
164
     */
165
    protected function loadStackIfEmpty(): void
166
    {
167
        if (0 == count($this->lightStack)) {
168
            /** @var ResourceEntityType $type */
169
            $type = $this->getRequest()->getTargetResourceType();
170
            $typeName = $type->getName();
171
            array_push($this->lightStack, ['type' => $typeName, 'property' => $typeName, 'count' => 1]);
172
        }
173
    }
174
175
    /**
176
     * Resource set wrapper for the resource being serialized.
177
     *
178
     * @throws InvalidOperationException
179
     * @return ResourceSetWrapper
180
     */
181
    protected function getCurrentResourceSetWrapper(): ResourceSetWrapper
182
    {
183
        $segmentWrappers = $this->getStack()->getSegmentWrappers();
184
        $count           = count($segmentWrappers);
185
186
        return 0 == $count ? $this->getRequest()->getTargetResourceSetWrapper() : $segmentWrappers[$count-1];
187
    }
188
189
    /**
190
     * @param int $newCount
191
     */
192
    protected function updateLightStack(int $newCount): void
193
    {
194
        $this->lightStack[$newCount - 1]['count']--;
195
        if (0 == $this->lightStack[$newCount - 1]['count']) {
196
            array_pop($this->lightStack);
197
        }
198
    }
199
}
200