UserNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 9 1
A supportsNormalization() 0 4 2
A normalizeTags() 0 9 2
1
<?php
2
3
namespace Badger\Bundle\UserBundle\Normalizer;
4
5
use Badger\Component\User\Model\UserInterface;
6
use Doctrine\ORM\PersistentCollection;
7
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8
9
/**
10
 * User normalizer
11
 *
12
 * @author  Olivier Soulet <[email protected]>
13
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
14
 */
15
class UserNormalizer implements NormalizerInterface
16
{
17
    /** @var array */
18
    protected $supportedFormats = ['json'];
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function normalize($user, $format = null, array $context = [])
24
    {
25
        return [
26
            'id'             => (int) $user->getId(),
27
            'username'       => (string) $user->getUsername(),
28
            'profilePicture' => (string) $user->getProfilePicture(),
29
            'tags'           => $this->normalizeTags($user->getTags()),
30
        ];
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function supportsNormalization($data, $format = null)
37
    {
38
        return $data instanceof UserInterface && in_array($format, $this->supportedFormats);
39
    }
40
41
    /**
42
     * @param array $tags
43
     *
44
     * @return array
45
     */
46
    private function normalizeTags(array $tags)
47
    {
48
        $normalizedTags = [];
49
        foreach ($tags as $tag) {
50
            $normalizedTags[] = (string) $tag->getName();
51
        }
52
53
        return $normalizedTags;
54
    }
55
}
56