Passed
Pull Request — master (#269)
by Alex
09:14
created

ODataEntry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 14
c 1
b 0
f 1
nc 1
nop 14
dl 0
loc 30
rs 9.7998

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\ObjectModel;
6
7
use AlgoWeb\ODataMetadata\MetadataManager;
8
use POData\Common\MimeTypes;
9
use POData\Common\ODataConstants;
10
use POData\ObjectModel\AtomObjectModel\AtomAuthor;
11
use POData\ObjectModel\AtomObjectModel\AtomContent;
12
13
/**
14
 * Class ODataEntry.
15
 * TODO: the methods should be rearranged to match theorder of the properties.
16
 * TODO: the properties are still public needs a lot of unpicking to work out as type hints maybe wrong.
17
 */
18
class ODataEntry extends ODataContainerBase
19
{
20
21
    /**
22
     * Entry Self Link.
23
     *
24
     * @var string|null
25
     */
26
    public $selfLink;
27
    /**
28
     * Entry Edit Link.
29
     *
30
     * @var ODataLink|null
31
     */
32
    public $editLink;
33
    /**
34
     * Entry Type. This become the value of term attribute of Category element.
35
     *
36
     * @var ODataCategory|null
37
     */
38
    public $type;
39
    /**
40
     * Instance to hold entity properties.
41
     * Properties corresponding to "m:properties" under content element
42
     * in the case of Non-MLE. For MLE "m:properties" is direct child of entry.
43
     *
44
     * @var ODataPropertyContent|null
45
     */
46
    public $propertyContent;
47
    /**
48
     * Collection of entry media links (Named Stream Links).
49
     *
50
     * @var array<ODataMediaLink>
51
     */
52
    public $mediaLinks = [];
53
    /**
54
     * media link entry (MLE Link).
55
     *
56
     * @var ODataMediaLink|null
57
     */
58
    public $mediaLink;
59
    /**
60
     * Collection of navigation links (can be expanded).
61
     *
62
     * @var array<ODataLink>
63
     */
64
    public $links = [];
65
    /**
66
     * Entry ETag.
67
     *
68
     * @var string|null
69
     */
70
    public $eTag;
71
72
    /**
73
     * True if this is a media link entry.
74
     *
75
     * @var bool|null
76
     */
77
    public $isMediaLinkEntry;
78
79
    /**
80
     * The name of the resource set this entry belongs to, use in metadata output.
81
     *
82
     * @var string|null
83
     */
84
    public $resourceSetName;
85
86
87
88
    /**
89
     * ODataEntry constructor.
90
     * @param string|null $id
91
     * @param string|null $selfLink
92
     * @param ODataTitle|null $title
93
     * @param ODataLink|null $editLink
94
     * @param ODataCategory|null $type
95
     * @param ODataPropertyContent|null $propertyContent
96
     * @param array $mediaLinks
97
     * @param ODataMediaLink|null $mediaLink
98
     * @param array $links
99
     * @param string|null $eTag
100
     * @param bool|null $isMediaLinkEntry
101
     * @param string|null $resourceSetName
102
     * @param string|null $updated
103
     * @param string|null $baseURI
104
     */
105
    public function __construct(
106
        ?string $id = null,
107
        ?string $selfLink = null,
108
        ?ODataTitle $title = null,
109
        ?ODataLink $editLink = null,
110
        ?ODataCategory $type = null,
111
        ?ODataPropertyContent $propertyContent = null,
112
        array $mediaLinks = [],
113
        ?ODataMediaLink $mediaLink = null,
114
        array $links = [],
115
        ?string $eTag = null,
116
        ?bool $isMediaLinkEntry = null,
117
        ?string $resourceSetName = null,
118
        ?string $updated = null,
119
        ?string $baseURI = null
120
    ) {
121
        $this->id = $id;
122
        $this->selfLink = $selfLink;
123
        $this->title = $title;
124
        $this->editLink = $editLink;
125
        $this->type = $type;
126
        $this->propertyContent = $propertyContent;
127
        $this->mediaLinks = $mediaLinks;
128
        $this->mediaLink = $mediaLink;
129
        $this->links = $links;
130
        $this->eTag = $eTag;
131
        $this->isMediaLinkEntry = $isMediaLinkEntry;
132
        $this->resourceSetName = $resourceSetName;
133
        $this->updated = $updated;
134
        $this->baseURI = $baseURI;
135
    }
136
137
    /**
138
     * @return string|null
139
     */
140
    public function getSelfLink(): ?string
141
    {
142
        return $this->selfLink;
143
    }
144
145
    /**
146
     * @param string|null $selfLink
147
     * @return ODataEntry
148
     */
149
    public function setSelfLink(?string $selfLink): ODataEntry
150
    {
151
        $this->selfLink = $selfLink;
152
        return $this;
153
    }
154
155
    /**
156
     * @return string|null
157
     */
158
    public function getETag(): ?string
159
    {
160
        return $this->eTag;
161
    }
162
163
    /**
164
     * @param string|null $eTag
165
     * @return ODataEntry
166
     */
167
    public function setETag(?string $eTag): ODataEntry
168
    {
169
        $this->eTag = $eTag;
170
        return $this;
171
    }
172
173
    /**
174
     * @return bool|null
175
     */
176
    public function getIsMediaLinkEntry(): ?bool
177
    {
178
        return $this->isMediaLinkEntry;
179
    }
180
181
    /**
182
     * @param bool|null $isMediaLinkEntry
183
     * @return ODataEntry
184
     */
185
    public function setIsMediaLinkEntry(?bool $isMediaLinkEntry): ODataEntry
186
    {
187
        $this->isMediaLinkEntry = $isMediaLinkEntry;
188
        return $this;
189
    }
190
191
    /**
192
     * @return string|null
193
     */
194
    public function getResourceSetName(): ?string
195
    {
196
        return $this->resourceSetName;
197
    }
198
199
    /**
200
     * @param string|null $resourceSetName
201
     * @return ODataEntry
202
     */
203
    public function setResourceSetName(?string $resourceSetName): ODataEntry
204
    {
205
        $this->resourceSetName = $resourceSetName;
206
        return $this;
207
    }
208
209
    /**
210
     * @return AtomContent
211
     */
212
    public function getAtomContent()
213
    {
214
        if (!$this->isMediaLinkEntry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isMediaLinkEntry of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
215
            return new AtomObjectModel\AtomContent(
216
                MimeTypes::MIME_APPLICATION_XML,
217
                null,
218
                $this->propertyContent
219
            );
220
        }
221
        return new AtomObjectModel\AtomContent($this->mediaLink->contentType, $this->mediaLink->srcLink);
222
    }
223
224
    /**
225
     * @param AtomContent $atomContent
226
     * @return ODataEntry
227
     */
228
    public function setAtomContent(AtomObjectModel\AtomContent $atomContent): self
229
    {
230
        $this->setPropertyContent($atomContent->properties);
231
        return $this;
232
    }
233
234
    /**
235
     * @return AtomAuthor
236
     */
237
    public function getAtomAuthor(): AtomAuthor
238
    {
239
        return new AtomObjectModel\AtomAuthor();
240
    }
241
242
    /**
243
     * @return null|ODataPropertyContent
244
     */
245
    public function getPropertyContent(): ?ODataPropertyContent
246
    {
247
        if (!$this->isMediaLinkEntry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isMediaLinkEntry of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
248
            return null;
249
        }
250
        return $this->propertyContent;
251
    }
252
253
    /**
254
     * @param ODataPropertyContent|null $oDataPropertyContent
255
     * @return ODataEntry
256
     */
257
    public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null): self
258
    {
259
        $this->propertyContent = $oDataPropertyContent;
260
        return $this;
261
    }
262
263
    /**
264
     * @return ODataLink
265
     */
266
    public function getEditLink(): ODataLink
267
    {
268
        return $this->editLink;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->editLink could return the type null which is incompatible with the type-hinted return POData\ObjectModel\ODataLink. Consider adding an additional type-check to rule them out.
Loading history...
269
    }
270
271
    /**
272
     * @return ODataCategory
273
     */
274
    public function getType(): ODataCategory
275
    {
276
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->type could return the type null which is incompatible with the type-hinted return POData\ObjectModel\ODataCategory. Consider adding an additional type-check to rule them out.
Loading history...
277
    }
278
279
    /**
280
     * @param ODataCategory|null $type
281
     * @return ODataEntry
282
     */
283
    public function setType(ODataCategory $type = null): self
284
    {
285
        $this->type = $type;
286
        if (null !== $type) {
287
            $rawTerm               = $type->getTerm();
288
            $termArray             = explode('.', $rawTerm);
289
            $final                 = $termArray[count($termArray) - 1];
290
            $this->resourceSetName = MetadataManager::getResourceSetNameFromResourceType($final);
291
        }
292
        return $this;
293
    }
294
295
    /**
296
     * @return ODataLink[]
297
     */
298
    public function getLinks(): array
299
    {
300
        return $this->links;
301
    }
302
303
    /**
304
     * @param $links ODataLink[]
305
     * @return ODataEntry
306
     */
307
    public function setLinks(array $links): self
308
    {
309
        $this->links = [];
310
        foreach ($links as $link) {
311
            if ('edit' == $link->getName()) {
312
                $this->editLink        = $link;
313
                $this->resourceSetName = explode('(', $link->getUrl())[0];
314
                continue;
315
            }
316
            if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->getName(), 0, 61)
317
            ) {
318
                $this->links[] = $link;
319
                continue;
320
            }
321
        }
