GetStream   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 14 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\TwitchBundle\Controller\Channel;
6
7
use ProjetNormandie\TwitchBundle\DataProvider\TwitchItemDataProvider;
8
use ProjetNormandie\TwitchBundle\Entity\Channel;
9
use ProjetNormandie\TwitchBundle\Model\Twitch\Stream;
10
use ProjetNormandie\TwitchBundle\Serializer\SerializerRegistry;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
14
class GetStream extends AbstractController
15
{
16
    public function __construct(
17
        private readonly TwitchItemDataProvider $twitchItemDataProvider
18
    ) {
19
    }
20
21
22
    /**
23
     * @param Channel $channel
24
     * @return JsonResponse
25
     */
26
    public function __invoke(Channel $channel): JsonResponse
27
    {
28
        $response = $this->twitchItemDataProvider->getStream($channel->getUsername());
29
        $data = json_decode($response->getBody()->getContents())->data;
30
        if (count($data) === 0) {
31
            return new JsonResponse(new Stream());
32
        }
33
34
        $stream = $data[0];
35
        $serializer = SerializerRegistry::getSerializer();
36
        $serialized = $serializer->serialize($stream, 'json');
37
        $deserialized = $serializer->deserialize($serialized, Stream::class, 'json');
38
39
        return new JsonResponse($deserialized);
40
    }
41
}
42