Attachment   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 102
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromModel() 0 34 5
A getModel() 0 15 3
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 XApi\Repository\Doctrine\Mapping;
13
14
use Xabbuh\XApi\Model\Attachment as AttachmentModel;
15
use Xabbuh\XApi\Model\IRI;
16
use Xabbuh\XApi\Model\IRL;
17
use Xabbuh\XApi\Model\LanguageMap;
18
19
/**
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class Attachment
23
{
24
    public $identifier;
25
26
    public $statement;
27
28
    /**
29
     * @var string
30
     */
31
    public $usageType;
32
33
    /**
34
     * @var string
35
     */
36
    public $contentType;
37
38
    /**
39
     * @var int
40
     */
41
    public $length;
42
43
    /**
44
     * @var string
45
     */
46
    public $sha2;
47
48
    /**
49
     * @var array
50
     */
51
    public $display;
52
53
    /**
54
     * @var bool
55
     */
56
    public $hasDescription;
57
58
    /**
59
     * @var array|null
60
     */
61
    public $description;
62
63
    /**
64
     * @var string|null
65
     */
66
    public $fileUrl;
67
68
    /**
69
     * @var string|null
70
     */
71
    public $content;
72
73
    public static function fromModel(AttachmentModel $model)
74
    {
75
        $attachment = new self();
76
        $attachment->usageType = $model->getUsageType()->getValue();
77
        $attachment->contentType = $model->getContentType();
78
        $attachment->length = $model->getLength();
79
        $attachment->sha2 = $model->getSha2();
80
        $attachment->display = array();
81
82
        if (null !== $model->getFileUrl()) {
83
            $attachment->fileUrl = $model->getFileUrl()->getValue();
84
        }
85
86
        $attachment->content = $model->getContent();
87
88
        $display = $model->getDisplay();
89
90
        foreach ($display->languageTags() as $languageTag) {
91
            $attachment->display[$languageTag] = $display[$languageTag];
92
        }
93
94
        if (null !== $description = $model->getDescription()) {
95
            $attachment->hasDescription = true;
96
            $attachment->description = array();
97
98
            foreach ($description->languageTags() as $languageTag) {
99
                $attachment->description[$languageTag] = $description[$languageTag];
100
            }
101
        } else {
102
            $attachment->hasDescription = false;
103
        }
104
105
        return $attachment;
106
    }
107
108
    public function getModel()
109
    {
110
        $description = null;
111
        $fileUrl = null;
112
113
        if ($this->hasDescription) {
114
            $description = LanguageMap::create($this->description);
0 ignored issues
show
Bug introduced by
It seems like $this->description can also be of type null; however, Xabbuh\XApi\Model\LanguageMap::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
115
        }
116
117
        if (null !== $this->fileUrl) {
118
            $fileUrl = IRL::fromString($this->fileUrl);
119
        }
120
121
        return new AttachmentModel(IRI::fromString($this->usageType), $this->contentType, $this->length, $this->sha2, LanguageMap::create($this->display), $description, $fileUrl, $this->content);
122
    }
123
}
124