Passed
Push — master ( bc13b6...650a1b )
by Nicolas
08:26 queued 04:11
created

ArticleNormalizer::hasCacheableSupportsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Serializer\Normalizer;
6
7
use App\Entity\Article;
8
use App\Entity\Tag;
9
use App\Entity\User;
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
12
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
13
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
14
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
15
16
/**
17
 * ArticleNormalizer.
18
 */
19
class ArticleNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
20
{
21
    use NormalizerAwareTrait;
22
23
    /**
24
     * @var TokenStorageInterface
25
     */
26
    private $tokenStorage;
27
28
    /**
29
     * @param TokenStorageInterface $tokenStorage
30
     */
31 30
    public function __construct(TokenStorageInterface $tokenStorage)
32
    {
33 30
        $this->tokenStorage = $tokenStorage;
34 30
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 12
    public function normalize($object, $format = null, array $context = []): array
40
    {
41
        /* @var Article $object */
42
43
        $data = [
44 12
            'slug' => $object->getSlug(),
45 12
            'title' => $object->getTitle(),
46 12
            'description' => $object->getDescription(),
47 12
            'body' => $object->getBody(),
48
            'tagList' => \array_map(function (Tag $tag) {
49 8
                return $this->normalizer->normalize($tag);
50 12
            }, $object->getTags()->toArray()),
51 12
            'createdAt' => $this->normalizer->normalize($object->getCreatedAt()),
52 12
            'updatedAt' => $this->normalizer->normalize($object->getCreatedAt()),
53
            'favorited' => false,
54 12
            'favoritesCount' => $object->getFavoritedByCount(),
55 12
            'author' => $this->normalizer->normalize($object->getAuthor()),
56
        ];
57
58 12
        $token = $this->tokenStorage->getToken();
59 12
        $user = $token ? $token->getUser() : null;
60
61 12
        if ($user instanceof User) {
62 6
            $data['favorited'] = $user->hasFavorite($object);
63
        }
64
65 12
        return $data;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 28
    public function supportsNormalization($data, $format = null): bool
72
    {
73 28
        return $data instanceof Article;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 28
    public function hasCacheableSupportsMethod(): bool
80
    {
81 28
        return true;
82
    }
83
}
84