DoctrineSerializer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
A getIdentifierValues() 0 11 2
1
<?php
2
3
namespace TreeHouse\Queue\Message\Serializer;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
7
class DoctrineSerializer implements SerializerInterface
8
{
9
    /**
10
     * @var ManagerRegistry
11
     */
12
    protected $doctrine;
13
14
    /**
15
     * @param ManagerRegistry $doctrine
16
     */
17 5
    public function __construct(ManagerRegistry $doctrine)
18
    {
19 5
        $this->doctrine = $doctrine;
20 5
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25 4
    public function serialize($value)
26
    {
27 4
        return json_encode($this->getIdentifierValues($value));
28
    }
29
30
    /**
31
     * @param object $value
32
     *
33
     * @throws \InvalidArgumentException When $value is not a Doctrine object
34
     *
35
     * @return array
36
     */
37 4
    protected function getIdentifierValues($value)
38
    {
39 4
        if (!is_object($value)) {
40 3
            throw new \InvalidArgumentException('Only Doctrine objects can be serialized');
41
        }
42
43 1
        $class = get_class($value);
44 1
        $metadata = $this->doctrine->getManager()->getClassMetadata($class);
45
46 1
        return $metadata->getIdentifierValues($value);
47
    }
48
}
49