123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Http\Helpers;
- use Monolog\Handler\StreamHandler;
- use Monolog\Logger;
- use Tmdb\Client;
- use Tmdb\Event\BeforeRequestEvent;
- use Tmdb\Event\HttpClientExceptionEvent;
- use Tmdb\Event\Listener\Logger\LogApiErrorListener;
- use Tmdb\Event\Listener\Logger\LogHttpMessageListener;
- use Tmdb\Event\Listener\Request\AcceptJsonRequestListener;
- use Tmdb\Event\Listener\Request\ApiTokenRequestListener;
- use Tmdb\Event\Listener\Request\ContentTypeJsonRequestListener;
- use Tmdb\Event\Listener\Request\LanguageFilterRequestListener;
- use Tmdb\Event\Listener\Request\UserAgentRequestListener;
- use Tmdb\Event\Listener\RequestListener;
- use Tmdb\Event\RequestEvent;
- use Tmdb\Event\ResponseEvent;
- use Tmdb\Token\Api\ApiToken;
- class TmdbProvider {
- /**
- * @return Client
- */
- public static function getClient() : Client {
- $key = "b187f8d9c5e72b1faecb741d5d04239a";
- $token = new ApiToken($key);
- define('TMDB_API_KEY', $key);
- $ed = new \Symfony\Component\EventDispatcher\EventDispatcher();
- $logger = new Logger(
- 'php-tmdb',
- [ new StreamHandler(storage_path('logs/tmdb.log')) ]
- );
- $client = new Client([
- 'api_token' => $token,
- //'secure' => true,
- 'base_uri' => Client::TMDB_URI,
- 'event_dispatcher' => [
- 'adapter' => $ed
- ]
- ]);
- $requestListener = new RequestListener($client->getHttpClient(), $ed);
- $ed->addListener(RequestEvent::class, $requestListener);
- $apiTokenListener = new ApiTokenRequestListener($client->getToken());
- $ed->addListener(BeforeRequestEvent::class, $apiTokenListener);
- $acceptJSONListener = new AcceptJsonRequestListener();
- $ed->addListener(BeforeRequestEvent::class, $acceptJSONListener);
- $jsonContentTypeListener = new ContentTypeJsonRequestListener();
- $ed->addListener(BeforeRequestEvent::class, $jsonContentTypeListener);
- $userAgentListener = new UserAgentRequestListener();
- $ed->addListener(BeforeRequestEvent::class, $userAgentListener);
- $requestLoggerListener = new LogHttpMessageListener(
- $logger,
- new \Tmdb\Formatter\HttpMessage\FullHttpMessageFormatter()
- );
- $ed->addListener(BeforeRequestEvent::class, $requestLoggerListener);
- $ed->addListener(ResponseEvent::class, $requestLoggerListener);
- $ed->addListener(HttpClientExceptionEvent::class, $requestLoggerListener);
- $ed->addListener(LogApiErrorListener::class, $requestLoggerListener);
- $langFilterListener = new LanguageFilterRequestListener('de-DE');
- $ed->addListener(BeforeRequestEvent::class, $langFilterListener);
- return $client;
- }
- }
|