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 |