Completed
Pull Request — master (#4)
by ANTHONIUS
05:57
created

UserNormalizer::supportsDenormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Doyo\UserBundle\Bridge\ApiPlatform;
5
6
use Doyo\UserBundle\Model\UserInterface;
7
use Doyo\UserBundle\Util\CanonicalFieldsUpdaterInterface;
8
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
9
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
10
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
11
12
class UserNormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
13
{
14
    use DenormalizerAwareTrait;
15
16
    private const ALREADY_CALLED = 'USER_NORMALIZER';
17
18
    /**
19
     * @var CanonicalFieldsUpdaterInterface
20
     */
21
    private $canonicalFieldsUpdater;
22
23
    /**
24
     * UserNormalizer constructor.
25
     * @param CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater
26
     */
27 10
    public function __construct(
28
        CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater
29
    )
30
    {
31 10
        $this->canonicalFieldsUpdater = $canonicalFieldsUpdater;
32
    }
33
34 4
    public function supportsDenormalization($data, $type, $format = null, array $context = [])
35
    {
36
        // Make sure we're not called twice
37 4
        if (isset($context[self::ALREADY_CALLED])) {
38 4
            return false;
39
        }
40
41 4
        return $type === 'App\Entity\User';
42
    }
43
44 4
    public function denormalize($data, $class, $format = null, array $context = [])
45
    {
46
        /* @var UserInterface $object */
47
48 4
        $context[self::ALREADY_CALLED] = true;
49
50 4
        $object = $this->denormalizer->denormalize($data, $class, $format, $context);
51
52 4
        $canonicalFieldsUpdater = $this->canonicalFieldsUpdater;
53 4
        if(isset($data['username'])){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
54 3
            $usernameCanonical = $canonicalFieldsUpdater->canonicalizeUsername($data['username']);
55 3
            $object->setUsernameCanonical($usernameCanonical);
56
        }
57 4
        if(isset($data['email'])){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
58 3
            $emailCanonical = $canonicalFieldsUpdater->canonicalizeEmail($data['email']);
59 3
            $object->setEmailCanonical($emailCanonical);
60
        }
61
62 4
        return $object;
63
    }
64
}