Skip to content

Commit

Permalink
Use url value objects in for plex
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Jun 28, 2023
1 parent 75cfd44 commit 9073427
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 78 deletions.
5 changes: 3 additions & 2 deletions src/Api/Plex/Exception/PlexNotFoundError.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Movary\Api\Plex\Exception;

use Movary\ValueObject\Url;
use RuntimeException;

class PlexNotFoundError extends RuntimeException
{
public static function create(string $requestedURI) : self
public static function create(Url $requestUri) : self
{
return new self('The requested URI does not exist: ' . $requestedURI);
return new self('The requested URI does not exist: ' . $requestUri);
}
}
16 changes: 12 additions & 4 deletions src/Api/Plex/PlexApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Movary\Domain\User\Service\Authentication;
use Movary\Domain\User\UserApi;
use Movary\Service\ServerSettings;
use Movary\ValueObject\RelativeUrl;
use Movary\ValueObject\Url;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -46,8 +48,10 @@ public function findPlexAccessToken(string $plexPinId, string $temporaryPlexClie
'code' => $temporaryPlexClientCode,
];

$relativeUrl = RelativeUrl::createFromString('/pins/' . $plexPinId);

try {
$plexRequest = $this->plexTvClient->get('/pins/' . $plexPinId, $headers);
$plexRequest = $this->plexTvClient->get($relativeUrl, $headers);
} catch (PlexNotFoundError) {
$this->logger->error('Plex pin does not exist');

Expand All @@ -63,8 +67,10 @@ public function findPlexAccount(PlexAccessToken $plexAccessToken) : ?PlexAccount
'X-Plex-Token' => $plexAccessToken->getPlexAccessTokenAsString()
];

$relativeUrl = RelativeUrl::createFromString('/user');

