Attachment::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 2
nop 8

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
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Model;
13
14
/**
15
 * An Experience API statement {@link https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#attachments attachment}.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Attachment
20
{
21
    private $usageType;
22
    private $contentType;
23
    private $length;
24
    private $sha2;
25
    private $display;
26
    private $description;
27
    private $fileUrl;
28
    private $content;
29
30
    /**
31
     * @param IRI              $usageType   The type of usage of this attachment
32
     * @param string           $contentType The content type of the attachment
33
     * @param int              $length      The length of the attachment data in octets
34
     * @param string           $sha2        The SHA-2 hash of the attachment data
35
     * @param LanguageMap      $display     Localized display name (title)
36
     * @param LanguageMap|null $description Localized description
37
     * @param IRL|null         $fileUrl     An IRL at which the attachment data can be retrieved
38
     * @param string|null      $content     The raw attachment content, please note that the content is not validated against
39
     *                                      the given SHA-2 hash
40
     */
41
    public function __construct(IRI $usageType, string $contentType, int $length, string $sha2, LanguageMap $display, LanguageMap $description = null, IRL $fileUrl = null, string $content = null)
42
    {
43
        if (null === $fileUrl && null === $content) {
44
            throw new \InvalidArgumentException('An attachment cannot be created without a file URL or raw content data.');
45
        }
46
47
        $this->usageType = $usageType;
48
        $this->contentType = $contentType;
49
        $this->length = $length;
50
        $this->sha2 = $sha2;
51
        $this->display = $display;
52
        $this->description = $description;
53
        $this->fileUrl = $fileUrl;
54
        $this->content = $content;
55
    }
56
57
    public function getUsageType(): IRI
58
    {
59
        return $this->usageType;
60
    }
61
62
    public function getContentType(): string
63
    {
64
        return $this->contentType;
65
    }
66
67
    public function getLength(): int
68
    {
69
        return $this->length;
70
    }
71
72
    public function getSha2(): string
73
    {
74
        return $this->sha2;
75
    }
76
77
    public function getDisplay(): LanguageMap
78
    {
79
        return $this->display;
80
    }
81
82
    public function getDescription(): ?LanguageMap
83
    {
84
        return $this->description;
85
    }
86
87
    public function getFileUrl(): ?IRL
88
    {
89
        return $this->fileUrl;
90
    }
91
92
    public function getContent(): ?string
93
    {
94
        return $this->content;
95
    }
96
97
    public function equals(Attachment $attachment): bool
98
    {
99
        if (!$this->usageType->equals($attachment->usageType)) {
100
            return false;
101
        }
102
103
        if ($this->contentType !== $attachment->contentType) {
104
            return false;
105
        }
106
107
        if ($this->length !== $attachment->length) {
108
            return false;
109
        }
110
111
        if ($this->sha2 !== $attachment->sha2) {
112
            return false;
113
        }
114
115
        if (!$this->display->equals($attachment->display)) {
116
            return false;
117
        }
118
119
        if (null !== $this->description xor null !== $attachment->description) {
120
            return false;
121
        }
122
123
        if (null !== $this->description && null !== $attachment->description && !$this->description->equals($attachment->description)) {
124
            return false;
125
        }
126
127
        if (null !== $this->fileUrl xor null !== $attachment->fileUrl) {
128
            return false;
129
        }
130
131
        if (null !== $this->fileUrl && null !== $attachment->fileUrl && !$this->fileUrl->equals($attachment->fileUrl)) {
132
            return false;
133
        }
134
135
        return true;
136
    }
137
}
138