Completed
Pull Request — master (#366)
by Beñat
05:23
created

supportsDenormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 3
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\Inbox\Notification;
16
17
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationBody;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationId;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationPublished;
20
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationStatus;
21
use Kreta\Notifier\Domain\Model\Inbox\UserId;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
23
24
final class SymfonyNotificationPublishedDenormalizer implements DenormalizerInterface
25
{
26
    public function denormalize($data, $class, $format = null, array $context = []) : NotificationPublished
27
    {
28
        $notificationPublished = new NotificationPublished(
29
            NotificationId::generate($data['payload']['id']),
30
            UserId::generate($data['payload']['user_id']),
31
            new NotificationBody($data['payload']['body'])
32
        );
33
34
        $reflectionClass = new \ReflectionClass($notificationPublished);
35
        $reflectionProperty = $reflectionClass->getProperty('occurredOn');
36
        $reflectionProperty->setAccessible(true);
37
        $reflectionProperty->setValue(
38
            $notificationPublished,
39
            \DateTimeImmutable::createFromFormat('U', (string) $data['occurred_on'])
40
        );
41
42
        $reflectionProperty = $reflectionClass->getProperty('status');
43
        $reflectionProperty->setAccessible(true);
44
        $reflectionProperty->setValue(
45
            $notificationPublished,
46
            new NotificationStatus($data['payload']['status'])
47
        );
48
49
        return $notificationPublished;
50
    }
51
52
    public function supportsDenormalization($data, $type, $format = null) : bool
53
    {
54
        return isset($data['type']) && $data['type'] === NotificationPublished::class;
55
    }
56
}
57