Skip to content

Commit

Permalink
Fetch exceptions when creating session
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Sep 14, 2017
1 parent 44da4d7 commit 8bcaec1
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Service/AuthService.php
Expand Up @@ -14,9 +14,14 @@
use Core23\LastFm\Connection\SessionInterface;
use Core23\LastFm\Exception\ApiException;
use Core23\LastFm\Exception\NotFoundException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

final class AuthService extends AbstractService
final class AuthService extends AbstractService implements LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* @var string
*/
Expand All @@ -33,6 +38,7 @@ public function __construct(ConnectionInterface $connection, string $authUrl = '
parent::__construct($connection);

$this->authUrl = $authUrl;
$this->logger = new NullLogger();
}

/**
Expand All @@ -41,17 +47,25 @@ public function __construct(ConnectionInterface $connection, string $authUrl = '
* @param string $token
*
* @return SessionInterface|null
*
* @throws ApiException
* @throws NotFoundException
*/
public function createSession(string $token): ?SessionInterface
{
$response = $this->signedCall('auth.getSession', array(
'token' => $token,
));
try {
$response = $this->signedCall('auth.getSession', array(
'token' => $token,
));

return new Session($response['session']['name'], $response['session']['key'], $response['session']['subscriber']);
return new Session($response['session']['name'], $response['session']['key'], $response['session']['subscriber']);
} catch (ApiException $e) {
$this->logger->warning(sprintf('Error getting session for "%s" token.', $token), array(
'exception' => $e,
));
} catch (NotFoundException $e) {
$this->logger->info(sprintf('No session was found for "%s" token.', $token), array(
'exception' => $e,
));
}
return null;
}

/**
Expand Down

0 comments on commit 8bcaec1

Please sign in to comment.