Completed
Pull Request — master (#269)
by Christopher
05:58 queued 01:46
created

ODataEntry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
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 29
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
19
{
20
    /**
21
     * Entry id.
22
     *
23
     * @var string|null
24
     */
25
    public $id;
26
    /**
27
     * Entry Self Link.
28
     *
29
     * @var string|null
30
     */
31
    public $selfLink;
32
    /**
33
     * Entry title.
34
     *
35
     * @var ODataTitle|null
36
     */
37
    public $title;
38
    /**
39
     * Entry Edit Link.
40
     *
41
     * @var ODataLink|null
42
     */
43
    public $editLink;
44
    /**
45
     * Entry Type. This become the value of term attribute of Category element.
46
     *
47
     * @var ODataCategory|null
48
     */
49
    public $type;
50
    /**
51
     * Instance to hold entity properties.
52
     * Properties corresponding to "m:properties" under content element
53
     * in the case of Non-MLE. For MLE "m:properties" is direct child of entry.
54
     *
55
     * @var ODataPropertyContent|null
56
     */
57
    public $propertyContent;
58
    /**
59
     * Collection of entry media links (Named Stream Links).
60
     *
61
     * @var array<ODataMediaLink>
62
     */
63
    public $mediaLinks = [];
64
    /**
65
     * media link entry (MLE Link).
66
     *
67
     * @var ODataMediaLink|null
68
     */
69
    public $mediaLink;
70
    /**
71
     * Collection of navigation links (can be expanded).
72
     *
73
     * @var array<ODataLink>
74
     */
75
    public $links = [];
76
    /**
77
     * Entry ETag.
78
     *
79
     * @var string|null
80
     */
81
    public $eTag;
82
83
    /**
84
     * True if this is a media link entry.
85
     *
86
     * @var bool|null
87
     */
88
    public $isMediaLinkEntry;
89
90
    /**
91
     * The name of the resource set this entry belongs to, use in metadata output.
92
     *
93
     * @var string|null
94
     */
95
    public $resourceSetName;
96
97
    /**
98
     * Last updated timestamp.
99
     *
100
     * @var string|null
101
     */
102
    public $updated;
103
104
    /**
105
     * Service Base URI.
106
     *
107
     * @var string|null
108
     */
109
    public $baseURI;
110
111
    /**
112
     * ODataEntry constructor.
113
     * @param string|null $id
114
     * @param string|null $selfLink
115
     * @param ODataTitle|null $title
116
     * @param ODataLink|null $editLink
117
     * @param ODataCategory|null $type
118
     * @param ODataPropertyContent|null $propertyContent
119
     * @param array $mediaLinks
120
     * @param ODataMediaLink|null $mediaLink
121
     * @param array $links
122
     * @param string|null $eTag
123
     * @param bool|null $isMediaLinkEntry
124
     * @param string|null $resourceSetName
125
     * @param string|null $updated
126
     * @param string|null $baseURI
127
     */
128
    public function __construct(?string $id = null,
129
                                ?string $selfLink = null,
130
                                ?ODataTitle $title = null,
131
                                ?ODataLink $editLink = null,
132
                                ?ODataCategory $type = null,
133
                                ?ODataPropertyContent $propertyContent = null,
134
                                array $mediaLinks = null,
135
                                ?ODataMediaLink $mediaLink = null,
136
                                array $links = null,
137
                                ?string $eTag = null,
138
                                ?bool $isMediaLinkEntry = null,
139
                                ?string $resourceSetName = null,
140
                                ?string $updated = null,
141
                                ?string $baseURI = null)
142
    {
143
        $this->id = $id;
144
        $this->selfLink = $selfLink;
145
        $this->title = $title;
146
        $this->editLink = $editLink;
147
        $this->type = $type;
148
        $this->propertyContent = $propertyContent;
149
        $this->mediaLinks = $mediaLinks;
150
        $this->mediaLink = $mediaLink;
151
        $this->links = $links;
152
        $this->eTag = $eTag;
153
        $this->isMediaLinkEntry = $isMediaLinkEntry;
154
        $this->resourceSetName = $resourceSetName;
155
        $this->updated = $updated;
156
        $this->baseURI = $baseURI;
157
    }
158
159
    /**
160
     * @return string|null
161
     */
162
    public function getId(): ?string
163
    {
164
        return $this->id;
165
    }
166
167
    /**
168
     * @param string|null $id
169
     * @return ODataEntry
170
     */
171
    public function setId(?string $id): ODataEntry
172
    {
173
        $this->id = $id;
174
        return $this;
175
    }
176
177
    /**
178
     * @return string|null
179
     */
180
    public function getSelfLink(): ?string
181
    {
182
        return $this->selfLink;
183
    }
184
185
    /**
186
     * @param string|null $selfLink
187
     * @return ODataEntry
188
     */
189
    public function setSelfLink(?string $selfLink): ODataEntry
190
    {
191
        $this->selfLink = $selfLink;
192
        return $this;
193
    }
194
195
    /**
196
     * @return ODataTitle|null
197
     */
198
    public function getTitle(): ?ODataTitle
199
    {
200
        return $this->title;
201
    }
202
203
    /**
204
     * @param ODataTitle|null $title
205
     * @return ODataEntry
206
     */
207
    public function setTitle(?ODataTitle $title): ODataEntry
208
    {
209
        $this->title = $title;
210
        return $this;
211
    }
212
213
    /**
214
     * @return string|null
215
     */
216
    public function getETag(): ?string
217
    {
218
        return $this->eTag;
219
    }
220
221
    /**
222
     * @param string|null $eTag
223
     * @return ODataEntry
224
     */
225
    public function setETag(?string $eTag): ODataEntry
226
    {
227
        $this->eTag = $eTag;
228
        return $this;
229
    }
230
231
    /**
232
     * @return bool|null
233
     */
234
    public function getIsMediaLinkEntry(): ?bool
235
    {
236
        return $this->isMediaLinkEntry;
237
    }
238
239
    /**
240
     * @param bool|null $isMediaLinkEntry
241
     * @return ODataEntry
242
     */
243
    public function setIsMediaLinkEntry(?bool $isMediaLinkEntry): ODataEntry
244
    {
245
        $this->isMediaLinkEntry = $isMediaLinkEntry;
246
        return $this;
247
    }
248
249
    /**
250
     * @return string|null
251
     */
252
    public function getResourceSetName(): ?string
253
    {
254
        return $this->resourceSetName;
255
    }
256
257
    /**
258
     * @param string|null $resourceSetName
259
     * @return ODataEntry
260
     */
261
    public function setResourceSetName(?string $resourceSetName): ODataEntry
262
    {
263
        $this->resourceSetName = $resourceSetName;
264
        return $this;
265
    }
266
267
    /**
268
     * @return string|null
269
     */
270
    public function getUpdated(): ?string
271
    {
272
        return $this->updated;
273
    }
274
275
    /**
276
     * @param string|null $updated
277
     * @return ODataEntry
278
     */
279
    public function setUpdated(?string $updated): ODataEntry
280
    {
281
        $this->updated = $updated;
282
        return $this;
283
    }
284
285
    /**
286
     * @return string|null
287
     */
288
    public function getBaseURI(): ?string
289
    {
290
        return $this->baseURI;
291
    }
292
293
    /**
294
     * @param string|null $baseURI
295
     * @return ODataEntry
296
     */
297
    public function setBaseURI(?string $baseURI): ODataEntry
298
    {
299
        $this->baseURI = $baseURI;
300
        return $this;
301
    }
302
    /**
303
     * @return AtomContent
304
     */
305
    public function getAtomContent()
306
    {
307
        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...
308
            return new AtomObjectModel\AtomContent(
309
                MimeTypes::MIME_APPLICATION_XML,
310
                null,
311
                $this->propertyContent
312
            );
313
        }
314
        return new AtomObjectModel\AtomContent($this->mediaLink->contentType, $this->mediaLink->srcLink);
315
    }
316
317
    /**
318
     * @param AtomContent $atomContent
319
     */
320
    public function setAtomContent(AtomObjectModel\AtomContent $atomContent)
321
    {
322
        $this->setPropertyContent($atomContent->properties);
323
    }
324
325
    /**
326
     * @return AtomAuthor
327
     */
328
    public function getAtomAuthor()
329
    {
330
        return new AtomObjectModel\AtomAuthor();
331
    }
332
333
    /**
334
     * @return null|ODataPropertyContent
335
     */
336
    public function getPropertyContent()
337
    {
338
        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...
339
            return null;
340
        }
341
        return $this->propertyContent;
342
    }
