|
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])); |
|
|
|
|
|
|
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
|
|
|
|