Passed
Pull Request — master (#4)
by ANTHONIUS
04:55
created

UserNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
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 4
    public function __construct(
28
        CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater
29
    )
30
    {
31 4
        $this->canonicalFieldsUpdater = $canonicalFieldsUpdater;
32
    }
33
34 2
    public function supportsDenormalization($data, $type, $format = null, array $context = [])
35
    {
36
        // Make sure we're not called twice
37 2
        if (isset($context[self::ALREADY_CALLED])) {
38 2
            return false;
39
        }
40
41 2
        return $type === 'App\Entity\User';
42
    }
43
44 2
    public function denormalize($data, $class, $format = null, array $context = [])
45
    {
46
        /* @var UserInterface $object */
47
48 2
        $context[self::ALREADY_CALLED] = true;
49
50 2
        $object = $this->denormalizer->denormalize($data, $class, $format, $context);
51
52 2
        $canonicalFieldsUpdater = $this->canonicalFieldsUpdater;
53 2
        if(isset($data['username'])){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
54 2
            $usernameCanonical = $canonicalFieldsUpdater->canonicalizeUsername($data['username']);
55 2
            $object->setUsernameCanonical($usernameCanonical);
56
        }
57 2
        if(isset($data['email'])){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
58 2
            $emailCanonical = $canonicalFieldsUpdater->canonicalizeEmail($data['email']);
59 2
            $object->setEmailCanonical($emailCanonical);
60
        }
61
62 2
        return $object;
63
    }
64
}