343
344
    /**
345
     * @param ODataPropertyContent|null $oDataPropertyContent
346
     */
347
    public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null)
348
    {
349
        $this->propertyContent = $oDataPropertyContent;
350
    }
351
352
    /**
353
     * @return ODataLink
354
     */
355
    public function getEditLink()
356
    {
357
        return $this->editLink;
358
    }
359
360
    /**
361
     * @return ODataCategory
362
     */
363
    public function getType()
364
    {
365
        return $this->type;
366
    }
367
368
    /**
369
     * @param ODataCategory|null $type
370
     */
371
    public function setType(ODataCategory $type = null)
372
    {
373
        $this->type = $type;
374
        if (null !== $type) {
375
            $rawTerm               = $type->getTerm();
376
            $termArray             = explode('.', $rawTerm);
377
            $final                 = $termArray[count($termArray) - 1];
378
            $this->resourceSetName = MetadataManager::getResourceSetNameFromResourceType($final);
379
        }
380
    }
381
382
    /**
383
     * @return ODataLink[]
384
     */
385
    public function getLinks()
386
    {
387
        return $this->links;
388
    }
389
390
    /**
391
     * @param $links ODataLink[]
392
     */
393
    public function setLinks(array $links)
394
    {
395
        $this->links = [];
396
        foreach ($links as $link) {
397
            if ('edit' == $link->name) {
398
                $this->editLink        = $link;
399
                $this->resourceSetName = explode('(', $link->url)[0];
400
                continue;
401
            }
402
            if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->name, 0, 61)
403
            ) {
404
                $this->links[] = $link;
405
                continue;
406
            }
