Reformatted code according to PSR12.

This commit is contained in:
Abdulmhsen B. A. A
2022-07-07 17:54:56 +03:00
parent b314aefc0a
commit 1752e55042
28 changed files with 655 additions and 653 deletions

View File

@@ -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
)
);
}
}

View File

@@ -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.'
],
);
}
}

View File

@@ -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,

View File

@@ -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()) {

View File

@@ -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,
);
}
}

View File

@@ -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
),
);
}

View File

@@ -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 = [];

View File

@@ -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')
);
}

View File

@@ -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) {

View File

@@ -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()) {

View File

@@ -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.'
],
);
}
}

View File

@@ -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) {

View File

@@ -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()
);

View File

@@ -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 = [

View File

@@ -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()) {

View File

@@ -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
);
}
}

View File

@@ -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
),
);
}

View File

@@ -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
)
);
}
}

View File

@@ -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')
);
}

View File

@@ -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
),
);
}
}

View File

@@ -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()) {

View File

@@ -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.'
],
);
}
}

View File

@@ -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) {

View File

@@ -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];
}