Passed
Push — develop ( 3bc85b...45c84f )
by BENARD
07:51
created

UpdateStreamHandler::__invoke()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 35
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 48
rs 8.4266
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
            if ($findStream && $twicthStream->viewer_count > $findStream->getViewerCount()) {
49
                $findStream->setViewerCount((int) $twicthStream->viewer_count);
50
                $this->em->flush();
51
                continue;
52
            }
53
54
            // Game
55
            $game = $this->em->getRepository(Game::class)->findOneBy(['externalId' => $twicthStream->game_id]);
56
            if (null == $game) {
57
                $game = new Game();
58
                $game->setExternalId($twicthStream->game_id);
59
                $game->setName($twicthStream->game_name);
60
                $this->em->persist($game);
61
            }
62
            $game->setLastStreamAt(new DateTime());
63
            $this->em->flush();
64
65
            $entityStream = new Stream();
66
            $entityStream->setGame($game);
67
            $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

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