407
        }
408
    }
409
410
    /**
411
     * @return ODataMediaLink[]
412
     */
413
    public function getMediaLinks()
414
    {
415
        return $this->mediaLinks;
416
    }
417
418
    /**
419
     * @param ODataMediaLink[] $mediaLinks
420
     */
421
    public function setMediaLinks(array $mediaLinks)
422
    {
423
        $this->mediaLinks = [];
424
        $editLink         = null;
425
        foreach ($mediaLinks as $mediaLink) {
426
            $this->handleMediaLinkEntry($mediaLink, $editLink);
427
        }
428
        $this->correctMediaLinkSrc($editLink);
429
        if (null === $this->mediaLink) {
430
            $this->isMediaLinkEntry = false;
431
        }
432
    }
433
434
    /**
435
     * @param ODataMediaLink      $mediaLink
436
     * @param ODataMediaLink|null $editLink
437
     */
438
    private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null)
439
    {
440
        if ('edit-media' == $mediaLink->rel) {
441
            $this->isMediaLinkEntry = true;
442
            $this->mediaLink        = $mediaLink;
443
        }
444
        if (ODataConstants::ATOM_MEDIA_RESOURCE_RELATION_ATTRIBUTE_VALUE == substr($mediaLink->rel, 0, 68)) {
445
            $this->mediaLinks[] = $mediaLink;
446
        }
447
        if ('edit' == $mediaLink->rel) {
448
            $editLink = $mediaLink;
449
        }
450
    }
451
452
    /**
453
     * @param ODataMediaLink|null $editLink
454
     */
455
    private function correctMediaLinkSrc(ODataMediaLink $editLink = null)
456
    {
457
        if (null !== $this->mediaLink && null !== $editLink) {
458
            $this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink;
459
            foreach ($this->mediaLinks as $mediaLink) {
460
                $mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name;
461
            }
462
        }
463
    }
464
465
    /**
466
     * @return ODataMediaLink
467
     */
468
    public function getMediaLink()
469
    {
470
        return $this->mediaLink;
471
    }
472
473
    /**
474
     * @param  string|null $msg
475
     * @return bool
476
     */
477
    public function isOk(&$msg = null)
478
    {
479
        if (!$this->propertyContent instanceof ODataPropertyContent) {
480
            $msg = 'Property content must be instanceof ODataPropertyContent.';
481
            return false;
482
        }
483
        if (0 === count($this->propertyContent->properties)) {
484
            $msg = 'Must have at least one property present.';
485
            return false;
486
        }
487
488
        return true;
489
    }
490
}
491