NullSerializer::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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