Completed
Pull Request — master (#183)
by Beñat
04:47
created

SimpleBusAsyncEventSerializer::deserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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\SharedKernel\Infrastructure\Serialization\SimpleBus;
16
17
use Kreta\SharedKernel\Event\AsyncEvent;
18
use Kreta\SharedKernel\Serialization\AsyncEventSerializer;
19
use SimpleBus\Serialization\Envelope\DefaultEnvelope;
20
use SimpleBus\Serialization\ObjectSerializer;
21
22
class SimpleBusAsyncEventSerializer implements ObjectSerializer
23
{
24
    private $asyncEventSerializer;
25
26
    public function __construct(AsyncEventSerializer $asyncEventSerializer)
27
    {
28
        $this->asyncEventSerializer = $asyncEventSerializer;
29
    }
30
31
    public function serialize($object)
32
    {
33
        if ($object instanceof DefaultEnvelope) {
34
            return $this->asyncEventSerializer->serialize($object->message());
35
        }
36
37
        return $this->asyncEventSerializer->serialize($object);
38
    }
39
40
    public function deserialize($serializedObject, $type)
41
    {
42
        if ($type === DefaultEnvelope::class) {
43
            return DefaultEnvelope::forSerializedMessage(
44
                AsyncEvent::class,
45
                $serializedObject
46
            );
47
        }
48
        $this->asyncEventSerializer->deserialize($serializedObject);
49
    }
50
}
51