Command::getKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * This file is part of the Aggrego.
5
 * (c) Tomasz Kunicki <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
declare(strict_types = 1);
13
14
namespace Aggrego\Domain\Api\Command\CreateBoard;
15
16
use Aggrego\CommandConsumer\Command as ConsumerCommand;
17
use Aggrego\CommandConsumer\Name;
18
use Aggrego\CommandConsumer\Uuid;
19
use Aggrego\Domain\Board\Key;
20
use Aggrego\Domain\Profile\Profile;
21
use Assert\Assertion;
22
23
class Command implements ConsumerCommand
24
{
25
    public const NAME = 'Aggrego/Domain/CreateBoard';
26
27
    /**
28
     * @var Uuid
29
     */
30
    private $uuid;
31
32
    /**
33
     * @var Key
34
     */
35
    private $key;
36
37
    /**
38
     * @var Profile
39
     */
40
    private $profile;
41
42
    public function __construct(string $uuid, array $key, string $profileName, string $versionNumber)
43
    {
44
        $this->uuid = new Uuid($uuid);
45
        $this->key = new Key($key);
46
        $this->profile = Profile::createFromParts($profileName, $versionNumber);
47
    }
48
49
    public function getKey(): Key
50
    {
51
        return $this->key;
52
    }
53
54
    public function getProfile(): Profile
55
    {
56
        return $this->profile;
57
    }
58
59
    public function getName(): Name
60
    {
61
        return new Name(self::NAME);
62
    }
63
64
    public function getUuid(): Uuid
65
    {
66
        return $this->uuid;
67
    }
68
69
    public function serialize()
70
    {
71
        return json_encode([
72
            'uuid' => $this->getUuid()->getValue(),
73
            'name' => $this->getName()->getValue(),
74
            'key' => $this->key->getValue(),
75
            'profile_name' => $this->profile->getName(),
76
            'profile_version' => $this->profile->getVersion()
77
        ]);
78
    }
79
80
    public function unserialize($serialized): self
81
    {
82
        $json = json_decode($serialized, true);
83
        Assertion::keyExists($json, 'uuid');
84
        Assertion::keyExists($json, 'name');
85
        Assertion::eq($json['name'], self::NAME);
86
        Assertion::keyExists($json, 'key');
87
        Assertion::keyExists($json, 'profile_name');
88
        Assertion::keyExists($json, 'profile_version');
89
90
        $this->uuid = new Uuid($json['uuid']);
91
        $this->key = new Key($json['key']);
92
        $this->profile = Profile::createFromParts($json['profile_name'], $json['profile_version']);
93
94
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aggrego\Domain\Api\Command\CreateBoard\Command which is incompatible with the return type mandated by Serializable::unserialize() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
95
    }
96
}
97