PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / WhatsNew / GetWhatsNewCommandHandler.php
ameliabooking / src / Application / Commands / WhatsNew Last commit date
GetWhatsNewCommand.php 6 months ago GetWhatsNewCommandHandler.php 3 months ago
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