Passed
Pull Request — master (#269)
by Christopher
03:18
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
     */
227
    public function setAtomContent(AtomObjectModel\AtomContent $atomContent)
228
    {
229
        $this->setPropertyContent($atomContent->properties);
230
    }
231
232
    /**
233
     * @return AtomAuthor
234
     */
235
    public function getAtomAuthor()
236
    {
237
        return new AtomObjectModel\AtomAuthor();
238
    }
239
240
    /**
241
     * @return null|ODataPropertyContent
242
     */
243
    public function getPropertyContent()
244
    {
245
        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...
246
            return null;
247
        }
248
        return $this->propertyContent;
249
    }
250
251
    /**
252
     * @param ODataPropertyContent|null $oDataPropertyContent
253
     */
254
    public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null)
255
    {
256
        $this->propertyContent = $oDataPropertyContent;
257
    }
258
259
    /**
260
     * @return ODataLink
261
     */
262
    public function getEditLink()
263
    {
264
        return $this->editLink;
265
    }
266
267
    /**
268
     * @return ODataCategory
269
     */
270
    public function getType()
271
    {
272
        return $this->type;
273
    }
274
275
    /**
276
     * @param ODataCategory|null $type
277
     */
278
    public function setType(ODataCategory $type = null)
279
    {
280
        $this->type = $type;
281
        if (null !== $type) {
282
            $rawTerm               = $type->getTerm();
283
            $termArray             = explode('.', $rawTerm);
284
            $final                 = $termArray[count($termArray) - 1];
285
            $this->resourceSetName = MetadataManager::getResourceSetNameFromResourceType($final);
286
        }
287
    }
288
289
    /**
290
     * @return ODataLink[]
291
     */
292
    public function getLinks()
293
    {
294
        return $this->links;
295
    }
296
297
    /**
298
     * @param $links ODataLink[]
299
     */
300
    public function setLinks(array $links)
301
    {
302
        $this->links = [];
303
        foreach ($links as $link) {
304
            if ('edit' == $link->getName()) {
305
                $this->editLink        = $link;
306
                $this->resourceSetName = explode('(', $link->url)[0];
307
                continue;
308
            }
309
            if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->getName(), 0, 61)
310
            ) {
311
                $this->links[] = $link;
312
                continue;
313
            }
314
        }
315
    }
316
317
    /**
318
     * @return ODataMediaLink[]
319
     */
320
    public function getMediaLinks()
321
    {
322
        return $this->mediaLinks;
323
    }
324
325
    /**
326
     * @param ODataMediaLink[] $mediaLinks
327
     */
328
    public function setMediaLinks(array $mediaLinks)
329
    {
330
        $this->mediaLinks = [];
331
        $editLink         = null;
332
        foreach ($mediaLinks as $mediaLink) {
333
            $this->handleMediaLinkEntry($mediaLink, $editLink);
334
        }
335
        $this->correctMediaLinkSrc($editLink);
336
        if (null === $this->mediaLink) {
337
            $this->isMediaLinkEntry = false;
338
        }
339
    }
340
341
    /**
342
     * @param ODataMediaLink      $mediaLink
343
     * @param ODataMediaLink|null $editLink
344
     */
345
    private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null)
346
    {
347
        if ('edit-media' == $mediaLink->rel) {
348
            $this->isMediaLinkEntry = true;
349
            $this->mediaLink        = $mediaLink;
350
        }
351
        if (ODataConstants::ATOM_MEDIA_RESOURCE_RELATION_ATTRIBUTE_VALUE == substr($mediaLink->rel, 0, 68)) {
352
            $this->mediaLinks[] = $mediaLink;
353
        }
354
        if ('edit' == $mediaLink->rel) {
355
            $editLink = $mediaLink;
356
        }
357
    }
358
359
    /**
360
     * @param ODataMediaLink|null $editLink
361
     */
362
    private function correctMediaLinkSrc(ODataMediaLink $editLink = null)
363
    {
364
        if (null !== $this->mediaLink && null !== $editLink) {
365
            $this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink;
366
            foreach ($this->mediaLinks as $mediaLink) {
367
                $mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name;
368
            }
369
        }
370
    }
371
372
    /**
373
     * @return ODataMediaLink
374
     */
375
    public function getMediaLink()
376
    {
377
        return $this->mediaLink;
378
    }
379
380
    /**
381
     * @param  string|null $msg
382
     * @return bool
383
     */
384
    public function isOk(&$msg = null)
385
    {
386
        if (!$this->propertyContent instanceof ODataPropertyContent) {
387
            $msg = 'Property content must be instanceof ODataPropertyContent.';
388
            return false;
389
        }
390
        if (0 === count($this->propertyContent->properties)) {
391
            $msg = 'Must have at least one property present.';
392
            return false;
393
        }
394
395
        return true;
396
    }
397
}
398