-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComicVineService.php
More file actions
180 lines (145 loc) · 5.7 KB
/
ComicVineService.php
File metadata and controls
180 lines (145 loc) · 5.7 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace App\Services;
use App\Services\Saloon\ComicVine\ComicVineConnector;
use App\Services\Saloon\ComicVine\Requests\GetIssues;
use App\Services\Saloon\ComicVine\Requests\GetVolumeDetails;
use App\Services\Saloon\ComicVine\Requests\SearchVolumes;
class ComicVineService
{
protected ComicVineConnector $connector;
public function __construct()
{
$this->connector = new ComicVineConnector();
}
public function isConfigured(): bool
{
return ! empty(config('services.comic_vine.api_key'));
}
public function searchVolumes(string $query): array
{
if (! $this->isConfigured()) {
return [];
}
try {
$response = $this->connector->send(new SearchVolumes($query));
if (! $response->successful()) {
return [];
}
$data = $response->json();
if (($data['status_code'] ?? 0) !== 1) {
return [];
}
return collect($data['results'] ?? [])
->filter(fn ($item) => ($item['resource_type'] ?? '') === 'volume')
->map(fn ($item) => [
'volume_id' => (string) $item['id'],
'title' => $item['name'] ?? '',
'publisher' => $item['publisher']['name'] ?? null,
'start_year' => ! empty($item['start_year']) ? (int) $item['start_year'] : null,
'issue_count' => $item['count_of_issues'] ?? null,
'cover_url' => $item['image']['medium_url'] ?? $item['image']['small_url'] ?? null,
'description' => $this->stripHtml($item['description'] ?? null),
'comicvine_url' => $item['site_detail_url'] ?? null,
])
->values()
->all();
} catch (\Exception) {
return [];
}
}
public function fetchVolumeDetails(string $volumeId): ?array
{
if (! $this->isConfigured()) {
return null;
}
try {
$response = $this->connector->send(new GetVolumeDetails($volumeId));
if (! $response->successful()) {
return null;
}
$data = $response->json();
if (($data['status_code'] ?? 0) !== 1) {
return null;
}
$result = $data['results'] ?? [];
$creators = collect($result['people'] ?? [])
->pluck('name')
->take(20)
->implode(', ');
$characters = collect($result['characters'] ?? [])
->pluck('name')
->take(20)
->implode(', ');
return [
'volume_id' => (string) $result['id'],
'title' => $result['name'] ?? '',
'publisher' => $result['publisher']['name'] ?? null,
'start_year' => ! empty($result['start_year']) ? (int) $result['start_year'] : null,
'issue_count' => $result['count_of_issues'] ?? null,
'cover_url' => $result['image']['medium_url'] ?? $result['image']['small_url'] ?? null,
'description' => $this->stripHtml($result['description'] ?? null),
'comicvine_url' => $result['site_detail_url'] ?? null,
'creators' => $creators ?: null,
'characters' => $characters ?: null,
];
} catch (\Exception) {
return null;
}
}
public function fetchVolumeIssues(string $volumeId): array
{
if (! $this->isConfigured()) {
return [];
}
$allIssues = [];
$offset = 0;
$limit = 100;
$maxPages = 10; // Safety cap to avoid exceeding execution time limits
$page = 0;
try {
do {
$response = $this->connector->send(new GetIssues($volumeId, $offset, $limit));
if (! $response->successful()) {
break;
}
$data = $response->json();
if (($data['status_code'] ?? 0) !== 1) {
break;
}
$results = $data['results'] ?? [];
$totalResults = $data['number_of_total_results'] ?? 0;
foreach ($results as $item) {
$allIssues[] = [
'issue_id' => (string) $item['id'],
'title' => $item['name'] ?? null,
'issue_number' => $item['issue_number'] ?? null,
'cover_date' => $item['cover_date'] ?? null,
'cover_url' => $item['image']['medium_url'] ?? $item['image']['small_url'] ?? null,
'description' => $this->stripHtml($item['description'] ?? null),
'comicvine_url' => $item['site_detail_url'] ?? null,
];
}
$offset += $limit;
$page++;
// Respect rate limits if we need more pages
if ($offset < $totalResults && ! empty($results)) {
usleep(400000);
}
} while ($offset < $totalResults && ! empty($results) && $page < $maxPages);
} catch (\Exception) {
// Return whatever we collected so far
}
return $allIssues;
}
protected function stripHtml(?string $html): ?string
{
if (empty($html)) {
return null;
}
$text = strip_tags($html);
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$text = preg_replace('/\s+/', ' ', $text);
return trim($text) ?: null;
}
}