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 |
|
|
|
|
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
|
|
|
|
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.