TmdbProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Http\Helpers;
  3. use Monolog\Handler\StreamHandler;
  4. use Monolog\Logger;
  5. use Tmdb\Client;
  6. use Tmdb\Event\BeforeRequestEvent;
  7. use Tmdb\Event\HttpClientExceptionEvent;
  8. use Tmdb\Event\Listener\Logger\LogApiErrorListener;
  9. use Tmdb\Event\Listener\Logger\LogHttpMessageListener;
  10. use Tmdb\Event\Listener\Request\AcceptJsonRequestListener;
  11. use Tmdb\Event\Listener\Request\ApiTokenRequestListener;
  12. use Tmdb\Event\Listener\Request\ContentTypeJsonRequestListener;
  13. use Tmdb\Event\Listener\Request\LanguageFilterRequestListener;
  14. use Tmdb\Event\Listener\Request\UserAgentRequestListener;
  15. use Tmdb\Event\Listener\RequestListener;
  16. use Tmdb\Event\RequestEvent;
  17. use Tmdb\Event\ResponseEvent;
  18. use Tmdb\Token\Api\ApiToken;
  19. class TmdbProvider {
  20. /**
  21. * @return Client
  22. */
  23. public static function getClient() : Client {
  24. $key = "b187f8d9c5e72b1faecb741d5d04239a";
  25. $token = new ApiToken($key);
  26. define('TMDB_API_KEY', $key);
  27. $ed = new \Symfony\Component\EventDispatcher\EventDispatcher();
  28. $logger = new Logger(
  29. 'php-tmdb',
  30. [ new StreamHandler(storage_path('logs/tmdb.log')) ]
  31. );
  32. $client = new Client([
  33. 'api_token' => $token,
  34. //'secure' => true,
  35. 'base_uri' => Client::TMDB_URI,
  36. 'event_dispatcher' => [
  37. 'adapter' => $ed
  38. ]
  39. ]);
  40. $requestListener = new RequestListener($client->getHttpClient(), $ed);
  41. $ed->addListener(RequestEvent::class, $requestListener);
  42. $apiTokenListener = new ApiTokenRequestListener($client->getToken());
  43. $ed->addListener(BeforeRequestEvent::class, $apiTokenListener);
  44. $acceptJSONListener = new AcceptJsonRequestListener();
  45. $ed->addListener(BeforeRequestEvent::class, $acceptJSONListener);
  46. $jsonContentTypeListener = new ContentTypeJsonRequestListener();
  47. $ed->addListener(BeforeRequestEvent::class, $jsonContentTypeListener);
  48. $userAgentListener = new UserAgentRequestListener();
  49. $ed->addListener(BeforeRequestEvent::class, $userAgentListener);
  50. $requestLoggerListener = new LogHttpMessageListener(
  51. $logger,
  52. new \Tmdb\Formatter\HttpMessage\FullHttpMessageFormatter()
  53. );
  54. $ed->addListener(BeforeRequestEvent::class, $requestLoggerListener);
  55. $ed->addListener(ResponseEvent::class, $requestLoggerListener);
  56. $ed->addListener(HttpClientExceptionEvent::class, $requestLoggerListener);
  57. $ed->addListener(LogApiErrorListener::class, $requestLoggerListener);
  58. $langFilterListener = new LanguageFilterRequestListener('de-DE');
  59. $ed->addListener(BeforeRequestEvent::class, $langFilterListener);
  60. return $client;
  61. }
  62. }