Passed
Push — refactor/add-CrawlStrategyFact... ( 6331a4 )
by Tomas Norre
07:44
created

QueueExecutor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 27.59%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 61
ccs 8
cts 29
cp 0.2759
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCrawlerIdFromQueueItem() 0 3 1
A __construct() 0 3 1
B executeQueueItem() 0 38 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
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\Configuration\ExtensionConfigurationProvider;
23
use AOE\Crawler\Controller\CrawlerController;
24
use AOE\Crawler\Converter\JsonCompatibilityConverter;
25
use AOE\Crawler\CrawlStrategy\CallbackExecutionStrategy;
26
use AOE\Crawler\CrawlStrategy\CrawlStrategy;
27
use AOE\Crawler\CrawlStrategy\GuzzleExecutionStrategy;
28
use AOE\Crawler\CrawlStrategy\SubProcessExecutionStrategy;
29
use AOE\Crawler\Utility\SignalSlotUtility;
30
use TYPO3\CMS\Core\Http\Uri;
31
use TYPO3\CMS\Core\SingletonInterface;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * Fetches a URL based on the selected strategy or via a callback.
36
 */
37
class QueueExecutor implements SingletonInterface
38
{
39
    /**
40
     * @var CrawlStrategy
41
     */
42
    protected $crawlStrategy;
43
44 47
    public function __construct(CrawlStrategy $crawlStrategy)
45
    {
46 47
        $this->crawlStrategy = $crawlStrategy;
47 47
    }
48
49
    /**
50
     * Takes a queue record and fetches the contents of the URL.
51
     * In the future, updating the queue item & additional signal/slot/events should also happen in here.
52
     *
53
     * @return array|bool|mixed|string
54
     */
55 1
    public function executeQueueItem(array $queueItem, CrawlerController $crawlerController)
56
    {
57 1
        $parameters = '';
58 1
        if (isset($queueItem['parameters'])) {
59
            // Decode parameters:
60
            /** @var JsonCompatibilityConverter $jsonCompatibleConverter */
61
            $jsonCompatibleConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
62
            $parameters = $jsonCompatibleConverter->convert($queueItem['parameters']);
63
        }
64
65 1
        if (! is_array($parameters) || empty($parameters)) {
66 1
            return 'ERROR';
67
        }
68
        if ($parameters['_CALLBACKOBJ']) {
69
            $className = $parameters['_CALLBACKOBJ'];
70
            unset($parameters['_CALLBACKOBJ']);
71
            $result = GeneralUtility::makeInstance(CallbackExecutionStrategy::class)
72
                ->fetchByCallback($className, $parameters, $crawlerController);
73
            $result = ['content' => json_encode($result)];
74
        } else {
75
            // Regular FE request
76
            $crawlerId = $this->generateCrawlerIdFromQueueItem($queueItem);
77
78
            // Get result:
79
            $url = new Uri($parameters['url']);
80
            $result = $this->crawlStrategy->fetchUrlContents($url, $crawlerId);
81
            if ($result !== false) {
82
                $result = ['content' => json_encode($result)];
83
            }
84
85
            $signalPayload = ['url' => $parameters['url'], 'result' => $result];
86
            SignalSlotUtility::emitSignal(
87
                self::class,
88
                SignalSlotUtility::SIGNAL_URL_CRAWLED,
89
                $signalPayload
90
            );
91
        }
92
        return $result;
93
    }
94
95
    protected function generateCrawlerIdFromQueueItem(array $queueItem): string
96
    {
97
        return $queueItem['qid'] . ':' . md5($queueItem['qid'] . '|' . $queueItem['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
98
    }
99
}
100