forked from yzPeedro/SugoiAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchEngine.php
More file actions
85 lines (72 loc) · 2.71 KB
/
SearchEngine.php
File metadata and controls
85 lines (72 loc) · 2.71 KB
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
<?php
namespace App\Support\Traits;
use App\Actions\Providers\SerializeEpisodeAction;
use App\Support\Media;
use GuzzleHttp\Client;
trait SearchEngine
{
use HandleRequest;
public function getAllSearchPromises(int $episodeNumber, int $season, string $slug): array
{
if ($this->mustSerializeEpisode()) {
$episodeNumber = SerializeEpisodeAction::run($episodeNumber);
}
$endpoints = [
$this->getSearchEpisodeEndpoint($episodeNumber, $season, $slug),
];
if (!$this->canUsePrefix() && !$this->canUseSuffix()) {
return $endpoints;
}
if ($this->canUsePrefix() && $this->mustUsePrefixes()) {
foreach (Media::COMMON_PREFIXES as $prefix) {
$endpoints[] = $this->getSearchEpisodeEndpoint($episodeNumber, $season, "$prefix-$slug");
}
}
if ($this->canUseSuffix() && $this->mustUseSuffixes()) {
foreach (Media::COMMON_SUFFIXES as $suffix) {
$endpoints[] = $this->getSearchEpisodeEndpoint($episodeNumber, $season, "$slug-$suffix");
}
}
return $endpoints;
}
/**
* @throws \Throwable
*/
public function search(int $episodeNumber, int $season, string $slug): array
{
$response = [];
$endpoints = $this->getAllSearchPromises($episodeNumber, $season, $slug);
foreach ($endpoints as $endpoint) {
try {
$client = new Client(['base_uri' => $this->baseUrl()]);
$promiseResponse = $client->request($this->searchRequestMethod(), $endpoint);
$response[] = match (true) {
$this->responseHasError($promiseResponse) && !$this->ignoreOnFail() => [
'error' => true,
'searched_endpoint' => $endpoint,
'episode' => null,
],
$this->mustHandleResponse() => [
'error' => false,
'searched_endpoint' => $endpoint,
'episode' => $this->handleResponse($promiseResponse),
],
default => [
'error' => false,
'searched_endpoint' => $endpoint,
'episode' => $endpoint,
],
};
} catch (\Throwable) {
if (!$this->ignoreOnFail()) {
$response[] = [
'error' => true,
'searched_endpoint' => $endpoint,
'episode' => null,
];
}
}
}
return $response;
}
}