NullSerializer::unserialize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Gendoria\CommandQueue\Serializer;
4
5
use Gendoria\CommandQueue\Command\CommandInterface;
6
use Gendoria\CommandQueue\Serializer\Exception\UnserializeErrorException;
7
use InvalidArgumentException;
8
9
/**
10
 * This class creates command data with no serialization at all.
11
 *
12
 * @author Tomasz Struczyński <[email protected]>
13
 */
14
class NullSerializer implements SerializerInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public function serialize(CommandInterface $command)
20
    {
21 1
        return new SerializedCommandData($command, get_class($command));
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     * 
27
     * @throws InvalidArgumentException Thrown, when "serialized" data is not an instance of correct command class.
28
     */
29 7
    public function unserialize(SerializedCommandData $serializedCommandData)
30
    {
31 7
        $commandClass = $serializedCommandData->getCommandClass();
32 7
        if (!is_object($serializedCommandData->getSerializedCommand()) || !$serializedCommandData->getSerializedCommand() instanceof $commandClass) {
33 3
            throw new UnserializeErrorException($serializedCommandData, "Null serializer accepts only commands as serialized command data.");
34
        }
35 4
        return $serializedCommandData->getSerializedCommand();
36
    }
37
38
}
39