123movies-seo/app/Services/ApiClient.php

232 lines
7.4 KiB
PHP

<?php
namespace App\Services;
use App\Option;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
class ApiClient
{
private $url;
private $per_page;
private $use_cache;
private $cache_time;
private $domain_src;
public function __construct($url, $per_page, $domain_src, $use_cache = true, $cache_time = 3600)
{
$this->url = Str::endsWith($url, '/') ? $url : $url . '/';
$this->per_page = $per_page;
$this->use_cache = $use_cache;
$this->cache_time = $cache_time;
$this->domain_src = $domain_src;
}
public function getMovie(int $id)
{
$action = sprintf('movie/%s', $id);
if ($this->use_cache === true) {
$key = $action . $this->page . $this->language;
$results = Cache::remember($key, $this->cache_time, function () use ($action) {
return $this->_call($action);
});
} else {
$results = $this->_call($action);
}
return $results;
}
public function getMoviesByTag(String $title, $page = 1)
{
$path = sprintf('movie/tag/%s', $this->per_page);
$options = [
'page' => $page,
'title' => urlencode(Str::of($title)->replace('-', ' ')->title())
];
$key = Str::slug(implode(' ', [$path, $title, $page]), '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options, $key) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getShowsByTag(String $title, $page = 1)
{
$path = sprintf('show/tag/%s', $this->per_page);
$options = [
'page' => $page,
'title' => urlencode(Str::of($title)->replace('-', ' ')->title())
];
$key = Str::slug(implode(' ', [$path, $title, $page]), '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options, $key) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getPopularMovies($page = 1, $per_page = null)
{
$per_page = is_null($per_page) ? $this->per_page : $per_page;
$path = 'movie/popular/' . $this->domain_src . '/' . $per_page;
$options = compact('page');
$key = Str::slug($path . ' ' . $page, '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getPopularShows($page = 1, $per_page = null)
{
$per_page = is_null($per_page) ? $this->per_page : $per_page;
$path = 'show/popular/' . $this->domain_src . '/' . $per_page;
$options = compact('page');
$key = Str::slug($path . ' ' . $page, '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getTrendingMovies($page = 1, $per_page = null)
{
$per_page = is_null($per_page) ? $this->per_page : $per_page;
$path = 'movie/trending/' . $this->domain_src . '/' . $per_page;
$options = compact('page');
$key = Str::slug($path . ' ' . $page, '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getTrendingShows($page = 1, $per_page = null)
{
$per_page = is_null($per_page) ? $this->per_page : $per_page;
$path = 'show/trending/' . $this->domain_src . '/' . $per_page;
$options = compact('page');
$key = Str::slug($path . ' ' . $page, '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getGenreMovies($id, $page = 1, $per_page = null)
{
$per_page = is_null($per_page) ? $this->per_page : $per_page;
$path = 'movie/popular/' . $this->domain_src . '/' . $per_page;
$options = [
'page' => $page,
'genre_id' => $id
];
$key = Str::slug(implode(' ', [$path, $id, $page, $per_page]), '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getGenreShows($id, $page = 1, $per_page = null)
{
$per_page = is_null($per_page) ? $this->per_page : $per_page;
$path = 'show/popular/' . $this->domain_src . '/' . $per_page;
$options = [
'page' => $page,
'genre_id' => $id
];
$key = Str::slug(implode(' ', [$path, $id, $page, $per_page]), '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
public function getSearch(string $query)
{
$path = 'search';
$options = [
'query' => $query
];
$key = Str::slug(implode(' ', [$path, str($query)->slug()]), '_');
if ($this->use_cache === true) {
$results = Cache::remember($key, $this->cache_time, function () use ($path, $options) {
return $this->_call($path, $options);
});
} else {
$results = $this->_call($path, $options);
}
return $results;
}
private function _call(string $path, array $options = [])
{
$url = empty($options) ? $this->url . $path : $this->url . $path . '?' . Arr::query($options);
// $response = Http::get($url);
$response = Http::withHeaders(['referer' => route('home')])->get($url);
if (!$response) {
return false;
}
$body = $response->getBody();
$results = json_decode($body);
if (isset($results->status_code)) {
abort(403, $results->status_message);
}
return json_decode($body);
}
}