try {
$accountData = $this->plexTvClient->get('/user', $headers);
$accountData = $this->plexTvClient->get($relativeUrl, $headers);
} catch (PlexAuthenticationError) {
$this->logger->error('Plex access token is invalid');

Expand All @@ -82,8 +88,10 @@ public function findPlexAccount(PlexAccessToken $plexAccessToken) : ?PlexAccount
*/
public function generatePlexAuthenticationUrl() : ?string
{
$relativeUrl = RelativeUrl::createFromString('/pins');

try {
$plexAuthenticationData = $this->plexTvClient->sendPostRequest('/pins');
$plexAuthenticationData = $this->plexTvClient->sendPostRequest($relativeUrl);
} catch (PlexNoClientIdentifier) {
return null;
}
Expand Down Expand Up @@ -112,7 +120,7 @@ public function generatePlexAuthenticationUrl() : ?string
return self::BASE_URL . http_build_query($getParameters);
}

public function verifyPlexUrl(int $userId, string $url) : bool
public function verifyPlexUrl(int $userId, Url $url) : bool
{
$query = [
'X-Plex-Token' => $this->userApi->fetchUser($userId)->getPlexAccessToken()
Expand Down
5 changes: 3 additions & 2 deletions src/Api/Plex/PlexLocalServerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Movary\Api\Plex\Exception\PlexNoClientIdentifier;
use Movary\Api\Plex\Exception\PlexNotFoundError;
use Movary\Util\Json;
use Movary\ValueObject\Url;
use RuntimeException;

class PlexLocalServerClient
Expand Down Expand Up @@ -41,7 +42,7 @@ public function __construct(
* @psalm-suppress PossiblyUndefinedVariable
*/
public function sendGetRequest(
string $requestUrl,
Url $requestUrl,
?array $customQuery = [],
) : array {
if ($this->plexIdentifier === '') {
Expand All @@ -55,7 +56,7 @@ public function sendGetRequest(
];

try {
$response = $this->httpClient->request('GET', $requestUrl, $requestOptions);
$response = $this->httpClient->request('GET', (string)$requestUrl, $requestOptions);
} catch (ClientException $e) {
match (true) {
$e->getCode() === 401 => throw PlexAuthenticationError::create(),
Expand Down
14 changes: 8 additions & 6 deletions src/Api/Plex/PlexTvClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Movary\Api\Plex\Exception\PlexNoClientIdentifier;
use Movary\Api\Plex\Exception\PlexNotFoundError;
use Movary\Util\Json;
use Movary\ValueObject\RelativeUrl;
use Movary\ValueObject\Url;
use RuntimeException;

class PlexTvClient
Expand Down Expand Up @@ -41,21 +43,21 @@ public function __construct(
* @psalm-suppress PossiblyUndefinedVariable
*/
public function get(
string $relativeUrl,
RelativeUrl $relativeUrl,
array $headers = [],
) : array {
if ($this->plexIdentifier === null) {
throw PlexNoClientIdentifier::create();
}

$requestUrl = self::BASE_URL . $relativeUrl;
$requestUrl = Url::createFromString(self::BASE_URL)->appendRelativeUrl($relativeUrl);
$requestOptions = [
'form_params' => $this->defaultFormData,
'headers' => array_merge(self::DEFAULT_HEADERS, $headers)
];

try {
$response = $this->httpClient->request('GET', $requestUrl, $requestOptions);
$response = $this->httpClient->request('GET', (string)$requestUrl, $requestOptions);
} catch (ClientException $e) {
match (true) {
$e->getCode() === 401 => throw PlexAuthenticationError::create(),
Expand All @@ -71,20 +73,20 @@ public function get(
/**
* @psalm-suppress PossiblyUndefinedVariable
*/
public function sendPostRequest(string $relativeUrl) : array
public function sendPostRequest(RelativeUrl $relativeUrl) : array
{
if ($this->plexIdentifier === null) {
throw PlexNoClientIdentifier::create();
}

$requestUrl = self::BASE_URL . $relativeUrl;
$requestUrl = Url::createFromString(self::BASE_URL)->appendRelativeUrl($relativeUrl);
$requestOptions = [
'form_params' => $this->defaultFormData,
'headers' => self::DEFAULT_HEADERS
];

try {
$response = $this->httpClient->request('POST', $requestUrl, $requestOptions);
$response = $this->httpClient->request('POST', (string)$requestUrl, $requestOptions);
} catch (ClientException $e) {
match (true) {
$e->getCode() === 401 => throw PlexAuthenticationError::create(),
Expand Down
43 changes: 22 additions & 21 deletions src/Domain/User/UserApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Movary\Domain\User;

use Movary\Domain\User\Service\Validator;
use Movary\ValueObject\Url;
use Ramsey\Uuid\Uuid;
use RuntimeException;

Expand Down Expand Up @@ -110,11 +111,6 @@ public function findPlexClientId(int $userId) : ?string
return $this->repository->findPlexClientId($userId);
}

public function findTemporaryPlexCode(int $userId) : ?string
{
return $this->repository->findTemporaryPlexCode($userId);
}

public function findPlexServerUrl(int $userId) : ?string
{
return $this->repository->findPlexServerUrl($userId);
Expand All @@ -125,6 +121,11 @@ public function findPlexWebhookId(int $userId) : ?string
return $this->repository->findPlexWebhookId($userId);
}

public function findTemporaryPlexCode(int $userId) : ?string
{
return $this->repository->findTemporaryPlexCode($userId);
}

public function findTraktClientId(int $userId) : ?string
{
return $this->repository->findTraktClientId($userId);
Expand Down Expand Up @@ -269,49 +270,49 @@ public function updatePassword(int $userId, string $newPassword) : void
$this->repository->updatePassword($userId, $passwordHash);
}

public function updatePlexScrobblerOptions(int $userId, bool $scrobbleWatches, bool $scrobbleRatings) : void
public function updatePlexAccessToken(int $userId, ?string $plexAccessToken) : void
{
$this->repository->updatePlexScrobblerOptions($userId, $scrobbleWatches, $scrobbleRatings);
$this->repository->updatePlexAccessToken($userId, $plexAccessToken);
}

public function updatePrivacyLevel(int $userId, int $privacyLevel) : void
public function updatePlexAccountId(int $userId, ?string $plexAccountId) : void
{
$this->repository->updatePrivacyLevel($userId, $privacyLevel);
$this->repository->updatePlexAccountId($userId, $plexAccountId);
}

public function updateTraktClientId(int $userId, ?string $traktClientId) : void
public function updatePlexClientId(int $userId, ?int $plexClientId) : void
{
$this->repository->updateTraktClientId($userId, $traktClientId);
$this->repository->updatePlexClientId($userId, $plexClientId);
}

public function updateTraktUserName(int $userId, ?string $traktUserName) : void
public function updatePlexScrobblerOptions(int $userId, bool $scrobbleWatches, bool $scrobbleRatings) : void
{
$this->repository->updateTraktUserName($userId, $traktUserName);
$this->repository->updatePlexScrobblerOptions($userId, $scrobbleWatches, $scrobbleRatings);
}

public function updatePlexAccessToken(int $userId, ?string $plexAccessToken) : void
public function updatePlexServerUrl(int $userId, Url $plexServerUrl) : void
{
$this->repository->updatePlexAccessToken($userId, $plexAccessToken);
$this->repository->updatePlexServerurl($userId, $plexServerUrl);
}

public function updatePlexClientId(int $userId, ?int $plexClientId) : void
public function updatePrivacyLevel(int $userId, int $privacyLevel) : void
{
$this->repository->updatePlexClientId($userId, $plexClientId);
$this->repository->updatePrivacyLevel($userId, $privacyLevel);
}

public function updateTemporaryPlexClientCode(int $userId, ?string $plexClientCode) : void
{
$this->repository->updateTemporaryPlexClientCode($userId, $plexClientCode);
}

public function updatePlexAccountId(int $userId, ?string $plexAccountId) : void
public function updateTraktClientId(int $userId, ?string $traktClientId) : void
{
$this->repository->updatePlexAccountId($userId, $plexAccountId);
$this->repository->updateTraktClientId($userId, $traktClientId);
}

public function updatePlexServerurl(int $userId, string $plexServerUrl) : void
public function updateTraktUserName(int $userId, ?string $traktUserName) : void
{
$this->repository->updatePlexServerurl($userId, $plexServerUrl);
$this->repository->updateTraktUserName($userId, $traktUserName);
}

public function updateVisibleDashboardRows(int $userId, ?string $visibleRows) : void
Expand Down
Loading

0 comments on commit 9073427

Please sign in to comment.