Completed
Pull Request — master (#105)
by Kristof
05:46
created

MediaObjectCreated::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6667
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Media\Events;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Media\Properties\MIMEType;
7
use ValueObjects\Identity\UUID;
8
use ValueObjects\String\String;
9
10
class MediaObjectCreated implements SerializableInterface
11
{
12
    /**
13
     * @var UUID
14
     */
15
    protected $fileId;
16
17
    /**
18
     * @var MIMEType
19
     */
20
    protected $mimeType;
21
22
    /**
23
     * @var String
24
     */
25
    protected $description;
26
27
    /**
28
     * @var String
29
     */
30
    protected $copyrightHolder;
31
32
    /**
33
     * @param UUID $fileId
34
     * @param MIMEType $fileType
35
     * @param String $description
36
     * @param String $copyrightHolder
37
     */
38 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
        UUID $fileId,
40
        MIMEType $fileType,
41
        String $description,
42
        String $copyrightHolder
43
    ) {
44
        $this->fileId = $fileId;
45
        $this->mimeType = $fileType;
46
        $this->description = $description;
0 ignored issues
show
Documentation Bug introduced by
It seems like $description can also be of type string. However, the property $description is declared as type object<ValueObjects\String\String>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
        $this->copyrightHolder = $copyrightHolder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $copyrightHolder can also be of type string. However, the property $copyrightHolder is declared as type object<ValueObjects\String\String>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
    }
49
50
    /**
51
     * @return UUID
52
     */
53
    public function getFileId()
54
    {
55
        return $this->fileId;
56
    }
57
58
    /**
59
     * @return String
60
     */
61
    public function getDescription()
62
    {
63
        return $this->description;
64
    }
65
66
    /**
67
     * @return String
68
     */
69
    public function getCopyrightHolder()
70
    {
71
        return $this->copyrightHolder;
72
    }
73
74
    /**
75
     * @return MIMEType
76
     */
77
    public function getMimeType()
78
    {
79
        return $this->mimeType;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function serialize()
86
    {
87
        return array(
88
            'file_id' => $this->getFileId()->toNative(),
89
            'mime_type' => $this->getMimeType()->toNative(),
90
            'description' => $this->getDescription()->toNative(),
91
            'copyright_holder' => $this->getCopyrightHolder()->toNative(),
92
        );
93
    }
94
95
    /**
96
     * @return MediaObjectCreated The object instance
97
     */
98
    public static function deserialize(array $data)
99
    {
100
        return new static(
101
            new UUID($data['file_id']),
102
            new MIMEType($data['mime_type']),
103
            new String($data['description']),
104
            new String($data['copyright_holder'])
105
        );
106
    }
107
}
108