Completed
Push — master ( 619ab6...2a9e7f )
by Christian
02:15
created

Attachment   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 119
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getUsageType() 0 4 1
A getContentType() 0 4 1
A getLength() 0 4 1
A getSha2() 0 4 1
A getDisplay() 0 4 1
A getDescription() 0 4 1
A getFileUrl() 0 4 1
A getContent() 0 4 1
C equals() 0 40 14
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, $contentType, $length, $sha2, LanguageMap $display, LanguageMap $description = null, IRL $fileUrl = null, $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()
58
    {
59
        return $this->usageType;
60
    }
61
62
    public function getContentType()
63
    {
64
        return $this->contentType;
65
    }
66
67
    public function getLength()
68
    {
69
        return $this->length;
70
    }
71
72
    public function getSha2()
73
    {
74
        return $this->sha2;
75
    }
76
77
    public function getDisplay()
78
    {
79
        return $this->display;
80
    }
81
82
    public function getDescription()
83
    {
84
        return $this->description;
85
    }
86
87
    public function getFileUrl()
88
    {
89
        return $this->fileUrl;
90
    }
91
92
    public function getContent()
93
    {
94
        return $this->content;
95
    }
96
97
    public function equals(Attachment $attachment)
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