UpdateStreamHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 63
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 51 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\TwitchBundle\Scheduler\Handler;
6
7
use DateMalformedStringException;
8
use DateTime;
9
use Doctrine\ORM\EntityManagerInterface;
10
use ProjetNormandie\TwitchBundle\DataProvider\TwitchItemDataProvider;
11
use ProjetNormandie\TwitchBundle\Entity\Channel;
12
use ProjetNormandie\TwitchBundle\Entity\Game;
13
use ProjetNormandie\TwitchBundle\Entity\Stream;
14
use ProjetNormandie\TwitchBundle\Scheduler\Message\UpdateStream;
15
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
16
17
18
#[AsMessageHandler]
19
readonly class UpdateStreamHandler
20
{
21
    public function __construct(
22
        private EntityManagerInterface $em,
23
        private TwitchItemDataProvider $twitchItemDataProvider,
24
    ) {
25
    }
26
27
    /**
28
     * @throws DateMalformedStringException
29
     */
30
    public function __invoke(UpdateStream $message): void
31
    {
32
        $channels = $this->em->getRepository(Channel::class)->findAll();
33
34
        $usernames = [];
35
        foreach ($channels as $channel) {
36
            $usernames[] = $channel->getUsername();
37
        }
38
39
        $response = $this->twitchItemDataProvider->getStreams($usernames);
40
        $data = json_decode($response->getBody()->getContents())->data;
41
        if (count($data) === 0) {
42
            return;
43
        }
44
45
        foreach ($data as $twicthStream) {
46
            $findStream = $this->em->getRepository(Stream::class)->findOneBy(['externalId' => $twicthStream->id]);
47
48
49
            if ($findStream) {
50
                if ($twicthStream->viewer_count > $findStream->getViewerCount()) {
51
                    $findStream->setViewerCount((int) $twicthStream->viewer_count);
52
                    $this->em->flush();
53
                }
54
                continue;
55
            }
56
57
            // Game
58
            $game = $this->em->getRepository(Game::class)->findOneBy(['externalId' => $twicthStream->game_id]);
59
            if (null == $game) {
60
                $game = new Game();
61
                $game->setExternalId($twicthStream->game_id);
62
                $game->setName($twicthStream->game_name);
63
                $this->em->persist($game);
64
            }
65
            $game->setLastStreamAt(new DateTime());
66
            $this->em->flush();
67
68
            $entityStream = new Stream();
69
            $entityStream->setGame($game);
70
            $entityStream->setChannel($this->em->getRepository(Channel::class)->findOneBy(['username' => $twicthStream->user_login]));
0 ignored issues
show
Bug introduced by
It seems like $this->em->getRepository...cthStream->user_login)) can also be of type null; however, parameter $channel of ProjetNormandie\TwitchBu...ty\Stream::setChannel() does only seem to accept ProjetNormandie\TwitchBundle\Entity\Channel, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $entityStream->setChannel(/** @scrutinizer ignore-type */ $this->em->getRepository(Channel::class)->findOneBy(['username' => $twicthStream->user_login]));
Loading history...
71
            $entityStream->setExternalId($twicthStream->id);
72
            $entityStream->setTitle($twicthStream->title);
73
            $entityStream->setType($twicthStream->type);
74
            $entityStream->setStartedAt(new DateTime($twicthStream->started_at));
75
            $entityStream->setViewerCount((int) $twicthStream->viewer_count);
76
            $entityStream->setLanguage($twicthStream->language);
77
            $entityStream->setTags($twicthStream->tags);
78
            $entityStream->setIsMature($twicthStream->is_mature);
79
            $this->em->persist($entityStream);
80
            $this->em->flush();
81
        }
82
    }
83
}
84