rvkolosov

Untitled

May 27th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. use Illuminate\Config\Repository;
  6. use Illuminate\Http\Client\PendingRequest;
  7. use Illuminate\Http\Client\Response;
  8. use Illuminate\Support\Arr;
  9.  
  10. class YandexService
  11. {
  12.     protected PendingRequest $client;
  13.  
  14.     protected array $params;
  15.  
  16.     protected Repository $repository;
  17.  
  18.     public function __construct()
  19.     {
  20.         $this->setRepository();
  21.         $this->setParams();
  22.         $this->setClient($this->getRepository());
  23.     }
  24.  
  25.     protected function setRepository(): void
  26.     {
  27.         $repository = new Repository();
  28.  
  29.         $repository->set('token', config('yandex.token'));
  30.         $repository->set('url', config('yandex.url'));
  31.  
  32.         $this->repository = $repository;
  33.     }
  34.  
  35.     public function getRepository(): Repository
  36.     {
  37.         return $this->repository;
  38.     }
  39.  
  40.     protected function setClient(Repository $repository): void
  41.     {
  42.         $this->client = PendingRequest::withHeaders([
  43.             'Authorization' => "OAuth {$repository->get('token')}",
  44.         ])->baseUrl($repository->get('url'));
  45.     }
  46.  
  47.     public function changeClient(Repository $repository): void
  48.     {
  49.         $this->setClient($repository);
  50.     }
  51.  
  52.     public function getClient(): PendingRequest
  53.     {
  54.         return $this->client;
  55.     }
  56.  
  57.     protected function setParams(): void
  58.     {
  59.         $this->params = [
  60.             'lang' => 'ru',
  61.             'level' => 'video_an_site',
  62.         ];
  63.     }
  64.  
  65.     public function getParams(): array
  66.     {
  67.         return $this->params;
  68.     }
  69.  
  70.     public function getStatistics(): Response
  71.     {
  72.         return $this->getClient()->get('statistics/get.json?' . Arr::query($this->getParams()));
  73.     }
  74. }
Add Comment
Please, Sign In to add comment