Completed
Push — master ( 4407aa...f0730d )
by ANTHONIUS
22s queued 14s
created

UserNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 49
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A denormalize() 0 19 3
A supportsDenormalization() 0 8 2
A __construct() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the DoyoUserBundle project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\UserBundle\Bridge\ApiPlatform;
15
16
use Doyo\UserBundle\Model\UserInterface;
17
use Doyo\UserBundle\Util\CanonicalFieldsUpdaterInterface;
18
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
21
22
class UserNormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
23
{
24
    use DenormalizerAwareTrait;
25
26
    private const ALREADY_CALLED = 'USER_NORMALIZER';
27
28
    /**
29
     * @var CanonicalFieldsUpdaterInterface
30
     */
31
    private $canonicalFieldsUpdater;
32
33
    /**
34
     * UserNormalizer constructor.
35
     */
36 15
    public function __construct(
37
        CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater
38
    ) {
39 15
        $this->canonicalFieldsUpdater = $canonicalFieldsUpdater;
40
    }
41
42 7
    public function supportsDenormalization($data, $type, $format = null, array $context = [])
43
    {
44
        // Make sure we're not called twice
45 7
        if (isset($context[self::ALREADY_CALLED])) {
46 5
            return false;
47
        }
48
49 7
        return 'App\Entity\User' === $type;
50
    }
51
52 5
    public function denormalize($data, $class, $format = null, array $context = [])
53
    {
54
        /* @var UserInterface $object */
55
56 5
        $context[self::ALREADY_CALLED] = true;
57
58 5
        $object = $this->denormalizer->denormalize($data, $class, $format, $context);
59
60 5
        $canonicalFieldsUpdater = $this->canonicalFieldsUpdater;
61 5
        if (isset($data['username'])) {
62 3
            $usernameCanonical = $canonicalFieldsUpdater->canonicalizeUsername($data['username']);
63 3
            $object->setUsernameCanonical($usernameCanonical);
64
        }
65 5
        if (isset($data['email'])) {
66 3
            $emailCanonical = $canonicalFieldsUpdater->canonicalizeEmail($data['email']);
67 3
            $object->setEmailCanonical($emailCanonical);
68
        }
69
70 5
        return $object;
71
    }
72
}
73