322
        return $this;
323
    }
324
325
    /**
326
     * @return ODataMediaLink[]
327
     */
328
    public function getMediaLinks(): array
329
    {
330
        return $this->mediaLinks;
331
    }
332
333
    /**
334
     * @param ODataMediaLink[] $mediaLinks
335
     * @return ODataEntry
336
     */
337
    public function setMediaLinks(array $mediaLinks): self
338
    {
339
        $this->mediaLinks = [];
340
        $editLink         = null;
341
        foreach ($mediaLinks as $mediaLink) {
342
            $this->handleMediaLinkEntry($mediaLink, $editLink);
343
        }
344
        $this->correctMediaLinkSrc($editLink);
345
        if (null === $this->mediaLink) {
346
            $this->isMediaLinkEntry = false;
347
        }
348
        return $this;
349
    }
350
351
    /**
352
     * @param ODataMediaLink      $mediaLink
353
     * @param ODataMediaLink|null $editLink
354
     */
355
    private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null): void
356
    {
357
        if ('edit-media' == $mediaLink->rel) {
358
            $this->isMediaLinkEntry = true;
359
            $this->mediaLink        = $mediaLink;
360
        }
361
        if (ODataConstants::ATOM_MEDIA_RESOURCE_RELATION_ATTRIBUTE_VALUE == substr($mediaLink->rel, 0, 68)) {
362
            $this->mediaLinks[] = $mediaLink;
363
        }
364
        if ('edit' == $mediaLink->rel) {
365
            $editLink = $mediaLink;
366
        }
367
    }
368
369
    /**
370
     * @param ODataMediaLink|null $editLink
371
     */
372
    private function correctMediaLinkSrc(ODataMediaLink $editLink = null): void
373
    {
374
        if (null !== $this->mediaLink && null !== $editLink) {
375
            $this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink;
376
            foreach ($this->mediaLinks as $mediaLink) {
377
                $mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name;
378
            }
379
        }
380
    }
381
382
    /**
383
     * @return ODataMediaLink
384
     */
385
    public function getMediaLink(): ODataMediaLink
386
    {
387
        return $this->mediaLink;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->mediaLink could return the type null which is incompatible with the type-hinted return POData\ObjectModel\ODataMediaLink. Consider adding an additional type-check to rule them out.
Loading history...
388
    }
389
390
    /**
391
     * @param  string|null $msg
392
     * @return bool
393
     */
394
    public function isOk(&$msg = null): bool
395
    {
396
        if (!$this->propertyContent instanceof ODataPropertyContent) {
397
            $msg = 'Property content must be instanceof ODataPropertyContent.';
398
            return false;
399
        }
400
        if (0 === count($this->propertyContent->properties)) {
401
            $msg = 'Must have at least one property present.';
402
            return false;
403
        }
404
405
        return true;
406
    }
407
}
408