-
Notifications
You must be signed in to change notification settings - Fork 187
/
Api.php
162 lines (133 loc) · 3.12 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace App\Services\Api;
use App\Episode;
use App\Item;
use App\Services\Models\ItemService;
use App\Services\TMDB;
use Symfony\Component\HttpFoundation\Response;
abstract class Api
{
/**
* @var array
*/
protected $data = [];
/**
* @var Item
*/
private $item;
/**
* @var TMDB
*/
private $tmdb;
/**
* @var ItemService
*/
private $itemService;
/**
* @var Episode
*/
private $episode;
public function __construct(Item $item, TMDB $tmdb, ItemService $itemService, Episode $episode)
{
$this->item = $item;
$this->tmdb = $tmdb;
$this->itemService = $itemService;
$this->episode = $episode;
}
public function handle(array $data)
{
logInfo('api data:', $data);
$this->data = $data;
if ($this->abortRequest()) {
abort(Response::HTTP_NOT_IMPLEMENTED);
}
$found = $this->item
->findByTitle($this->getTitle(), $this->getType())
->first();
// Nothing found in our database, so we search in TMDb.
if (!$found) {
$foundFromTmdb = $this->tmdb->search($this->getTitle(), $this->getType());
if (!$foundFromTmdb) {
return false;
}
// The first result is mostly the one we need.
$firstResult = $foundFromTmdb[0];
// Search again in our database with the TMDb ID.
$found = $this->item->findByTmdbId($firstResult['tmdb_id'])->first();
if (!$found) {
$found = $this->itemService->create($firstResult);
}
}
if ($this->shouldRateItem()) {
$found->update([
'rating' => $this->getRating(),
]);
}
if ($this->shouldEpisodeMarkedAsSeen()) {
$episode = $this->episode
->findByTmdbId($found->tmdb_id)
->findByEpisodeNumber($this->getEpisodeNumber())
->findBySeasonNumber($this->getSeasonNumber())
->first();
// Mark the episode as seen and update the last_seen_at attribute of the item
if ($episode) {
$found->updateLastSeenAt($found->tmdb_id);
$episode->update([
'seen' => true,
]);
}
}
}
/**
* Abort the complete request if it's not a movie or episode.
*
* @return bool
*/
abstract protected function abortRequest();
/**
* Is it a movie or tv show? Should return 'tv' or 'movie'.
*
* @return string
*/
abstract protected function getType();
/**
* Title for the item (name of the movie or tv show).
*
* @return string
*/
abstract protected function getTitle();
/**
* Rating for flox in a 3-Point system.
*
* 1 = Good.
* 2 = Medium.
* 3 = Bad.
*
* @return int
*/
abstract protected function getRating();
/**
* Check if rating is requested.
*
* @return bool
*/
abstract protected function shouldRateItem();
/**
* Check if seen episode is requested.
*
* @return bool
*/
abstract protected function shouldEpisodeMarkedAsSeen();
/**
* Number of the episode.
*
* @return null|int
*/
abstract protected function getEpisodeNumber();
/**
* Number of the season.
*
* @return null|int
*/
abstract protected function getSeasonNumber();
}