Completed
Pull Request — master (#366)
by Beñat
04:52
created

JMSSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\Notifier\Infrastructure\Serialization\JMS;
16
17
use JMS\Serializer\SerializerBuilder;
18
use JMS\Serializer\SerializerInterface;
19
use Kreta\SharedKernel\Serialization\Serializer;
20
21
final class JMSSerializer implements Serializer
22
{
23
    private $serializer;
24
    private $format;
25
26
    public function __construct(string $format = 'json')
27
    {
28
        $this->serializer = $this->serializer();
29
        $this->format = $format;
30
    }
31
32
    public function serialize($object) : string
33
    {
34
        return $this->serializer->serialize($object, $this->format);
35
    }
36
37
    public function deserialize(string $serializedObject, string $type)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        return $this->serializer->deserialize($serializedObject, $type, $this->format);
40
    }
41
42
    private function serializer() : SerializerInterface
43
    {
44
        return SerializerBuilder::create()
45
            ->addMetadataDir(
46
                __DIR__ . '/../../../../../../vendor/kreta/shared-kernel/src/Kreta/SharedKernel' .
47
                '/Infrastructure/Serialization/JMS/Config/Identity',
48
                'Kreta\\SharedKernel\\Domain\\Model\\Identity'
49
            )
50
            ->addMetadataDir(__DIR__ . '/Config/Inbox', 'Kreta\\Notifier\\Domain\\Model\\Inbox')
51
            ->addMetadataDir(
52
                __DIR__ . '/Config/Inbox/Notification',
53
                'Kreta\\Notifier\\Domain\\Model\\Inbox\\Notification'
54
            )
55
            ->setCacheDir(__DIR__ . '/../../../../../../var/cache/jms-serializer')
56
            ->build();
57
    }
58
}
59