NullSerializer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 25
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 4 1
A unserialize() 0 8 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