* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ declare(strict_types=1); namespace PrestaShop\Module\PsEditionBasic\Controller; use Context; use GuzzleHttp\Psr7\Request; use Prestashop\ModuleLibGuzzleAdapter\ClientFactory; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; use Symfony\Component\HttpFoundation\JsonResponse; (new \Symfony\Component\Dotenv\Dotenv(true))->loadEnv(dirname(__DIR__, 2) . '/.env'); define('URL', getenv('PS_EDITION_BASIC_PS_ACADEMY_URL')); define('KEY', getenv('PS_EDITION_BASIC_PS_ACADEMY_KEY')); class AdminPsEditionBasicPsAcademyController extends FrameworkBundleAdminController { private function getProductsId(): array { $client = (new ClientFactory())->getClient(); $requestVideoHosted = new Request('GET', URL . '/api/products?filter[mpn]=[videoHosted]&ws_key=' . KEY . '&output_format=JSON'); $requestLiveHosted = new Request('GET', URL . '/api/products?filter[mpn]=[liveHosted]&ws_key=' . KEY . '&output_format=JSON'); $responseVideoHosted = $client->sendRequest($requestVideoHosted); $responseLiveHosted = $client->sendRequest($requestLiveHosted); $responseContentsVideoHosted = json_decode($responseVideoHosted->getBody()->getContents(), true); $responseContentsLiveHosted = json_decode($responseLiveHosted->getBody()->getContents(), true); $httpStatusCodeVideoHosted = $responseVideoHosted->getStatusCode(); $httpStatusCodeLiveHosted = $responseLiveHosted->getStatusCode(); if ($httpStatusCodeVideoHosted >= 400 || $httpStatusCodeLiveHosted >= 400) { return []; } return array_column(array_merge($responseContentsLiveHosted['products'], $responseContentsVideoHosted['products']), 'id'); } private function createObjectFromResponse(array $response): array { $context = Context::getContext(); $locale = 'gb'; if ($context->language->iso_code) { $locale = $context->language->iso_code; $availableLang = ['fr', 'it', 'es']; if ($locale === 'en' || !in_array($locale, $availableLang)) { $locale = 'gb'; } } $langIds = [ 'fr' => 0, 'gb' => 1, 'es' => 2, 'it' => 3, ]; $client = (new ClientFactory())->getClient(); $requestCategory = new Request('GET', URL . '/api/categories/' . $response['id_category_default'] . '?ws_key=' . KEY . '&output_format=JSON'); $responseCategory = $client->sendRequest($requestCategory); $httpStatusCode = $responseCategory->getStatusCode(); if ($httpStatusCode > 300) { return []; } $responseContents = json_decode($responseCategory->getBody()->getContents(), true); $category = $responseContents['category']['link_rewrite'][$langIds[$locale]]['value']; $link_rewrite = $response['link_rewrite'][$langIds[$locale]]['value']; $productUrl = URL . '/' . $locale . '/' . $category . '/' . $response['id'] . '-' . $link_rewrite . '.html'; $tmp = [ 'name' => $response['name'][$langIds[$locale]]['value'], 'description' => $response['description'][$langIds[$locale]]['value'], 'url' => $productUrl, ]; return $tmp; } /** * Handle the call back requests * * @return JsonResponse */ public function getProducts(): JsonResponse { $ids = $this->getProductsId(); $client = (new ClientFactory())->getClient(); $products = []; if (!empty($ids)) { foreach ($ids as $id) { $request = new Request('GET', URL . '/api/products/' . $id . '?ws_key=' . KEY . '&output_format=JSON'); $response = $client->sendRequest($request); $httpStatusCode = $response->getStatusCode(); if ($httpStatusCode <= 300) { $responseContents = json_decode($response->getBody()->getContents(), true); $tempObject = $this->createObjectFromResponse($responseContents['product']); array_push($products, $tempObject); } } } return new JsonResponse($products); } }