+ private function purgeChannels(Episode $episode, $sgEntry) {
+ $ext_ids = [];
+ foreach ($sgEntry['channels'] as $sgChannel) {
+ $ext_ids[] = 'sg:'.$sgChannel['id'];
+ }
+ $channels = $episode->channels()
+ ->where('ext_id', 'LIKE', 'sg:%')
+ ->whereNotIn('ext_id', $ext_ids)
+ ->get();
+ if (!$channels->isEmpty()) {
+ $episode->channels()->detach($channels->pluck('id'));
+ }
+ }
+
+ private function syncChannel(Episode $episode, $sgChannel) {
+ $ext_id = 'sg:'.$sgChannel['id'];
+ $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
+ if (!$channel) {
+ $channel = new Channel();
+ $channel->ext_id = $ext_id;
+ $channel->organization()->associate($this->org);
+ }
+ $channel->short_name = $sgChannel['initials'];
+ $channel->title = $sgChannel['name'];
+ $channel->stream_link = 'https://twitch.tv/'.strtolower($sgChannel['name']);
+ $channel->languages = [$sgChannel['language']];
+ $channel->save();
+ return $channel;
+ }
+
+ private function purgeCrew(Episode $episode, $sgCrews, $prefix) {
+ $ext_ids = [];
+ foreach ($sgCrews as $sgCrew) {
+ $ext_ids[] = 'sg:'.$prefix.':'.$sgCrew['id'];
+ }
+ $episode->crew()->where('ext_id', 'LIKE', 'sg:'.$prefix.':%')->whereNotIn('ext_id', $ext_ids)->delete();
+ }
+
+ private function syncCrew(Episode $episode, $sgCrew, $prefix, $role) {
+ $ext_id = 'sg:'.$prefix.':'.$sgCrew['id'];
+ $crew = $episode->crew()->firstWhere('ext_id', '=', $ext_id);
+ if (!$crew) {
+ $crew = new EpisodeCrew();
+ $crew->ext_id = $ext_id;
+ $crew->episode()->associate($episode);
+ }
+ $user = $this->getUserBySGPlayer($sgCrew);
+ if ($user) {
+ $crew->user()->associate($user);
+ } else {
+ $crew->user()->disassociate();
+ }
+ if ($role == 'commentary') {
+ $channel = $this->getChannelByCrew($episode, $sgCrew);
+ if ($channel) {
+ $crew->channel()->associate($channel);
+ } else {
+ $crew->channel()->disassociate();
+ }
+ }
+ $crew->role = $role;
+ $crew->confirmed = $sgCrew['approved'] ?: false;
+ if (!empty($sgCrew['displayName'])) {
+ $crew->name_override = $sgCrew['displayName'];
+ }
+ if (!empty($sgCrew['publicStream'])) {
+ $crew->stream_override = 'https://twitch.tv/'.strtolower($sgCrew['publicStream']);
+ }
+ $crew->save();
+ }
+