Skip to content

Commit 7d7240f

Browse files
committed
More documentations update.
1 parent 3283285 commit 7d7240f

File tree

7 files changed

+353
-93
lines changed

7 files changed

+353
-93
lines changed

src/Backends/Emby/Action/Progress.php

+32-21
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ private function action(
8383
];
8484

8585
if ($context->backendName === $entity->via) {
86-
$this->logger->info('Ignoring [{item.title}] for [{client}: {backend}]. Event generator.', [
86+
$this->logger->info(
87+
'Emby.Progress: Ignoring [{item.title}] for [{client}: {backend}]. Event generator.',
88+
[
8789
'client' => $context->clientName,
8890
'backend' => $context->backendName,
8991
...$logContext,
@@ -93,29 +95,35 @@ private function action(
9395
}
9496

9597
if (null === ag($metadata, iState::COLUMN_ID, null)) {
96-
$this->logger->warning('Ignoring [{item.title}] for [{client}: {backend}]. No metadata.', [
97-
'client' => $context->clientName,
98-
'backend' => $context->backendName,
99-
...$logContext,
100-
]);
98+
$this->logger->warning(
99+
'Emby.Progress: Ignoring [{item.title}] for [{client}: {backend}]. No metadata.',
100+
[
101+
'client' => $context->clientName,
102+
'backend' => $context->backendName,
103+
...$logContext,
104+
]
105+
);
101106
continue;
102107
}
103108

104109
$senderDate = ag($entity->getExtra($entity->via), iState::COLUMN_EXTRA_DATE);
105110
if (null === $senderDate) {
106-
$this->logger->warning('Ignoring [{item.title}] for [{client}: {backend}]. Sender date is not set.', [
107-
'client' => $context->clientName,
108-
'backend' => $context->backendName,
109-
...$logContext,
110-
]);
111+
$this->logger->warning(
112+
'Emby.Progress: Ignoring [{item.title}] for [{client}: {backend}]. Sender date is not set.',
113+
[
114+
'client' => $context->clientName,
115+
'backend' => $context->backendName,
116+
...$logContext,
117+
]
118+
);
111119
continue;
112120
}
113121
$senderDate = makeDate($senderDate)->getTimestamp();
114122

115123
$datetime = ag($entity->getExtra($context->backendName), iState::COLUMN_EXTRA_DATE, null);
116124
if (false === $ignoreDate && null !== $datetime && makeDate($datetime)->getTimestamp() > $senderDate) {
117125
$this->logger->warning(
118-
'Ignoring [{item.title}] for [{client}: {backend}]. Sender date is older than recorded backend date.',
126+
'Emby.Progress: Ignoring [{item.title}] for [{client}: {backend}]. Sender date is older than recorded backend date.',
119127
[
120128
'client' => $context->clientName,
121129
'backend' => $context->backendName,
@@ -138,7 +146,7 @@ private function action(
138146

139147
if (false === $ignoreDate && makeDate($remoteItem->updated)->getTimestamp() > $senderDate) {
140148
$this->logger->info(
141-
'Ignoring [{item.title}] for [{client}: {backend}]. Sender date is older than backend remote item date.',
149+
'Emby.Progress: Ignoring [{item.title}] for [{client}: {backend}]. Sender date is older than backend remote item date.',
142150
[
143151
'client' => $context->clientName,
144152
'backend' => $context->backendName,
@@ -150,7 +158,7 @@ private function action(
150158

151159
if ($remoteItem->isWatched()) {
152160
$this->logger->info(
153-
'Ignoring [{item.title}] for [{client}: {backend}]. The backend reported the item as watched.',
161+
'Emby.Progress: Ignoring [{item.title}] for [{client}: {backend}]. The backend reported the item as watched.',
154162
[
155163
'client' => $context->clientName,
156164
'backend' => $context->backendName,
@@ -194,13 +202,16 @@ private function action(
194202

195203
$logContext['remote']['url'] = (string)$url;
196204

197-
$this->logger->debug('Updating [{client}: {backend}] {item.type} [{item.title}] watch progress.', [
198-
// -- convert time to ticks for emby to understand it.
199-
'time' => floor($entity->getPlayProgress() * 1_00_00),
200-
'client' => $context->clientName,
201-
'backend' => $context->backendName,
202-
...$logContext,
203-
]);
205+
$this->logger->debug(
206+
'Emby.Progress: Updating [{client}: {backend}] {item.type} [{item.title}] watch progress.',
207+
[
208+
// -- convert time to ticks for emby to understand it.
209+
'time' => floor($entity->getPlayProgress() * 1_00_00),
210+
'client' => $context->clientName,
211+
'backend' => $context->backendName,
212+
...$logContext,
213+
]
214+
);
204215

205216
if (false === (bool)ag($context->options, Options::DRY_RUN, false)) {
206217
$queue->add(

src/Backends/Emby/EmbyClient.php

+35-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Backends\Common\ClientInterface as iClient;
99
use App\Backends\Common\Context;
1010
use App\Backends\Common\GuidInterface as iGuid;
11+
use App\Backends\Common\Response;
1112
use App\Backends\Emby\Action\Backup;
1213
use App\Backends\Emby\Action\Export;
1314
use App\Backends\Emby\Action\GetIdentifier;
@@ -42,8 +43,9 @@
4243
/**
4344
* Class EmbyClient
4445
*
45-
* This class represents a client for the Emby backend.
46-
* It implements the iClient interface.
46+
* This class is responsible for facilitating communication with Emby Server backend.
47+
*
48+
* @implements iClient
4749
*/
4850
class EmbyClient implements iClient
4951
{
@@ -200,10 +202,7 @@ public function parseWebhook(ServerRequestInterface $request): iState
200202
}
201203

202204
if (false === $response->isSuccessful()) {
203-
throw new HttpException(
204-
ag($response->extra, 'message', fn() => $response->error->format()),
205-
ag($response->extra, 'http_code', 400),
206-
);
205+
$this->throwError($response, HttpException::class, ag($response->extra, 'http_code', 400));
207206
}
208207

209208
return $response->response;
@@ -229,7 +228,7 @@ public function pull(iImport $mapper, iDate|null $after = null): array
229228
}
230229

231230
if (false === $response->isSuccessful()) {
232-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
231+
$this->throwError($response);
233232
}
234233

235234
return $response->response;
@@ -252,7 +251,7 @@ public function backup(iImport $mapper, SplFileObject|null $writer = null, array
252251
}
253252

254253
if (false === $response->isSuccessful()) {
255-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
254+
$this->throwError($response);
256255
}
257256

258257
return $response->response;
@@ -279,7 +278,7 @@ public function export(iImport $mapper, QueueRequests $queue, iDate|null $after
279278
}
280279

281280
if (false === $response->isSuccessful()) {
282-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
281+
$this->throwError($response);
283282
}
284283

285284
return $response->response;
@@ -302,7 +301,7 @@ public function push(array $entities, QueueRequests $queue, iDate|null $after =
302301
}
303302

304303
if (false === $response->isSuccessful()) {
305-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
304+
$this->throwError($response);
306305
}
307306

308307
return [];
@@ -326,7 +325,7 @@ public function progress(array $entities, QueueRequests $queue, iDate|null $afte
326325
}
327326

328327
if (false === $response->isSuccessful()) {
329-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
328+
$this->throwError($response);
330329
}
331330

332331
return [];
@@ -349,7 +348,7 @@ public function search(string $query, int $limit = 25, array $opts = []): array
349348
}
350349

351350
if (false === $response->isSuccessful()) {
352-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
351+
$this->throwError($response);
353352
}
354353

355354
return $response->response;
@@ -367,7 +366,7 @@ public function searchId(string|int $id, array $opts = []): array
367366
}
368367

369368
if (false === $response->isSuccessful()) {
370-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
369+
$this->throwError($response);
371370
}
372371

373372
return $response->response;
@@ -385,7 +384,7 @@ public function getMetadata(string|int $id, array $opts = []): array
385384
);
386385

387386
if (false === $response->isSuccessful()) {
388-
throw new RuntimeException(message: $response->error->format(), previous: $response->error->previous);
387+
$this->throwError($response);
389388
}
390389

391390
return $response->response;
@@ -403,7 +402,7 @@ public function getLibrary(string|int $id, array $opts = []): array
403402
}
404403

405404
if (false === $response->isSuccessful()) {
406-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
405+
$this->throwError($response);
407406
}
408407

409408
return $response->response;
@@ -439,9 +438,7 @@ public function getUsersList(array $opts = []): array
439438
$this->logger->log($response->error->level(), $response->error->message, $response->error->context);
440439
}
441440

442-
throw new RuntimeException(
443-
ag($response->extra, 'message', fn() => $response->error->format())
444-
);
441+
$this->throwError($response);
445442
}
446443

447444
return $response->response;
@@ -475,7 +472,7 @@ public function listLibraries(array $opts = []): array
475472
}
476473

477474
if (false === $response->isSuccessful()) {
478-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
475+
$this->throwError($response);
479476
}
480477

481478
return $response->response;
@@ -493,7 +490,7 @@ public function getInfo(array $opts = []): array
493490
}
494491

495492
if (false === $response->isSuccessful()) {
496-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
493+
$this->throwError($response);
497494
}
498495

499496
return $response->response;
@@ -511,7 +508,7 @@ public function getVersion(array $opts = []): string
511508
}
512509

513510
if (false === $response->isSuccessful()) {
514-
throw new RuntimeException(ag($response->extra, 'message', fn() => $response->error->format()));
511+
$this->throwError($response);
515512
}
516513

517514
return $response->response;
@@ -524,4 +521,21 @@ public static function manage(array $backend, array $opts = []): array
524521
{
525522
return Container::get(EmbyManage::class)->manage(backend: $backend, opts: $opts);
526523
}
524+
525+
/**
526+
* Throws an exception with the specified message and previous exception.
527+
*
528+
* @template T
529+
* @param Response $response The response object containing the error details.
530+
* @param class-string<T> $className The exception class name.
531+
* @param int $code The exception code.
532+
*/
533+
private function throwError(Response $response, string $className = RuntimeException::class, int $code = 0): void
534+
{
535+
throw new $className(
536+
message: ag($response->extra, 'message', fn() => $response->error->format()),
537+
code: $code,
538+
previous: $response->error->previous
539+
);
540+
}
527541
}

0 commit comments

Comments
 (0)