• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tomasnorre / crawler / 11237471329

08 Oct 2024 02:20PM UTC coverage: 68.586% (-1.3%) from 69.862%
11237471329

push

github

web-flow
ci: Update coveralls workflow (#1109)

1834 of 2674 relevant lines covered (68.59%)

3.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/Classes/QueueExecutor.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler;
6

7
/*
8
 * (c) 2022 Tomas Norre Mikkelsen <tomasnorre@gmail.com>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21

22
use AOE\Crawler\Controller\CrawlerController;
23
use AOE\Crawler\Converter\JsonCompatibilityConverter;
24
use AOE\Crawler\CrawlStrategy\CallbackExecutionStrategy;
25
use AOE\Crawler\CrawlStrategy\CrawlStrategyFactory;
26
use AOE\Crawler\CrawlStrategy\CrawlStrategyInterface;
27
use AOE\Crawler\Event\AfterUrlCrawledEvent;
28
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
29
use TYPO3\CMS\Core\Http\Uri;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32

33
/**
34
 * Fetches a URL based on the selected strategy or via a callback.
35
 * @internal since v9.2.5
36
 */
37
class QueueExecutor implements SingletonInterface
38
{
39
    protected CrawlStrategyInterface $crawlStrategy;
40

41
    public function __construct(
42
        CrawlStrategyFactory $crawlStrategyFactory,
43
        private readonly EventDispatcher $eventDispatcher
44
    ) {
45
        $this->crawlStrategy = $crawlStrategyFactory->create();
21✔
46
    }
47

48
    /**
49
     * Takes a queue record and fetches the contents of the URL.
50
     * In the future, updating the queue item & additional signal/slot/events should also happen in here.
51
     *
52
     * @return array|bool|mixed|string
53
     */
54
    public function executeQueueItem(array $queueItem, CrawlerController $crawlerController)
55
    {
56
        $parameters = '';
8✔
57
        if (isset($queueItem['parameters'])) {
8✔
58
            // Decode parameters:
59
            /** @var JsonCompatibilityConverter $jsonCompatibleConverter */
60
            $jsonCompatibleConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
7✔
61
            $parameters = $jsonCompatibleConverter->convert($queueItem['parameters']);
7✔
62
        }
63

64
        if (!is_array($parameters) || empty($parameters)) {
8✔
65
            return 'ERROR';
6✔
66
        }
67
        if (isset($parameters['_CALLBACKOBJ'])) {
2✔
68
            $className = $parameters['_CALLBACKOBJ'];
1✔
69
            unset($parameters['_CALLBACKOBJ']);
1✔
70
            $result = GeneralUtility::makeInstance(CallbackExecutionStrategy::class)
1✔
71
                ->fetchByCallback($className, $parameters, $crawlerController);
1✔
72
            $result = [
1✔
73
                'content' => json_encode($result),
1✔
74
            ];
1✔
75
        } else {
76
            // Regular FE request
77
            $crawlerId = $this->generateCrawlerIdFromQueueItem($queueItem);
1✔
78

79
            $url = new Uri($parameters['url']);
1✔
80
            $result = $this->crawlStrategy->fetchUrlContents($url, $crawlerId);
1✔
81
            if ($result !== false) {
1✔
82
                $result = [
×
83
                    'content' => json_encode($result),
×
84
                ];
×
85
                $this->eventDispatcher->dispatch(new AfterUrlCrawledEvent($parameters['url'], $result));
×
86
            }
87
        }
88
        return $result;
2✔
89
    }
90

91
    protected function generateCrawlerIdFromQueueItem(array $queueItem): string
92
    {
93
        return $queueItem['qid'] . ':' . md5(
1✔
94
            $queueItem['qid'] . '|' . $queueItem['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
1✔
95
        );
1✔
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc