From 1752e5504265760c803f536ed37da9f2ca8df70b Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Thu, 7 Jul 2022 17:54:56 +0300 Subject: [PATCH] Reformatted code according to PSR12. --- src/Backends/Common/CommonTrait.php | 36 +++--- src/Backends/Emby/Action/ParseWebhook.php | 82 ++++++------- src/Backends/Jellyfin/Action/Backup.php | 6 +- src/Backends/Jellyfin/Action/Export.php | 6 +- .../Jellyfin/Action/GetIdentifier.php | 28 ++--- .../Jellyfin/Action/GetLibrariesList.php | 38 +++--- src/Backends/Jellyfin/Action/GetLibrary.php | 70 +++++------ src/Backends/Jellyfin/Action/GetMetaData.php | 26 ++-- src/Backends/Jellyfin/Action/GetUsersList.php | 22 ++-- src/Backends/Jellyfin/Action/Import.php | 77 ++++++------ src/Backends/Jellyfin/Action/ParseWebhook.php | 82 ++++++------- src/Backends/Jellyfin/Action/SearchQuery.php | 24 ++-- src/Backends/Jellyfin/JellyfinActionTrait.php | 10 +- src/Backends/Plex/Action/Backup.php | 6 +- src/Backends/Plex/Action/Export.php | 6 +- src/Backends/Plex/Action/GetIdentifier.php | 30 ++--- src/Backends/Plex/Action/GetLibrariesList.php | 38 +++--- src/Backends/Plex/Action/GetLibrary.php | 116 +++++++++--------- src/Backends/Plex/Action/GetMetaData.php | 28 ++--- src/Backends/Plex/Action/GetUsersList.php | 104 ++++++++-------- src/Backends/Plex/Action/Import.php | 69 ++++++----- src/Backends/Plex/Action/ParseWebhook.php | 82 ++++++------- src/Backends/Plex/Action/SearchQuery.php | 24 ++-- src/Backends/Plex/PlexActionTrait.php | 4 +- src/Libs/Database/PDO/PDODataMigration.php | 110 ++++++++--------- src/Libs/Entity/StateEntity.php | 16 +-- src/Libs/Servers/JellyfinServer.php | 88 ++++++------- src/Libs/Servers/PlexServer.php | 80 ++++++------ 28 files changed, 655 insertions(+), 653 deletions(-) diff --git a/src/Backends/Common/CommonTrait.php b/src/Backends/Common/CommonTrait.php index 7467e624..f975d6bb 100644 --- a/src/Backends/Common/CommonTrait.php +++ b/src/Backends/Common/CommonTrait.php @@ -30,24 +30,24 @@ trait CommonTrait } catch (Throwable $e) { return new Response( status: false, - error: new Error( - message: 'Unhandled exception was thrown in [%(client): %(backend)] %(action). %(message)', - context: [ - 'action' => $action ?? 'context', - 'backend' => $context->backendName, - 'client' => $context->clientName, - 'message' => $e->getMessage(), - 'exception' => [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'kind' => get_class($e), - 'message' => $e->getMessage(), - 'trace' => $context->trace ? $e->getTrace() : [], - ] - ], - level: Levels::WARNING, - previous: $e - ) + error: new Error( + message: 'Unhandled exception was thrown in [%(client): %(backend)] %(action). %(message)', + context: [ + 'action' => $action ?? 'context', + 'backend' => $context->backendName, + 'client' => $context->clientName, + 'message' => $e->getMessage(), + 'exception' => [ + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'kind' => get_class($e), + 'message' => $e->getMessage(), + 'trace' => $context->trace ? $e->getTrace() : [], + ] + ], + level: Levels::WARNING, + previous: $e + ) ); } } diff --git a/src/Backends/Emby/Action/ParseWebhook.php b/src/Backends/Emby/Action/ParseWebhook.php index 73ef181c..4705c22d 100644 --- a/src/Backends/Emby/Action/ParseWebhook.php +++ b/src/Backends/Emby/Action/ParseWebhook.php @@ -154,7 +154,7 @@ final class ParseWebhook iState::COLUMN_META_DATA => [ $context->backendName => [ iState::COLUMN_GUIDS => $guid->parse( - guids: ag($json, 'Item.ProviderIds', []), + guids: ag($json, 'Item.ProviderIds', []), context: $logContext ), ] @@ -182,31 +182,31 @@ final class ParseWebhook $entity = $this->createEntity( context: $context, - guid: $guid, - item: $obj, - opts: ['override' => $fields, Options::DISABLE_GUID => $disableGuid], + guid: $guid, + item: $obj, + opts: ['override' => $fields, Options::DISABLE_GUID => $disableGuid], )->setIsTainted(isTainted: true === in_array($event, self::WEBHOOK_TAINTED_EVENTS)); if (false === $entity->hasGuids() && false === $entity->hasRelativeGuid()) { return new Response( status: false, - error: new Error( - message: 'Ignoring [%(backend)] [%(title)] webhook event. No valid/supported external ids.', - context: [ - 'backend' => $context->backendName, - 'title' => $entity->getName(), - 'context' => [ - 'attributes' => $request->getAttributes(), - 'parsed' => $entity->getAll(), - 'payload' => $request->getParsedBody(), - ], - ], - level: Levels::ERROR - ), - extra: [ - 'http_code' => 200, - 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + error: new Error( + message: 'Ignoring [%(backend)] [%(title)] webhook event. No valid/supported external ids.', + context: [ + 'backend' => $context->backendName, + 'title' => $entity->getName(), + 'context' => [ + 'attributes' => $request->getAttributes(), + 'parsed' => $entity->getAll(), + 'payload' => $request->getParsedBody(), ], + ], + level: Levels::ERROR + ), + extra: [ + 'http_code' => 200, + 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + ], ); } @@ -214,28 +214,28 @@ final class ParseWebhook } catch (Throwable $e) { return new Response( status: false, - error: new Error( - message: 'Unhandled exception was thrown during [%(backend)] webhook event parsing.', - context: [ - 'backend' => $context->backendName, - 'exception' => [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'kind' => get_class($e), - 'message' => $e->getMessage(), - ], - 'context' => [ - 'attributes' => $request->getAttributes(), - 'payload' => $request->getParsedBody(), - ], - 'trace' => $context->trace ? $e->getTrace() : [], - ], - level: Levels::ERROR - ), - extra: [ - 'http_code' => 200, - 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + error: new Error( + message: 'Unhandled exception was thrown during [%(backend)] webhook event parsing.', + context: [ + 'backend' => $context->backendName, + 'exception' => [ + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'kind' => get_class($e), + 'message' => $e->getMessage(), ], + 'context' => [ + 'attributes' => $request->getAttributes(), + 'payload' => $request->getParsedBody(), + ], + 'trace' => $context->trace ? $e->getTrace() : [], + ], + level: Levels::ERROR + ), + extra: [ + 'http_code' => 200, + 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + ], ); } } diff --git a/src/Backends/Jellyfin/Action/Backup.php b/src/Backends/Jellyfin/Action/Backup.php index 50c5b023..3647e88f 100644 --- a/src/Backends/Jellyfin/Action/Backup.php +++ b/src/Backends/Jellyfin/Action/Backup.php @@ -67,9 +67,9 @@ class Backup extends Import $entity = $this->createEntity( context: $context, - guid: $guid, - item: $item, - opts: $opts + ['library' => ag($logContext, 'library.id')] + guid: $guid, + item: $item, + opts: $opts + ['library' => ag($logContext, 'library.id')] ); $arr = [ iState::COLUMN_TYPE => $entity->type, diff --git a/src/Backends/Jellyfin/Action/Export.php b/src/Backends/Jellyfin/Action/Export.php index 5763b2e6..f25adce4 100644 --- a/src/Backends/Jellyfin/Action/Export.php +++ b/src/Backends/Jellyfin/Action/Export.php @@ -85,9 +85,9 @@ class Export extends Import $rItem = $this->createEntity( context: $context, - guid: $guid, - item: $item, - opts: $opts + ['library' => ag($logContext, 'library.id')] + guid: $guid, + item: $item, + opts: $opts + ['library' => ag($logContext, 'library.id')] ); if (!$rItem->hasGuids() && !$rItem->hasRelativeGuid()) { diff --git a/src/Backends/Jellyfin/Action/GetIdentifier.php b/src/Backends/Jellyfin/Action/GetIdentifier.php index 8419051e..36c8c562 100644 --- a/src/Backends/Jellyfin/Action/GetIdentifier.php +++ b/src/Backends/Jellyfin/Action/GetIdentifier.php @@ -56,23 +56,23 @@ class GetIdentifier if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] %(action) returned with unexpected [%(status_code)] status code.', - context: [ - 'action' => $this->action, - 'client' => $context->clientName, - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::WARNING - ) + error: new Error( + message: 'Request for [%(backend)] %(action) returned with unexpected [%(status_code)] status code.', + context: [ + 'action' => $this->action, + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::WARNING + ) ); } $item = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if (true === $context->trace) { @@ -85,11 +85,11 @@ class GetIdentifier } return new Response( - status: true, + status: true, response: ag($item, 'Id', null) ); }, - action: $this->action, + action: $this->action, ); } } diff --git a/src/Backends/Jellyfin/Action/GetLibrariesList.php b/src/Backends/Jellyfin/Action/GetLibrariesList.php index b98a0b2d..f745f814 100644 --- a/src/Backends/Jellyfin/Action/GetLibrariesList.php +++ b/src/Backends/Jellyfin/Action/GetLibrariesList.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Jellyfin\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Backends\Jellyfin\JellyfinClient; use App\Libs\Options; use JsonException; @@ -55,14 +55,14 @@ class GetLibrariesList if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] libraries returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] libraries returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } @@ -87,16 +87,16 @@ class GetLibrariesList if (empty($listDirs)) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] libraries returned empty list.', - context: [ - 'backend' => $context->backendName, - 'response' => [ - 'body' => $json - ], - ], - level: Levels::WARNING - ), + error: new Error( + message: 'Request for [%(backend)] libraries returned empty list.', + context: [ + 'backend' => $context->backendName, + 'response' => [ + 'body' => $json + ], + ], + level: Levels::WARNING + ), ); } diff --git a/src/Backends/Jellyfin/Action/GetLibrary.php b/src/Backends/Jellyfin/Action/GetLibrary.php index fd158855..65214ddc 100644 --- a/src/Backends/Jellyfin/Action/GetLibrary.php +++ b/src/Backends/Jellyfin/Action/GetLibrary.php @@ -5,11 +5,11 @@ declare(strict_types=1); namespace App\Backends\Jellyfin\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\GuidInterface as iGuid; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Backends\Jellyfin\JellyfinActionTrait; use App\Backends\Jellyfin\JellyfinClient; use App\Libs\Options; @@ -54,17 +54,17 @@ class GetLibrary if (null === ($section = ag($libraries, $id))) { return new Response( status: false, - error: new Error( - message: 'No Library with id [%(id)] found in [%(backend)] response.', - context: [ - 'id' => $id, - 'backend' => $context->backendName, - 'response' => [ - 'body' => $libraries - ], - ], - level: Levels::WARNING - ), + error: new Error( + message: 'No Library with id [%(id)] found in [%(backend)] response.', + context: [ + 'id' => $id, + 'backend' => $context->backendName, + 'response' => [ + 'body' => $libraries + ], + ], + level: Levels::WARNING + ), ); } @@ -84,14 +84,14 @@ class GetLibrary )) { return new Response( status: false, - error: new Error( - message: 'The Requested [%(backend)] Library [%(library.id): %(library.title)] returned with unsupported type [%(library.type)].', - context: [ - 'backend' => $context->backendName, - ...$logContext, - ], - level: Levels::WARNING - ), + error: new Error( + message: 'The Requested [%(backend)] Library [%(library.id): %(library.title)] returned with unsupported type [%(library.type)].', + context: [ + 'backend' => $context->backendName, + ...$logContext, + ], + level: Levels::WARNING + ), ); } @@ -120,26 +120,26 @@ class GetLibrary if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] library [%(library.title)] returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ...$logContext, - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] library [%(library.title)] returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ...$logContext, + ], + level: Levels::ERROR + ), ); } $it = Items::fromIterable( iterable: httpClientChunks($this->http->stream($response)), - options: [ - 'pointer' => '/Items', - 'decoder' => new ErrorWrappingDecoder( - new ExtJsonDecoder(assoc: true, options: JSON_INVALID_UTF8_IGNORE) - ) - ] + options: [ + 'pointer' => '/Items', + 'decoder' => new ErrorWrappingDecoder( + new ExtJsonDecoder(assoc: true, options: JSON_INVALID_UTF8_IGNORE) + ) + ] ); $list = []; diff --git a/src/Backends/Jellyfin/Action/GetMetaData.php b/src/Backends/Jellyfin/Action/GetMetaData.php index 24cea40a..fb2db973 100644 --- a/src/Backends/Jellyfin/Action/GetMetaData.php +++ b/src/Backends/Jellyfin/Action/GetMetaData.php @@ -81,29 +81,29 @@ class GetMetaData if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] item [%(id)] returned with unexpected [%(status_code)] status code.', - context: [ - 'id' => $id, - 'client' => $context->clientName, - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ] - ) + error: new Error( + message: 'Request for [%(backend)] item [%(id)] returned with unexpected [%(status_code)] status code.', + context: [ + 'id' => $id, + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ] + ) ); } $item = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if (null !== $cacheKey) { $this->cache->set( - key: $cacheKey, + key: $cacheKey, value: $item, - ttl: $opts[Options::CACHE_TTL] ?? new DateInterval('PT5M') + ttl: $opts[Options::CACHE_TTL] ?? new DateInterval('PT5M') ); } diff --git a/src/Backends/Jellyfin/Action/GetUsersList.php b/src/Backends/Jellyfin/Action/GetUsersList.php index 4be0e0a0..40474336 100644 --- a/src/Backends/Jellyfin/Action/GetUsersList.php +++ b/src/Backends/Jellyfin/Action/GetUsersList.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Jellyfin\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Libs\Options; use JsonException; use Psr\Log\LoggerInterface; @@ -56,21 +56,21 @@ class GetUsersList if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] users list returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] users list returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if ($context->trace) { diff --git a/src/Backends/Jellyfin/Action/Import.php b/src/Backends/Jellyfin/Action/Import.php index 42a0c80e..a19957d6 100644 --- a/src/Backends/Jellyfin/Action/Import.php +++ b/src/Backends/Jellyfin/Action/Import.php @@ -22,18 +22,19 @@ use JsonMachine\Items; use JsonMachine\JsonDecoder\DecodingError; use JsonMachine\JsonDecoder\ErrorWrappingDecoder; use JsonMachine\JsonDecoder\ExtJsonDecoder; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerInterface as iLogger; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; -use Symfony\Contracts\HttpClient\HttpClientInterface; +use Symfony\Contracts\HttpClient\HttpClientInterface as iHttp; use Symfony\Contracts\HttpClient\ResponseInterface as iResponse; use Throwable; class Import { - use CommonTrait, JellyfinActionTrait; + use CommonTrait; + use JellyfinActionTrait; - public function __construct(protected HttpClientInterface $http, protected LoggerInterface $logger) + public function __construct(protected iHttp $http, protected iLogger $logger) { } @@ -56,15 +57,15 @@ class Import return $this->tryResponse($context, fn() => $this->getLibraries( context: $context, handle: fn(array $logContext = []) => fn(iResponse $response) => $this->handle( - context: $context, - response: $response, + context: $context, + response: $response, callback: fn(array $item, array $logContext = []) => $this->process( - context: $context, - guid: $guid, - mapper: $mapper, - item: $item, + context: $context, + guid: $guid, + mapper: $mapper, + item: $item, logContext: $logContext, - opts: $opts + ['after' => $after], + opts: $opts + ['after' => $after], ), logContext: $logContext ), @@ -109,9 +110,9 @@ class Import } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); $listDirs = ag($json, 'Items', []); @@ -349,15 +350,15 @@ class Import try { $it = Items::fromIterable( iterable: httpClientChunks($this->http->stream($response)), - options: [ - 'pointer' => '/Items', - 'decoder' => new ErrorWrappingDecoder( - innerDecoder: new ExtJsonDecoder( - assoc: true, - options: JSON_INVALID_UTF8_IGNORE - ) - ) - ] + options: [ + 'pointer' => '/Items', + 'decoder' => new ErrorWrappingDecoder( + innerDecoder: new ExtJsonDecoder( + assoc: true, + options: JSON_INVALID_UTF8_IGNORE + ) + ) + ] ); foreach ($it as $entity) { @@ -453,9 +454,9 @@ class Import Guid::fromArray( payload: $guid->get(guids: $providersId, context: $logContext), context: [ - 'backend' => $context->backendName, - ...$logContext, - ] + 'backend' => $context->backendName, + ...$logContext, + ] )->getAll() ); } @@ -524,19 +525,19 @@ class Import $entity = $this->createEntity( context: $context, - guid: $guid, - item: $item, - opts: $opts + [ - 'library' => ag($logContext, 'library.id'), - 'override' => [ - iState::COLUMN_EXTRA => [ - $context->backendName => [ - iState::COLUMN_EXTRA_EVENT => 'task.import', - iState::COLUMN_EXTRA_DATE => makeDate('now'), - ], - ], - ] - ], + guid: $guid, + item: $item, + opts: $opts + [ + 'library' => ag($logContext, 'library.id'), + 'override' => [ + iState::COLUMN_EXTRA => [ + $context->backendName => [ + iState::COLUMN_EXTRA_EVENT => 'task.import', + iState::COLUMN_EXTRA_DATE => makeDate('now'), + ], + ], + ] + ], ); if (false === $entity->hasGuids() && false === $entity->hasRelativeGuid()) { diff --git a/src/Backends/Jellyfin/Action/ParseWebhook.php b/src/Backends/Jellyfin/Action/ParseWebhook.php index b05aeefb..fbbf8541 100644 --- a/src/Backends/Jellyfin/Action/ParseWebhook.php +++ b/src/Backends/Jellyfin/Action/ParseWebhook.php @@ -143,7 +143,7 @@ final class ParseWebhook $context->backendName => [ iState::COLUMN_WATCHED => true === $isPlayed ? '1' : '0', iState::COLUMN_GUIDS => $guid->parse( - guids: $providersId, + guids: $providersId, context: $logContext ), ] @@ -170,31 +170,31 @@ final class ParseWebhook $entity = $this->createEntity( context: $context, - guid: $guid, - item: $obj, - opts: ['override' => $fields, Options::DISABLE_GUID => $disableGuid], + guid: $guid, + item: $obj, + opts: ['override' => $fields, Options::DISABLE_GUID => $disableGuid], )->setIsTainted(isTainted: true === in_array($event, self::WEBHOOK_TAINTED_EVENTS)); if (false === $entity->hasGuids() && false === $entity->hasRelativeGuid()) { return new Response( status: false, - error: new Error( - message: 'Ignoring [%(backend)] [%(title)] webhook event. No valid/supported external ids.', - context: [ - 'backend' => $context->backendName, - 'title' => $entity->getName(), - 'context' => [ - 'attributes' => $request->getAttributes(), - 'parsed' => $entity->getAll(), - 'payload' => $request->getParsedBody(), - ], - ], - level: Levels::ERROR - ), - extra: [ - 'http_code' => 200, - 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + error: new Error( + message: 'Ignoring [%(backend)] [%(title)] webhook event. No valid/supported external ids.', + context: [ + 'backend' => $context->backendName, + 'title' => $entity->getName(), + 'context' => [ + 'attributes' => $request->getAttributes(), + 'parsed' => $entity->getAll(), + 'payload' => $request->getParsedBody(), ], + ], + level: Levels::ERROR + ), + extra: [ + 'http_code' => 200, + 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + ], ); } @@ -202,28 +202,28 @@ final class ParseWebhook } catch (Throwable $e) { return new Response( status: false, - error: new Error( - message: 'Unhandled exception was thrown during [%(backend)] webhook event parsing.', - context: [ - 'backend' => $context->backendName, - 'exception' => [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'kind' => get_class($e), - 'message' => $e->getMessage(), - 'trace' => $context->trace ? $e->getTrace() : [], - ], - 'context' => [ - 'attributes' => $request->getAttributes(), - 'payload' => $request->getParsedBody(), - ], - ], - level: Levels::ERROR - ), - extra: [ - 'http_code' => 200, - 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + error: new Error( + message: 'Unhandled exception was thrown during [%(backend)] webhook event parsing.', + context: [ + 'backend' => $context->backendName, + 'exception' => [ + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'kind' => get_class($e), + 'message' => $e->getMessage(), + 'trace' => $context->trace ? $e->getTrace() : [], ], + 'context' => [ + 'attributes' => $request->getAttributes(), + 'payload' => $request->getParsedBody(), + ], + ], + level: Levels::ERROR + ), + extra: [ + 'http_code' => 200, + 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + ], ); } } diff --git a/src/Backends/Jellyfin/Action/SearchQuery.php b/src/Backends/Jellyfin/Action/SearchQuery.php index bebc2943..1449afb0 100644 --- a/src/Backends/Jellyfin/Action/SearchQuery.php +++ b/src/Backends/Jellyfin/Action/SearchQuery.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Jellyfin\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Backends\Jellyfin\JellyfinClient; use App\Libs\Options; use JsonException; @@ -79,22 +79,22 @@ class SearchQuery if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Search request for [%(query)] in [%(backend)] returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'query' => $query, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Search request for [%(query)] in [%(backend)] returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'query' => $query, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if ($context->trace) { diff --git a/src/Backends/Jellyfin/JellyfinActionTrait.php b/src/Backends/Jellyfin/JellyfinActionTrait.php index 16fa3cf9..8b4e88d2 100644 --- a/src/Backends/Jellyfin/JellyfinActionTrait.php +++ b/src/Backends/Jellyfin/JellyfinActionTrait.php @@ -123,8 +123,8 @@ trait JellyfinActionTrait if (null !== $parentId) { $builder[iState::COLUMN_PARENT] = $this->getEpisodeParent( context: $context, - guid: $guid, - id: $parentId + guid: $guid, + id: $parentId ); $metadata[iState::COLUMN_PARENT] = $builder[iState::COLUMN_PARENT]; } @@ -219,9 +219,9 @@ trait JellyfinActionTrait Guid::fromArray( payload: $guid->get(guids: $providersId, context: $logContext), context: [ - 'backend' => $context->backendName, - ...$logContext, - ] + 'backend' => $context->backendName, + ...$logContext, + ] )->getAll() ); diff --git a/src/Backends/Plex/Action/Backup.php b/src/Backends/Plex/Action/Backup.php index 49a68794..43147eb2 100644 --- a/src/Backends/Plex/Action/Backup.php +++ b/src/Backends/Plex/Action/Backup.php @@ -70,9 +70,9 @@ final class Backup extends Import $entity = $this->createEntity( context: $context, - guid: $guid, - item: $item, - opts: $opts + guid: $guid, + item: $item, + opts: $opts ); $arr = [ diff --git a/src/Backends/Plex/Action/Export.php b/src/Backends/Plex/Action/Export.php index 2207908f..432c8307 100644 --- a/src/Backends/Plex/Action/Export.php +++ b/src/Backends/Plex/Action/Export.php @@ -88,9 +88,9 @@ final class Export extends Import $rItem = $this->createEntity( context: $context, - guid: $guid, - item: $item, - opts: $opts + guid: $guid, + item: $item, + opts: $opts ); if (!$rItem->hasGuids() && !$rItem->hasRelativeGuid()) { diff --git a/src/Backends/Plex/Action/GetIdentifier.php b/src/Backends/Plex/Action/GetIdentifier.php index e93e4053..60e0e28d 100644 --- a/src/Backends/Plex/Action/GetIdentifier.php +++ b/src/Backends/Plex/Action/GetIdentifier.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Plex\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use Psr\Log\LoggerInterface; use Psr\SimpleCache\CacheInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -56,25 +56,25 @@ final class GetIdentifier if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] %(action) returned with unexpected [%(status_code)] status code.', - context: [ - 'action' => $this->action, - 'client' => $context->clientName, - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::WARNING - ) + error: new Error( + message: 'Request for [%(backend)] %(action) returned with unexpected [%(status_code)] status code.', + context: [ + 'action' => $this->action, + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::WARNING + ) ); } $content = $response->getContent(); $item = json_decode( - json: $content, + json: $content, associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if (true === $context->trace) { @@ -87,11 +87,11 @@ final class GetIdentifier } return new Response( - status: true, + status: true, response: ag($item, 'MediaContainer.machineIdentifier', null) ); }, - action: $this->action + action: $this->action ); } } diff --git a/src/Backends/Plex/Action/GetLibrariesList.php b/src/Backends/Plex/Action/GetLibrariesList.php index a9e739f5..907801f7 100644 --- a/src/Backends/Plex/Action/GetLibrariesList.php +++ b/src/Backends/Plex/Action/GetLibrariesList.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Plex\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Backends\Plex\PlexClient; use App\Libs\Options; use JsonException; @@ -55,14 +55,14 @@ final class GetLibrariesList if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] libraries returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] libraries returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } @@ -87,16 +87,16 @@ final class GetLibrariesList if (empty($listDirs)) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] libraries returned empty list.', - context: [ - 'backend' => $context->backendName, - 'response' => [ - 'body' => $json - ], - ], - level: Levels::WARNING - ), + error: new Error( + message: 'Request for [%(backend)] libraries returned empty list.', + context: [ + 'backend' => $context->backendName, + 'response' => [ + 'body' => $json + ], + ], + level: Levels::WARNING + ), ); } diff --git a/src/Backends/Plex/Action/GetLibrary.php b/src/Backends/Plex/Action/GetLibrary.php index 2c53498e..82f60c8d 100644 --- a/src/Backends/Plex/Action/GetLibrary.php +++ b/src/Backends/Plex/Action/GetLibrary.php @@ -5,11 +5,11 @@ declare(strict_types=1); namespace App\Backends\Plex\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\GuidInterface as iGuid; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Backends\Plex\PlexActionTrait; use App\Backends\Plex\PlexClient; use App\Libs\Options; @@ -59,17 +59,17 @@ final class GetLibrary if (null === ($section = ag($libraries, $id))) { return new Response( status: false, - error: new Error( - message: 'No Library with id [%(id)] found in [%(backend)] response.', - context: [ - 'id' => $id, - 'backend' => $context->backendName, - 'response' => [ - 'body' => $libraries - ], - ], - level: Levels::WARNING - ), + error: new Error( + message: 'No Library with id [%(id)] found in [%(backend)] response.', + context: [ + 'id' => $id, + 'backend' => $context->backendName, + 'response' => [ + 'body' => $libraries + ], + ], + level: Levels::WARNING + ), ); } @@ -86,14 +86,14 @@ final class GetLibrary if (true !== in_array(ag($logContext, 'library.type'), [PlexClient::TYPE_MOVIE, PlexClient::TYPE_SHOW])) { return new Response( status: false, - error: new Error( - message: 'The Requested [%(backend)] Library [%(library.title)] returned with unsupported type [%(library.type)].', - context: [ - 'backend' => $context->backendName, - ...$logContext, - ], - level: Levels::WARNING - ), + error: new Error( + message: 'The Requested [%(backend)] Library [%(library.title)] returned with unsupported type [%(library.type)].', + context: [ + 'backend' => $context->backendName, + ...$logContext, + ], + level: Levels::WARNING + ), ); } @@ -118,31 +118,31 @@ final class GetLibrary if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] library [%(library.title)] returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ...$logContext, - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] library [%(library.title)] returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ...$logContext, + ], + level: Levels::ERROR + ), ); } $it = Items::fromIterable( iterable: httpClientChunks( - stream: $this->http->stream($response) - ), - options: [ - 'pointer' => '/MediaContainer/Metadata', - 'decoder' => new ErrorWrappingDecoder( - innerDecoder: new ExtJsonDecoder( - assoc: true, - options: JSON_INVALID_UTF8_IGNORE - ) - ) - ] + stream: $this->http->stream($response) + ), + options: [ + 'pointer' => '/MediaContainer/Metadata', + 'decoder' => new ErrorWrappingDecoder( + innerDecoder: new ExtJsonDecoder( + assoc: true, + options: JSON_INVALID_UTF8_IGNORE + ) + ) + ] ); $list = $requests = []; @@ -220,9 +220,9 @@ final class GetLibrary } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); $list[] = $this->process( @@ -235,22 +235,22 @@ final class GetLibrary } catch (JsonException|HttpExceptionInterface $e) { return new Response( status: false, - error: new Error( - message: 'Unhandled exception was thrown during request for [%(backend)] %(item.type) [%(item.title)] metadata.', - context: [ - 'backend' => $context->backendName, - 'exception' => [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'kind' => get_class($e), - 'message' => $e->getMessage(), - 'trace' => $context->trace ? $e->getTrace() : [], - ], - ...$requestContext, - ], - level: Levels::WARNING, - previous: $e - ) + error: new Error( + message: 'Unhandled exception was thrown during request for [%(backend)] %(item.type) [%(item.title)] metadata.', + context: [ + 'backend' => $context->backendName, + 'exception' => [ + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'kind' => get_class($e), + 'message' => $e->getMessage(), + 'trace' => $context->trace ? $e->getTrace() : [], + ], + ...$requestContext, + ], + level: Levels::WARNING, + previous: $e + ) ); } } diff --git a/src/Backends/Plex/Action/GetMetaData.php b/src/Backends/Plex/Action/GetMetaData.php index 55a57413..f41600d5 100644 --- a/src/Backends/Plex/Action/GetMetaData.php +++ b/src/Backends/Plex/Action/GetMetaData.php @@ -5,9 +5,9 @@ declare(strict_types=1); namespace App\Backends\Plex\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Libs\Options; use DateInterval; use Psr\Log\LoggerInterface; @@ -68,31 +68,31 @@ final class GetMetaData if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] item [%(id)] returned with unexpected [%(status_code)] status code.', - context: [ - 'id' => $id, - 'client' => $context->clientName, - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ] - ) + error: new Error( + message: 'Request for [%(backend)] item [%(id)] returned with unexpected [%(status_code)] status code.', + context: [ + 'id' => $id, + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ] + ) ); } $content = $response->getContent(); $item = json_decode( - json: $content, + json: $content, associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if (null !== $cacheKey) { $this->cache->set( - key: $cacheKey, + key: $cacheKey, value: $item, - ttl: $opts[Options::CACHE_TTL] ?? new DateInterval('PT5M') + ttl: $opts[Options::CACHE_TTL] ?? new DateInterval('PT5M') ); } diff --git a/src/Backends/Plex/Action/GetUsersList.php b/src/Backends/Plex/Action/GetUsersList.php index f592d632..9035228d 100644 --- a/src/Backends/Plex/Action/GetUsersList.php +++ b/src/Backends/Plex/Action/GetUsersList.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Plex\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Libs\Container; use App\Libs\Options; use JsonException; @@ -66,21 +66,21 @@ final class GetUsersList if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] users list returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] users list returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if ($context->trace) { @@ -115,8 +115,8 @@ final class GetUsersList if (true === (bool)ag($opts, 'tokens')) { $tokenRequest = $this->getUserToken( - context: $context, - userId: ag($user, 'uuid'), + context: $context, + userId: ag($user, 'uuid'), username: ag($data, 'name'), ); @@ -175,23 +175,23 @@ final class GetUsersList if (201 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Request for [%(backend)] user [%(username)] temporary access token responded with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'username' => $username, - 'user_id' => $userId, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Request for [%(backend)] user [%(username)] temporary access token responded with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'username' => $username, + 'user_id' => $userId, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if ($context->trace) { @@ -233,9 +233,9 @@ final class GetUsersList ]); $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if ($context->trace) { @@ -257,35 +257,35 @@ final class GetUsersList return new Response( status: false, - error: new Error( - message: 'No permanent access token found in [%(backend)] user [%(username)] response.', - context: [ - 'backend' => $context->backendName, - 'username' => $username, - 'user_id' => $userId, - ], - level: Levels::ERROR - ), + error: new Error( + message: 'No permanent access token found in [%(backend)] user [%(username)] response.', + context: [ + 'backend' => $context->backendName, + 'username' => $username, + 'user_id' => $userId, + ], + level: Levels::ERROR + ), ); } catch (Throwable $e) { return new Response( status: false, - error: new Error( - message: 'Unhandled exception was thrown during request for [%(backend)] [%(username)] access token.', - context: [ - 'backend' => $context->backendName, - 'username' => $username, - 'user_id' => $userId, - 'exception' => [ - 'file' => after($e->getFile(), ROOT_PATH), - 'line' => $e->getLine(), - 'kind' => get_class($e), - 'message' => $e->getMessage(), - 'trace' => $context->trace ? $e->getTrace() : [], - ], - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Unhandled exception was thrown during request for [%(backend)] [%(username)] access token.', + context: [ + 'backend' => $context->backendName, + 'username' => $username, + 'user_id' => $userId, + 'exception' => [ + 'file' => after($e->getFile(), ROOT_PATH), + 'line' => $e->getLine(), + 'kind' => get_class($e), + 'message' => $e->getMessage(), + 'trace' => $context->trace ? $e->getTrace() : [], + ], + ], + level: Levels::ERROR + ), ); } } diff --git a/src/Backends/Plex/Action/Import.php b/src/Backends/Plex/Action/Import.php index 5a314316..a7081e5b 100644 --- a/src/Backends/Plex/Action/Import.php +++ b/src/Backends/Plex/Action/Import.php @@ -22,18 +22,19 @@ use JsonMachine\Items; use JsonMachine\JsonDecoder\DecodingError; use JsonMachine\JsonDecoder\ErrorWrappingDecoder; use JsonMachine\JsonDecoder\ExtJsonDecoder; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerInterface as iLogger; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; -use Symfony\Contracts\HttpClient\HttpClientInterface; +use Symfony\Contracts\HttpClient\HttpClientInterface as iHttp; use Symfony\Contracts\HttpClient\ResponseInterface as iResponse; use Throwable; class Import { - use CommonTrait, PlexActionTrait; + use CommonTrait; + use PlexActionTrait; - public function __construct(protected HttpClientInterface $http, protected LoggerInterface $logger) + public function __construct(protected iHttp $http, protected iLogger $logger) { } @@ -56,15 +57,15 @@ class Import return $this->tryResponse($context, fn() => $this->getLibraries( context: $context, handle: fn(array $logContext = []) => fn(iResponse $response) => $this->handle( - context: $context, - response: $response, + context: $context, + response: $response, callback: fn(array $item, array $logContext = []) => $this->process( - context: $context, - guid: $guid, - mapper: $mapper, - item: $item, + context: $context, + guid: $guid, + mapper: $mapper, + item: $item, logContext: $logContext, - opts: $opts + ['after' => $after], + opts: $opts + ['after' => $after], ), logContext: $logContext ), @@ -110,9 +111,9 @@ class Import } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); $listDirs = ag($json, 'MediaContainer.Directory', []); @@ -374,15 +375,15 @@ class Import try { $it = Items::fromIterable( iterable: httpClientChunks($this->http->stream($response)), - options: [ - 'pointer' => '/MediaContainer/Metadata', - 'decoder' => new ErrorWrappingDecoder( - innerDecoder: new ExtJsonDecoder( - assoc: true, - options: JSON_INVALID_UTF8_IGNORE - ) - ) - ] + options: [ + 'pointer' => '/MediaContainer/Metadata', + 'decoder' => new ErrorWrappingDecoder( + innerDecoder: new ExtJsonDecoder( + assoc: true, + options: JSON_INVALID_UTF8_IGNORE + ) + ) + ] ); foreach ($it as $entity) { @@ -566,18 +567,18 @@ class Import $entity = $this->createEntity( context: $context, - guid: $guid, - item: $item, - opts: $opts + [ - 'override' => [ - iState::COLUMN_EXTRA => [ - $context->backendName => [ - iState::COLUMN_EXTRA_EVENT => 'task.import', - iState::COLUMN_EXTRA_DATE => makeDate('now'), - ], - ], - ], - ] + guid: $guid, + item: $item, + opts: $opts + [ + 'override' => [ + iState::COLUMN_EXTRA => [ + $context->backendName => [ + iState::COLUMN_EXTRA_EVENT => 'task.import', + iState::COLUMN_EXTRA_DATE => makeDate('now'), + ], + ], + ], + ] ); if (!$entity->hasGuids() && !$entity->hasRelativeGuid()) { diff --git a/src/Backends/Plex/Action/ParseWebhook.php b/src/Backends/Plex/Action/ParseWebhook.php index 58194422..7ec8ae10 100644 --- a/src/Backends/Plex/Action/ParseWebhook.php +++ b/src/Backends/Plex/Action/ParseWebhook.php @@ -155,7 +155,7 @@ final class ParseWebhook $context->backendName => [ iState::COLUMN_WATCHED => true === $isPlayed ? '1' : '0', iState::COLUMN_GUIDS => $guid->parse( - guids: ag($item, 'Guid', []), + guids: ag($item, 'Guid', []), context: $logContext ), ] @@ -181,31 +181,31 @@ final class ParseWebhook $entity = $this->createEntity( context: $context, - guid: $guid, - item: $obj, - opts: ['override' => $fields, Options::DISABLE_GUID => $disableGuid], + guid: $guid, + item: $obj, + opts: ['override' => $fields, Options::DISABLE_GUID => $disableGuid], )->setIsTainted(isTainted: true === in_array($event, self::WEBHOOK_TAINTED_EVENTS)); if (false === $entity->hasGuids() && false === $entity->hasRelativeGuid()) { return new Response( status: false, - error: new Error( - message: 'Ignoring [%(backend)] [%(title)] webhook event. No valid/supported external ids.', - context: [ - 'backend' => $context->backendName, - 'title' => $entity->getName(), - 'context' => [ - 'attributes' => $request->getAttributes(), - 'parsed' => $entity->getAll(), - 'payload' => $request->getParsedBody(), - ], - ], - level: Levels::ERROR - ), - extra: [ - 'http_code' => 200, - 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + error: new Error( + message: 'Ignoring [%(backend)] [%(title)] webhook event. No valid/supported external ids.', + context: [ + 'backend' => $context->backendName, + 'title' => $entity->getName(), + 'context' => [ + 'attributes' => $request->getAttributes(), + 'parsed' => $entity->getAll(), + 'payload' => $request->getParsedBody(), ], + ], + level: Levels::ERROR + ), + extra: [ + 'http_code' => 200, + 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + ], ); } @@ -213,28 +213,28 @@ final class ParseWebhook } catch (Throwable $e) { return new Response( status: false, - error: new Error( - message: 'Unhandled exception was thrown during [%(backend)] webhook event parsing.', - context: [ - 'backend' => $context->backendName, - 'exception' => [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'kind' => get_class($e), - 'message' => $e->getMessage(), - 'trace' => $context->trace ? $e->getTrace() : [], - ], - 'context' => [ - 'attributes' => $request->getAttributes(), - 'payload' => $request->getParsedBody(), - ], - ], - level: Levels::ERROR - ), - extra: [ - 'http_code' => 200, - 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + error: new Error( + message: 'Unhandled exception was thrown during [%(backend)] webhook event parsing.', + context: [ + 'backend' => $context->backendName, + 'exception' => [ + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'kind' => get_class($e), + 'message' => $e->getMessage(), + 'trace' => $context->trace ? $e->getTrace() : [], ], + 'context' => [ + 'attributes' => $request->getAttributes(), + 'payload' => $request->getParsedBody(), + ], + ], + level: Levels::ERROR + ), + extra: [ + 'http_code' => 200, + 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + ], ); } } diff --git a/src/Backends/Plex/Action/SearchQuery.php b/src/Backends/Plex/Action/SearchQuery.php index f2fb7638..061ec19b 100644 --- a/src/Backends/Plex/Action/SearchQuery.php +++ b/src/Backends/Plex/Action/SearchQuery.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace App\Backends\Plex\Action; use App\Backends\Common\CommonTrait; +use App\Backends\Common\Context; use App\Backends\Common\Error; use App\Backends\Common\Levels; use App\Backends\Common\Response; -use App\Backends\Common\Context; use App\Libs\Options; use JsonException; use Psr\Log\LoggerInterface; @@ -81,22 +81,22 @@ final class SearchQuery if (200 !== $response->getStatusCode()) { return new Response( status: false, - error: new Error( - message: 'Search request for [%(query)] in [%(backend)] returned with unexpected [%(status_code)] status code.', - context: [ - 'backend' => $context->backendName, - 'query' => $query, - 'status_code' => $response->getStatusCode(), - ], - level: Levels::ERROR - ), + error: new Error( + message: 'Search request for [%(query)] in [%(backend)] returned with unexpected [%(status_code)] status code.', + context: [ + 'backend' => $context->backendName, + 'query' => $query, + 'status_code' => $response->getStatusCode(), + ], + level: Levels::ERROR + ), ); } $json = json_decode( - json: $response->getContent(), + json: $response->getContent(), associative: true, - flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ); if ($context->trace) { diff --git a/src/Backends/Plex/PlexActionTrait.php b/src/Backends/Plex/PlexActionTrait.php index 9c8a1f24..c5f909ce 100644 --- a/src/Backends/Plex/PlexActionTrait.php +++ b/src/Backends/Plex/PlexActionTrait.php @@ -139,8 +139,8 @@ trait PlexActionTrait if (null !== ($parentId = ag($item, ['grandparentRatingKey', 'parentRatingKey']))) { $builder[iState::COLUMN_PARENT] = $this->getEpisodeParent( context: $context, - guid: $guid, - id: $parentId + guid: $guid, + id: $parentId ); $metadata[iState::COLUMN_PARENT] = $builder[iState::COLUMN_PARENT]; } diff --git a/src/Libs/Database/PDO/PDODataMigration.php b/src/Libs/Database/PDO/PDODataMigration.php index 15e17867..315ee171 100644 --- a/src/Libs/Database/PDO/PDODataMigration.php +++ b/src/Libs/Database/PDO/PDODataMigration.php @@ -97,9 +97,9 @@ final class PDODataMigration foreach ($stmt as $row) { $row['meta'] = json_decode( - json: $row['meta'] ?? '[]', + json: $row['meta'] ?? '[]', associative: true, - flags: JSON_INVALID_UTF8_IGNORE + flags: JSON_INVALID_UTF8_IGNORE ); $extra = [ @@ -146,10 +146,10 @@ final class PDODataMigration iFace::COLUMN_EPISODE => ag($row['meta'], iFace::COLUMN_EPISODE, null), iFace::COLUMN_PARENT => json_encode( value: array_intersect_key( - $row, - ag($row['meta'], iFace::COLUMN_PARENT, []), - Guid::getSupported(includeVirtual: false) - ), + $row, + ag($row['meta'], iFace::COLUMN_PARENT, []), + Guid::getSupported(includeVirtual: false) + ), flags: $this->jFlags ), iFace::COLUMN_GUIDS => json_encode( @@ -158,32 +158,32 @@ final class PDODataMigration ), iFace::COLUMN_META_DATA => json_encode( value: (function () use ($metadata): array { - $list = []; + $list = []; - foreach (Config::get('servers', []) as $name => $info) { - if (true !== (bool)ag($info, 'import.enabled', false)) { - continue; - } - $list[$name] = $metadata; - } + foreach (Config::get('servers', []) as $name => $info) { + if (true !== (bool)ag($info, 'import.enabled', false)) { + continue; + } + $list[$name] = $metadata; + } - return $list; - })(), + return $list; + })(), flags: $this->jFlags ), iFace::COLUMN_EXTRA => json_encode( value: (function () use ($extra): array { - $list = []; + $list = []; - foreach (Config::get('servers', []) as $name => $info) { - if (true !== (bool)ag($info, 'import.enabled', false)) { - continue; - } - $list[$name] = $extra; - } + foreach (Config::get('servers', []) as $name => $info) { + if (true !== (bool)ag($info, 'import.enabled', false)) { + continue; + } + $list[$name] = $extra; + } - return $list; - })(), + return $list; + })(), flags: $this->jFlags ), ] @@ -262,27 +262,27 @@ final class PDODataMigration foreach ($oldDB->query("SELECT * FROM state") as $row) { $row[iFace::COLUMN_EXTRA] = json_decode( - json: $row[iFace::COLUMN_EXTRA] ?? '[]', + json: $row[iFace::COLUMN_EXTRA] ?? '[]', associative: true, - flags: JSON_INVALID_UTF8_IGNORE + flags: JSON_INVALID_UTF8_IGNORE ); $row[iFace::COLUMN_GUIDS] = json_decode( - json: $row[iFace::COLUMN_GUIDS] ?? '[]', + json: $row[iFace::COLUMN_GUIDS] ?? '[]', associative: true, - flags: JSON_INVALID_UTF8_IGNORE + flags: JSON_INVALID_UTF8_IGNORE ); $row[iFace::COLUMN_PARENT] = json_decode( - json: $row[iFace::COLUMN_PARENT] ?? '[]', + json: $row[iFace::COLUMN_PARENT] ?? '[]', associative: true, - flags: JSON_INVALID_UTF8_IGNORE + flags: JSON_INVALID_UTF8_IGNORE ); $row['suids'] = json_decode( - json: $row['suids'] ?? '[]', + json: $row['suids'] ?? '[]', associative: true, - flags: JSON_INVALID_UTF8_IGNORE + flags: JSON_INVALID_UTF8_IGNORE ); $extra = [ @@ -330,9 +330,9 @@ final class PDODataMigration iFace::COLUMN_EPISODE => $row[iFace::COLUMN_EPISODE] ?? null, iFace::COLUMN_PARENT => json_encode( value: array_intersect_key( - $row[iFace::COLUMN_PARENT] ?? [], - Guid::getSupported(includeVirtual: false) - ), + $row[iFace::COLUMN_PARENT] ?? [], + Guid::getSupported(includeVirtual: false) + ), flags: $this->jFlags ), iFace::COLUMN_GUIDS => json_encode( @@ -341,37 +341,37 @@ final class PDODataMigration ), iFace::COLUMN_META_DATA => json_encode( value: (function () use ($row, $metadata): array { - $list = []; + $list = []; - foreach (Config::get('servers', []) as $name => $info) { - $list[$name] = []; - if (null !== ($row['suids'][$name] ?? null)) { - $list[$name][iFace::COLUMN_ID] = $row['suids'][$name]; - } + foreach (Config::get('servers', []) as $name => $info) { + $list[$name] = []; + if (null !== ($row['suids'][$name] ?? null)) { + $list[$name][iFace::COLUMN_ID] = $row['suids'][$name]; + } - $list[$name] += $metadata; - } + $list[$name] += $metadata; + } - return $list; - })(), + return $list; + })(), flags: $this->jFlags ), iFace::COLUMN_EXTRA => json_encode( value: (function () use ($row, $extra): array { - $list = []; + $list = []; - foreach (Config::get('servers', []) as $name => $info) { - $list[$name] = []; + foreach (Config::get('servers', []) as $name => $info) { + $list[$name] = []; - if (null !== ($row['suids'][$name] ?? null)) { - continue; - } + if (null !== ($row['suids'][$name] ?? null)) { + continue; + } - $list[$name] += $extra; - } + $list[$name] += $extra; + } - return $list; - })(), + return $list; + })(), flags: $this->jFlags ), ]; diff --git a/src/Libs/Entity/StateEntity.php b/src/Libs/Entity/StateEntity.php index 4aa4af63..971cff47 100644 --- a/src/Libs/Entity/StateEntity.php +++ b/src/Libs/Entity/StateEntity.php @@ -161,15 +161,15 @@ final class StateEntity implements iFace $this->guids, Guid::getSupported(includeVirtual: true) ), context: [ - 'backend' => $this->via, - 'backend_id' => ag($this->getMetadata($this->via) ?? [], 'id'), - 'item' => [ - 'id' => $this->id, - 'type' => $this->type, - 'year' => $this->year, - 'title' => $this->getName() - ] + 'backend' => $this->via, + 'backend_id' => ag($this->getMetadata($this->via) ?? [], 'id'), + 'item' => [ + 'id' => $this->id, + 'type' => $this->type, + 'year' => $this->year, + 'title' => $this->getName() ] + ] )->getPointers(); } diff --git a/src/Libs/Servers/JellyfinServer.php b/src/Libs/Servers/JellyfinServer.php index 8d2943ef..983b30bc 100644 --- a/src/Libs/Servers/JellyfinServer.php +++ b/src/Libs/Servers/JellyfinServer.php @@ -62,24 +62,24 @@ class JellyfinServer implements ServerInterface ): ServerInterface { $cloned = clone $this; $cloned->context = new Context( - clientName: static::NAME, - backendName: $name, - backendUrl: $url, - cache: $this->cache->withData(static::NAME . '_' . $name, $options), - backendId: $uuid, - backendToken: $token, - backendUser: $userId, + clientName: static::NAME, + backendName: $name, + backendUrl: $url, + cache: $this->cache->withData(static::NAME . '_' . $name, $options), + backendId: $uuid, + backendToken: $token, + backendUser: $userId, backendHeaders: array_replace_recursive( - [ - 'headers' => [ - 'Accept' => 'application/json', - 'X-MediaBrowser-Token' => $token, - ], - ], - $options['client'] ?? [] - ), - trace: true === ag($options, Options::DEBUG_TRACE), - options: $options + [ + 'headers' => [ + 'Accept' => 'application/json', + 'X-MediaBrowser-Token' => $token, + ], + ], + $options['client'] ?? [] + ), + trace: true === ag($options, Options::DEBUG_TRACE), + options: $options ); $cloned->guid = $cloned->guid->withContext($cloned->context); @@ -156,9 +156,9 @@ class JellyfinServer implements ServerInterface { $response = Container::get(SearchQuery::class)( context: $this->context, - query: $query, - limit: $limit, - opts: $opts + query: $query, + limit: $limit, + opts: $opts ); if ($response->hasError()) { @@ -179,8 +179,8 @@ class JellyfinServer implements ServerInterface { $response = Container::get(SearchId::class)( context: $this->context, - id: $id, - opts: $opts + id: $id, + opts: $opts ); if ($response->hasError()) { @@ -206,9 +206,9 @@ class JellyfinServer implements ServerInterface { $response = Container::get(GetLibrary::class)( context: $this->context, - guid: $this->guid, - id: $id, - opts: $opts + guid: $this->guid, + id: $id, + opts: $opts ); if ($response->hasError()) { @@ -240,10 +240,10 @@ class JellyfinServer implements ServerInterface public function push(array $entities, QueueRequests $queue, DateTimeInterface|null $after = null): array { $response = Container::get(Push::class)( - context: $this->context, + context: $this->context, entities: $entities, - queue: $queue, - after: $after + queue: $queue, + after: $after ); if ($response->hasError()) { @@ -261,12 +261,12 @@ class JellyfinServer implements ServerInterface { $response = Container::get(Import::class)( context: $this->context, - guid: $this->guid, - mapper: $mapper, - after: $after, - opts: [ - Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), - ] + guid: $this->guid, + mapper: $mapper, + after: $after, + opts: [ + Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), + ] ); if ($response->hasError()) { @@ -284,9 +284,9 @@ class JellyfinServer implements ServerInterface { $response = Container::get(Backup::class)( context: $this->context, - guid: $this->guid, - mapper: $mapper, - opts: $opts + ['writer' => $writer] + guid: $this->guid, + mapper: $mapper, + opts: $opts + ['writer' => $writer] ); if ($response->hasError()) { @@ -304,13 +304,13 @@ class JellyfinServer implements ServerInterface { $response = Container::get(Export::class)( context: $this->context, - guid: $this->guid, - mapper: $mapper, - after: $after, - opts: [ - 'queue' => $queue, - Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), - ] + guid: $this->guid, + mapper: $mapper, + after: $after, + opts: [ + 'queue' => $queue, + Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), + ] ); if ($response->hasError()) { diff --git a/src/Libs/Servers/PlexServer.php b/src/Libs/Servers/PlexServer.php index 4f65a5c0..86a27a80 100644 --- a/src/Libs/Servers/PlexServer.php +++ b/src/Libs/Servers/PlexServer.php @@ -61,24 +61,24 @@ class PlexServer implements ServerInterface ): ServerInterface { $cloned = clone $this; $cloned->context = new Context( - clientName: static::NAME, - backendName: $name, - backendUrl: $url, - cache: $this->cache->withData(static::NAME . '_' . $name, $options), - backendId: $uuid, - backendToken: $token, - backendUser: $userId, + clientName: static::NAME, + backendName: $name, + backendUrl: $url, + cache: $this->cache->withData(static::NAME . '_' . $name, $options), + backendId: $uuid, + backendToken: $token, + backendUser: $userId, backendHeaders: array_replace_recursive( - [ - 'headers' => [ - 'Accept' => 'application/json', - 'X-Plex-Token' => $token, - ], - ], - $options['client'] ?? [] - ), - trace: true === ag($options, Options::DEBUG_TRACE), - options: $options + [ + 'headers' => [ + 'Accept' => 'application/json', + 'X-Plex-Token' => $token, + ], + ], + $options['client'] ?? [] + ), + trace: true === ag($options, Options::DEBUG_TRACE), + options: $options ); $cloned->guid = $this->guid->withContext($cloned->context); @@ -141,7 +141,7 @@ class PlexServer implements ServerInterface { $response = Container::get(ParseWebhook::class)( context: $this->context, - guid: $this->guid, + guid: $this->guid, request: $request ); @@ -163,9 +163,9 @@ class PlexServer implements ServerInterface { $response = Container::get(SearchQuery::class)( context: $this->context, - query: $query, - limit: $limit, - opts: $opts + query: $query, + limit: $limit, + opts: $opts ); if ($response->hasError()) { @@ -232,10 +232,10 @@ class PlexServer implements ServerInterface public function push(array $entities, QueueRequests $queue, DateTimeInterface|null $after = null): array { $response = Container::get(Push::class)( - context: $this->context, + context: $this->context, entities: $entities, - queue: $queue, - after: $after + queue: $queue, + after: $after ); if ($response->hasError()) { @@ -253,12 +253,12 @@ class PlexServer implements ServerInterface { $response = Container::get(Import::class)( context: $this->context, - guid: $this->guid, - mapper: $mapper, - after: $after, - opts: [ - Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), - ] + guid: $this->guid, + mapper: $mapper, + after: $after, + opts: [ + Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), + ] ); if ($response->hasError()) { @@ -276,9 +276,9 @@ class PlexServer implements ServerInterface { $response = Container::get(Backup::class)( context: $this->context, - guid: $this->guid, - mapper: $mapper, - opts: $opts + ['writer' => $writer] + guid: $this->guid, + mapper: $mapper, + opts: $opts + ['writer' => $writer] ); if ($response->hasError()) { @@ -296,13 +296,13 @@ class PlexServer implements ServerInterface { $response = Container::get(Export::class)( context: $this->context, - guid: $this->guid, - mapper: $mapper, - after: $after, - opts: [ - 'queue' => $queue, - Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), - ], + guid: $this->guid, + mapper: $mapper, + after: $after, + opts: [ + 'queue' => $queue, + Options::DISABLE_GUID => (bool)Config::get('episodes.disable.guid'), + ], ); if ($response->hasError()) {