DoctrineSerializer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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