<?php
namespace App\Controller\Front;
use App\Entity\Info;
use App\Manager\InfoManager;
use App\Traits\UrlTrait;
use App\Traits\UtilTrait;
use DateTime;
use Exception;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class BlogController extends AbstractController
{
use UtilTrait;
use UrlTrait;
/**
* @Route("/get-blog-posts/", name="get-blog-posts")
*/
public function blogPosts(InfoManager $infoManager): JsonResponse
{
/** @var Info $info */
$info = $infoManager->find(1);
$url = $info->getSocialBlogLink();
$categories = $info->getSocialBlogCategories();
$n_posts = $info->getSocialBlogPosts() ?: 3;
try {
$posts = $this->getPosts($url, $categories, $n_posts);
$response = new JsonResponse(['success' => true, 'posts' => $posts, 'message' => 'success']);
} catch (Exception $e) {
$response = new JsonResponse(['success' => false, 'posts' => [], 'message' => $e->getMessage()]);
}
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
return $response;
}
/**
* @throws Exception
*/
public function getPosts($url, $categories, $n_posts): array
{
$host = $this->getHostUrl($url);
if (is_null($url)) {
throw new Exception('Blog url not defined');
}
$path = $this->getPathUrl($url);
$path = $this->addPath($path, 'wp-json/wp/v2/posts');
$query = $this->getParametersBlog($categories, $n_posts);
$reponse = $this->getClientRequest($host, $path, $query);
$posts = $this->getRequestData($reponse);
return $this->postsToArray($url, $posts);
}
public function postsToArray($url, $posts): array
{
$postsArray = [];
foreach ($posts as $index => $post) {
$postsArray[] = $this->postToArray($url, $post);
}
return $postsArray;
}
public function postToArray($url, $post): array
{
$host = $this->getHostUrl($url);
$path = $this->getPathUrl($url);
$path = $this->addPath($path, 'wp-json/wp/v2/media');
$path = $path . '/' . $post->featured_media;
$mediaRequest = $this->getClientRequest($host, $path);
$mediaData = $this->getRequestData($mediaRequest, null, true);
$date = new Datetime($post->date);
return [
'postid' => $post->id,
'image' => array_key_exists('guid', $mediaData) ? $mediaData['guid']['rendered'] : '',
'title' => $post->title->rendered,
'excerpt' => $post->excerpt->rendered,
'link' => $post->link,
'categories' => $post->categories,
'createdat' => $date->format('d/m/Y'),
];
}
public function getParametersBlog($categories = null, $perPage = 3): array
{
$parameters = [];
$categories = $this->getParameterCategoriesBlog($categories);
if (count($categories) > 0) {
$parameters['categories'] = implode(',', $categories);
}
$perPage = $this->getParameterPerPageBlog($perPage);
$parameters['per_page'] = $perPage;
return $parameters;
}
public function getParameterCategoriesBlog($categories = null): array
{
$categoriesArray = [];
if (!is_null($categories) and $categories != '') {
$categoriesArray = explode(',', $categories);
$categoriesArray = array_map('trim', $categoriesArray);
}
return $categoriesArray;
}
public function getParameterPerPageBlog($perPage = 3)
{
if (is_null($perPage) or $perPage == 0) {
$perPage = 3;
}
return $perPage;
}
public function getClientRequest($url, $path, $query = []): ResponseInterface
{
$client = new Client(['base_uri' => $url]);
$urlArray = parse_url($url);
$urlPath = array_key_exists('path', $urlArray) ? $urlArray['path'] : '';
$urlPath = trim($urlPath, '/');
$path = $urlPath ? ($urlPath . $path) : $path;
return $client->request('GET', $path, ['query' => $query]);
}
public function getRequestData(ResponseInterface $response, $origin = null, $array = false)
{
$body = $response->getBody();
$bodyArray = json_decode($body, $array);
return ($origin) ? $bodyArray->$origin : $bodyArray;
}
}