Completed
Push — master ( 787675...b66143 )
by Tomasz
14s
created

Command::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 13
rs 9.9332
c 0
b 0
f 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\Neo4jIntegration\Api\Command\RunCommand;
15
16
use Aggrego\CommandConsumer\Command as CommandConsumer;
17
use Aggrego\CommandConsumer\Name;
18
use Aggrego\CommandConsumer\Uuid;
19
use Assert\Assertion;
20
21
class Command implements CommandConsumer
22
{
23
    public const NAME = 'neo4j_integration.run_command';
24
25
    /** @var Uuid */
26
    private $uuid;
27
28
    /** @var string */
29
    private $query;
30
31
    /** @var array */
32
    private $parameters;
33
34
    public function __construct(string $uuid, string $query, array $parameters = [])
35
    {
36
        Assertion::notEmpty($query);
37
        $this->uuid = new Uuid($uuid);
38
        $this->query = $query;
39
        $this->parameters = $parameters;
40
    }
41
42
    public function getUuid(): Uuid
43
    {
44
        return $this->uuid;
45
    }
46
47
    public function getName(): Name
48
    {
49
        return new Name(self::NAME);
50
    }
51
52
    public function getCypherQuery(): string
53
    {
54
        return $this->query;
55
    }
56
57
    public function getParameters(): array
58
    {
59
        return $this->parameters;
60
    }
61
62
    public function serialize()
63
    {
64
        return json_encode(
65
            [
66
                'uuid' => $this->uuid->getValue(),
67
                'name' => self::NAME,
68
                'query' => $this->query,
69
                'parameters' => $this->parameters
70
            ]
71
        );
72
    }
73
74
    public function unserialize($serialized): self
75
    {
76
        $json = json_decode($serialized, true);
77
        Assertion::keyExists($json, 'uuid');
78
        Assertion::keyExists($json, 'name');
79
        Assertion::eq($json['name'], self::NAME);
80
        Assertion::keyExists($json, 'query');
81
        Assertion::keyExists($json, 'parameters');
82
83
        return new self(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new self($json['u...], $json['parameters']) returns the type Aggrego\Neo4jIntegration...mand\RunCommand\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...
84
            $json['uuid'],
85
            $json['query'],
86
            $json['parameters']
87
        );
88
    }
89
}
90