Test Failed
Branch develop (7f369c)
by Nicolas
04:26
created

UserNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Serializer\Normalizer;
4
5
use App\Entity\User;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
11
/**
12
 * UserNormalizer.
13
 */
14
class UserNormalizer implements NormalizerInterface, NormalizerAwareInterface
15
{
16
    use NormalizerAwareTrait;
17
18
    /**
19
     * @var TokenStorageInterface
20
     */
21
    private $tokenStorage;
22
23
    /**
24
     * @param TokenStorageInterface $tokenStorage
25
     */
26
    public function __construct(TokenStorageInterface $tokenStorage)
27
    {
28
        $this->tokenStorage = $tokenStorage;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function normalize($object, $format = null, array $context = [])
35
    {
36
        /* @var User $object */
37
38
        $data = [
39
            'username' => $object->getUsername(),
40
            'image' => $object->getImage(),
41
            'bio' => $object->getBio(),
42
        ];
43
44
        if (isset($context['groups']) && in_array('me', $context['groups'], true)) {
45
            $data['email'] = $object->getEmail();
46
        } else {
47
            /** @var User $user */
48
            $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
49
            $data['following'] = $user instanceof User ? $user->follows($object) : false;
0 ignored issues
show
introduced by
$user is always a sub-type of App\Entity\User.
Loading history...
50
        }
51
52
        return $data;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportsNormalization($data, $format = null)
59
    {
60
        return $data instanceof User;
61
    }
62
}
63