Passed
Pull Request — master (#269)
by Christopher
03:17
created

ODataEntry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 11
c 1
b 0
f 1
nc 1
nop 14
dl 0
loc 27
rs 9.9

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