Passed
Pull Request — master (#25)
by
unknown
13:18
created

AbstractODMType::setEntityClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Goodwix\DoctrineJsonOdm\Type;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Types\JsonType;
9
use Goodwix\DoctrineJsonOdm\Exception\JsonOdmException;
10
use Symfony\Component\Serializer\Exception\ExceptionInterface;
11
use Symfony\Component\Serializer\SerializerInterface;
12
13
14
abstract class AbstractODMType extends JsonType
15
{
16
    /** @var array */
17
    public $deserializationContext = [];
18
    /** @var SerializerInterface */
19
    private $serializer;
20
21
    /** @var string */
22
    private $format = 'json';
23
24
    /** @var string */
25
    private $entityClass;
26
27
    /** @var array */
28
    private $serializationContext = [];
29
30 20
    public function getSerializer(): SerializerInterface
31
    {
32 20
        if (null === $this->serializer) {
33 1
            throw new \RuntimeException(
34 1
                sprintf(
35
                    'An instance of "%s" must be available. Call the "setSerializer" method.',
36
                    SerializerInterface::class
37
                ));
38
        }
39
40 19
        return $this->serializer;
41
    }
42
43 10
    public function setSerializer(SerializerInterface $serializer): void
44
    {
45 10
        $this->serializer = $serializer;
46
    }
47
48 2
    public function getFormat(): string
49
    {
50 2
        return $this->format;
51
    }
52
53 1
    public function setFormat(string $format): void
54
    {
55 1
        $this->format = $format;
56
    }
57
58 14
    public function getEntityClass(): string
59
    {
60 14
        if (null === $this->entityClass) {
61 1
            throw new \RuntimeException(
62
                'ODM entity class name must be available. Call the "setEntityClass" method.'
63
            );
64
        }
65
66 13
        return $this->entityClass;
67
    }
68
69 9
    public function setEntityClass(string $entityClass): void
70
    {
71 9
        $this->entityClass = $entityClass;
72
    }
73
74 16
    public function getSerializationContext(): array
75
    {
76 16
        return $this->serializationContext;
77
    }
78
79 4
    public function setSerializationContext(array $serializationContext): void
80
    {
81 4
        $this->serializationContext = $serializationContext;
82
    }
83
84 10
    public function getDeserializationContext(): array
85
    {
86 10
        return $this->deserializationContext;
87
    }
88
89 4
    public function setDeserializationContext(array $deserializationContext): void
90
    {
91 4
        $this->deserializationContext = $deserializationContext;
92
    }
93
94 1
    public function getName(): string
95
    {
96 1
        return $this->getEntityClass();
97
    }
98
99
    /**
100
     * @throws JsonOdmException
101
     */
102 17
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
103
    {
104 17
        if (null === $value) {
105 3
            return null;
106
        }
107
108
        try {
109 14
            $context = $this->getSerializationContext();
110 14
            $value = $this->getSerializer()->serialize($value, $this->format, $context);
111 1
        } catch (ExceptionInterface $exception) {
112 1
            $message = sprintf('Serialization exception occurred for class "%s".', $this->getEntityClass());
113
114 1
            throw new JsonOdmException($message, 0, $exception);
115
        }
116
117 13
        return $value;
118
    }
119
120
    /**
121
     * @throws JsonOdmException
122
     * @return object|null|array
123
     */
124 12
    public function convertToPHPValue($value, AbstractPlatform $platform)
125
    {
126 12
        if (null === $value || '' === $value) {
127 4
            return null;
128
        }
129
130
        try {
131 8
            $context = $this->getDeserializationContext();
132 8
            $value = $this->getSerializer()->deserialize($value, $this->getEntityClass(), $this->format, $context);
133 1
        } catch (ExceptionInterface $exception) {
134 1
            $message = sprintf('Deserialization exception occurred for class "%s".', $this->getEntityClass());
135
136 1
            throw new JsonOdmException($message, 0, $exception);
137
        }
138
139 7
        return $value;
140
    }
141
142
    public static abstract function registerODMType(string $entityClass, SerializerInterface $serializer): void;
143
}