GetWhatsNewCommandHandler.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\WhatsNew; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 | use Exception; |
| 17 | use Slim\Exception\ContainerValueNotFoundException; |
| 18 | |
| 19 | /** |
| 20 | * Class GetWhatsNewCommandHandler |
| 21 | * |
| 22 | * @package AmeliaBooking\Application\Commands\WhatsNew |
| 23 | */ |
| 24 | class GetWhatsNewCommandHandler extends CommandHandler |
| 25 | { |
| 26 | /** |
| 27 | * @param GetWhatsNewCommand $command |
| 28 | * |
| 29 | * @return CommandResult |
| 30 | * @throws ContainerValueNotFoundException |
| 31 | * @throws AccessDeniedException |
| 32 | * @throws InvalidArgumentException |
| 33 | * @throws QueryExecutionException |
| 34 | * @throws Exception |
| 35 | */ |
| 36 | public function handle(GetWhatsNewCommand $command) |
| 37 | { |
| 38 | if (!$this->getContainer()->getPermissionsService()->currentUserCanRead(Entities::DASHBOARD)) { |
| 39 | throw new AccessDeniedException('You are not allowed to read news.'); |
| 40 | } |
| 41 | |
| 42 | $result = new CommandResult(); |
| 43 | $params = $command->getFields(); |
| 44 | |
| 45 | try { |
| 46 | $response = $this->getPosts($params); |
| 47 | |
| 48 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 49 | $result->setMessage('Successfully retrieved posts.'); |
| 50 | $result->setData([ |
| 51 | 'posts' => $response['data'], |
| 52 | 'filteredPostsNumber' => $response['pagination']['total'], |
| 53 | ]); |
| 54 | } catch (Exception $e) { |
| 55 | $result->setResult(CommandResult::RESULT_ERROR); |
| 56 | $result->setMessage('Failed to retrieve news: ' . $e->getMessage()); |
| 57 | } |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param array $params |
| 64 | * @return array |
| 65 | * @throws Exception |
| 66 | */ |
| 67 | private function getPosts(array $params): array |
| 68 | { |
| 69 | $fullUrl = AMELIA_MIDDLEWARE_URL . 'blog-content?' . http_build_query($params); |
| 70 | |
| 71 | $curl = curl_init(); |
| 72 | |
| 73 | curl_setopt_array($curl, [ |
| 74 | CURLOPT_URL => $fullUrl, |
| 75 | CURLOPT_RETURNTRANSFER => true, |
| 76 | CURLOPT_TIMEOUT => 30, |
| 77 | CURLOPT_CONNECTTIMEOUT => 10, |
| 78 | CURLOPT_HTTPHEADER => [ |
| 79 | 'Content-Type: application/json', |
| 80 | 'Accept: application/json' |
| 81 | ], |
| 82 | CURLOPT_USERAGENT => 'Amelia Plugin/1.0', |
| 83 | CURLOPT_SSL_VERIFYPEER => true, |
| 84 | CURLOPT_SSL_VERIFYHOST => 2, |
| 85 | CURLOPT_FOLLOWLOCATION => true, |
| 86 | CURLOPT_MAXREDIRS => 3 |
| 87 | ]); |
| 88 | |
| 89 | $response = curl_exec($curl); |
| 90 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 91 | $curlError = curl_error($curl); |
| 92 | |
| 93 | if ($response === false) { |
| 94 | throw new Exception('Failed to fetch posts from API: ' . $curlError); |
| 95 | } |
| 96 | |
| 97 | if ($httpCode !== 200) { |
| 98 | throw new Exception('Failed to fetch posts from API. HTTP Code: ' . $httpCode); |
| 99 | } |
| 100 | |
| 101 | $content = json_decode($response, true); |
| 102 | |
| 103 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 104 | throw new Exception('Invalid JSON response from API: ' . json_last_error_msg()); |
| 105 | } |
| 106 | |
| 107 | return $content; |
| 108 | } |
| 109 | } |
| 110 |