Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

denormalize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 4
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\NotificationId;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationMarkedAsUnread;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationStatus;
20
use Kreta\Notifier\Domain\Model\Inbox\UserId;
21
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
23
24 View Code Duplication
final class SymfonyNotificationMarkedAsUnreadDenormalizer implements DenormalizerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
{
26
    private $nameConverter;
27
28
    public function __construct(NameConverterInterface $nameConverter)
29
    {
30
        $this->nameConverter = $nameConverter;
31
    }
32
33
    public function denormalize($data, $class, $format = null, array $context = []) : NotificationMarkedAsUnread
34
    {
35
        $notificationMarkedAsUnread = new NotificationMarkedAsUnread(
36
            NotificationId::generate($data['payload']['id']),
37
            UserId::generate($data['payload']['user_id'])
38
        );
39
40
        $reflectionClass = new \ReflectionClass($notificationMarkedAsUnread);
41
        $reflectionProperty = $reflectionClass->getProperty('occurredOn');
42
        $reflectionProperty->setAccessible(true);
43
        $reflectionProperty->setValue(
44
            $notificationMarkedAsUnread,
45
            \DateTimeImmutable::createFromFormat('U', (string) $data['occurred_on'])
46
        );
47
48
        $reflectionProperty = $reflectionClass->getProperty('status');
49
        $reflectionProperty->setAccessible(true);
50
        $reflectionProperty->setValue(
51
            $notificationMarkedAsUnread,
52
            new NotificationStatus($data['payload']['status'])
53
        );
54
55
        return $notificationMarkedAsUnread;
56
    }
57
58
    public function supportsDenormalization($data, $type, $format = null) : bool
59
    {
60
        return isset($data['type'])
61
            && $this->nameConverter->denormalize($data['type']) === NotificationMarkedAsUnread::class;
62
    }
63
}
64