diff --git a/src/HttpController/ImportController.php b/src/HttpController/ImportController.php index fe4dd2a3..ed152671 100644 --- a/src/HttpController/ImportController.php +++ b/src/HttpController/ImportController.php @@ -8,12 +8,14 @@ use Movary\ValueObject\Http\Request; use Movary\ValueObject\Http\Response; use Movary\ValueObject\Http\StatusCode; +use Psr\Log\LoggerInterface; class ImportController { public function __construct( private readonly Authentication $authenticationService, - private readonly ImportService $importService + private readonly ImportService $importService, + private readonly LoggerInterface $logger, ) { } @@ -27,11 +29,16 @@ public function handleCsvImport(Request $request) : Response $exportType = $request->getRouteParameters()['exportType']; $fileParameters = $request->getFileParameters(); - match ($exportType) { - 'history' => $this->importHistory($userId, $fileParameters), - 'ratings' => $this->importRatings($userId, $fileParameters), - default => throw new \RuntimeException('Export type not handled: ' . $exportType) - }; + try { + match ($exportType) { + 'history' => $this->importHistory($userId, $fileParameters), + 'ratings' => $this->importRatings($userId, $fileParameters), + default => throw new \RuntimeException('Export type not handled: ' . $exportType) + }; + } catch (\Throwable $t) { + $this->logger->error('Could not import: ' . $exportType, ['exception' => $t]); + $_SESSION['importHistoryError'] = $exportType; + } return Response::create( StatusCode::createSeeOther(), @@ -47,6 +54,8 @@ private function importHistory(int $userId, array $fileParameter) : void } $this->importService->importHistory($userId, $fileParameter['history']['tmp_name']); + + $_SESSION['importHistorySuccessful'] = true; } private function importRatings(int $userId, array $fileParameter) : void @@ -56,5 +65,7 @@ private function importRatings(int $userId, array $fileParameter) : void } $this->importService->importRatings($userId, $fileParameter['ratings']['tmp_name']); + + $_SESSION['importRatingsSuccessful'] = true; } } diff --git a/src/HttpController/SettingsController.php b/src/HttpController/SettingsController.php index e98dfd53..83a5f78e 100644 --- a/src/HttpController/SettingsController.php +++ b/src/HttpController/SettingsController.php @@ -22,6 +22,7 @@ public function __construct( ) { } + // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh public function render() : Response { if ($this->authenticationService->isUserAuthenticated() === false) { @@ -34,7 +35,20 @@ public function render() : Response $passwordErrorMinLength = empty($_SESSION['passwordErrorMinLength']) === false ? $_SESSION['passwordErrorMinLength'] : null; $passwordErrorCurrentInvalid = empty($_SESSION['passwordErrorCurrentInvalid']) === false ? $_SESSION['passwordErrorCurrentInvalid'] : null; $passwordUpdated = empty($_SESSION['passwordUpdated']) === false ? $_SESSION['passwordUpdated'] : null; - unset($_SESSION['passwordUpdated'], $_SESSION['passwordErrorCurrentInvalid'], $_SESSION['passwordErrorMinLength'], $_SESSION['passwordErrorNotEqual']); + $traktCredentialsUpdated = empty($_SESSION['traktCredentialsUpdated']) === false ? $_SESSION['traktCredentialsUpdated'] : null; + $importHistorySuccessful = empty($_SESSION['importHistorySuccessful']) === false ? $_SESSION['importHistorySuccessful'] : null; + $importRatingsSuccessful = empty($_SESSION['importRatingsSuccessful']) === false ? $_SESSION['importRatingsSuccessful'] : null; + $importHistoryError = empty($_SESSION['importHistoryError']) === false ? $_SESSION['importHistoryError'] : null; + unset( + $_SESSION['passwordUpdated'], + $_SESSION['passwordErrorCurrentInvalid'], + $_SESSION['passwordErrorMinLength'], + $_SESSION['passwordErrorNotEqual'], + $_SESSION['traktCredentialsUpdated'], + $_SESSION['importHistorySuccessful'], + $_SESSION['importRatingsSuccessful'], + $_SESSION['importHistoryError'], + ); return Response::create( StatusCode::createOk(), @@ -43,7 +57,11 @@ public function render() : Response 'passwordErrorNotEqual' => $passwordErrorNotEqual, 'passwordErrorMinLength' => $passwordErrorMinLength, 'passwordErrorCurrentInvalid' => $passwordErrorCurrentInvalid, + 'traktCredentialsUpdated' => $traktCredentialsUpdated, + 'importHistorySuccessful' => $importHistorySuccessful, + 'importRatingsSuccessful' => $importRatingsSuccessful, 'passwordUpdated' => $passwordUpdated, + 'importHistoryError' => $importHistoryError, 'traktClientId' => $this->userApi->findTraktClientId($userId), 'traktUserName' => $this->userApi->findTraktUserName($userId), 'applicationVersion' => $this->applicationVersion ?? '-', @@ -104,4 +122,35 @@ public function updatePassword(Request $request) : Response [Header::createLocation($_SERVER['HTTP_REFERER'])] ); } + + public function updateTrakt(Request $request) : Response + { + if ($this->authenticationService->isUserAuthenticated() === false) { + return Response::createFoundRedirect('/'); + } + + $userId = $this->authenticationService->getCurrentUserId(); + $postParameters = $request->getPostParameters(); + + $traktClientId = $postParameters['traktClientId']; + if (empty($traktClientId) === true) { + $traktClientId = null; + } + + $traktUserName = $postParameters['traktUserName']; + if (empty($traktUserName) === true) { + $traktUserName = null; + } + + $this->userApi->updateTraktClientId($userId, $traktClientId); + $this->userApi->updateTraktUserName($userId, $traktUserName); + + $_SESSION['traktCredentialsUpdated'] = true; + + return Response::create( + StatusCode::createSeeOther(), + null, + [Header::createLocation($_SERVER['HTTP_REFERER'])] + ); + } } diff --git a/templates/page/settings.html.twig b/templates/page/settings.html.twig index 3709295e..5d272a02 100644 --- a/templates/page/settings.html.twig +++ b/templates/page/settings.html.twig @@ -38,22 +38,26 @@ {% if passwordErrorNotEqual == true %} {% endif %} {% if passwordErrorMinLength == true %} {% endif %} {% if passwordErrorCurrentInvalid == true %} {% endif %} {% if passwordUpdated == true %} {% endif %} @@ -65,7 +69,7 @@
trakt.tv
-

To generate a client id visit this url here.

+

To get your username and generate a client id visit this url.

Username:

@@ -79,6 +83,14 @@
+ + {% if traktCredentialsUpdated == true %} + + {% endif %} + @@ -109,14 +121,39 @@ -
+ {% if importHistorySuccessful == true %} + + {% endif %} -
+ {% if importHistoryError == 'history' %} + + {% endif %} + +
+ {% if importRatingsSuccessful == true %} + + {% endif %} + + {% if importHistoryError == 'ratings' %} + + {% endif %}