PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.11
Booking for Appointments and Events Calendar – Amelia v1.2.11
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 2 years ago GetWhatsNewCommandHandler.php 2 years ago
GetWhatsNewCommandHandler.php
94 lines
1 <?php
2 /**
3 * @copyright © TMS-Plugins. All rights reserved.
4 * @licence See LICENCE.md for license details.
5 */
6
7 namespace AmeliaBooking\Application\Commands\WhatsNew;
8
9 use AmeliaBooking\Application\Commands\CommandHandler;
10 use AmeliaBooking\Application\Commands\CommandResult;
11 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
12 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
13 use AmeliaBooking\Domain\Entity\Entities;
14 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
15 use Exception;
16 use Interop\Container\Exception\ContainerException;
17 use Slim\Exception\ContainerValueNotFoundException;
18 use DOMDocument;
19 use DOMXPath;
20 use stdClass;
21
22 /**
23 * Class GetWhatsNewCommandHandler
24 *
25 * @package AmeliaBooking\Application\Commands\WhatsNew
26 */
27 class GetWhatsNewCommandHandler extends CommandHandler
28 {
29 /**
30 * @param GetWhatsNewCommand $command
31 *
32 * @return CommandResult
33 * @throws ContainerValueNotFoundException
34 * @throws AccessDeniedException
35 * @throws InvalidArgumentException
36 * @throws QueryExecutionException
37 * @throws Exception
38 * @throws ContainerException
39 */
40 public function handle(GetWhatsNewCommand $command)
41 {
42 if (!$this->getContainer()->getPermissionsService()->currentUserCanRead(Entities::DASHBOARD)) {
43 throw new AccessDeniedException('You are not allowed to read news.');
44 }
45
46 $blogPosts = null;
47
48 $result = new CommandResult();
49
50 $blogPageContent = $this->getPageContent('https://wpamelia.com/blog/');
51
52 $blogPostsElements = $blogPageContent->query('//article[contains(@class, "post-list-post")]');
53
54 foreach ($blogPostsElements as $blogPostElement) {
55 $post = new stdClass();
56
57 $post->href = $blogPostElement->getElementsByTagName('a')[0]->getAttribute('href');
58
59 $post->title = $blogPostElement->getElementsByTagName('h2')->item(0)->textContent;
60
61 $blogPosts[] = $post;
62 }
63
64 $result->setResult(CommandResult::RESULT_SUCCESS);
65 $result->setMessage('Successfully retrieved news.');
66 $result->setData(
67 [
68 'blogPosts' => $blogPosts
69 ]
70 );
71
72 return $result;
73 }
74
75 /**
76 * @param string $URL
77 * @return DOMXPath
78 */
79 public function getPageContent(string $URL)
80 {
81 $curl = curl_init($URL);
82 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
83 $pageData = curl_exec($curl);
84 curl_close($curl);
85
86 $dom = new DOMDocument();
87 libxml_use_internal_errors(true);
88 $dom->loadHTML($pageData);
89 libxml_clear_errors();
90
91 return new DOMXPath($dom);
92 }
93 }
94