Passed
Push — wip/remove-deprecations-for-v1... ( f97f5b )
by Tomas Norre
05:15
created

QueueExecutor::executeQueueItem()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.6563

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 31
ccs 14
cts 19
cp 0.7368
crap 6.6563
rs 9.0111
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\Controller\CrawlerController;
23
use AOE\Crawler\Converter\JsonCompatibilityConverter;
24
use AOE\Crawler\CrawlStrategy\CallbackExecutionStrategy;
25
use AOE\Crawler\CrawlStrategy\CrawlStrategyFactory;
26
use TYPO3\CMS\Core\Http\Uri;
27
use TYPO3\CMS\Core\SingletonInterface;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Fetches a URL based on the selected strategy or via a callback.
32
 */
33
class QueueExecutor implements SingletonInterface
34
{
35
    /**
36
     * @var CrawlStrategy
37
     */
38
    protected $crawlStrategy;
39
40 24
    public function __construct(CrawlStrategyFactory $crawlStrategyFactory)
41
    {
42 24
        $this->crawlStrategy = $crawlStrategyFactory->create();
0 ignored issues
show
Documentation Bug introduced by
It seems like $crawlStrategyFactory->create() of type AOE\Crawler\CrawlStrategy\CrawlStrategy is incompatible with the declared type AOE\Crawler\CrawlStrategy of property $crawlStrategy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43 24
    }
44
45
    /**
46
     * Takes a queue record and fetches the contents of the URL.
47
     * In the future, updating the queue item & additional signal/slot/events should also happen in here.
48
     *
49
     * @return array|bool|mixed|string
50
     */
51 7
    public function executeQueueItem(array $queueItem, CrawlerController $crawlerController)
52
    {
53 7
        $parameters = '';
54 7
        if (isset($queueItem['parameters'])) {
55
            // Decode parameters:
56
            /** @var JsonCompatibilityConverter $jsonCompatibleConverter */
57 6
            $jsonCompatibleConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
58 6
            $parameters = $jsonCompatibleConverter->convert($queueItem['parameters']);
59
        }
60
61 7
        if (! is_array($parameters) || empty($parameters)) {
62 6
            return 'ERROR';
63
        }
64 1
        if ($parameters['_CALLBACKOBJ']) {
65 1
            $className = $parameters['_CALLBACKOBJ'];
66 1
            unset($parameters['_CALLBACKOBJ']);
67 1
            $result = GeneralUtility::makeInstance(CallbackExecutionStrategy::class)
68 1
                ->fetchByCallback($className, $parameters, $crawlerController);
69 1
            $result = ['content' => json_encode($result)];
70
        } else {
71
            // Regular FE request
72
            $crawlerId = $this->generateCrawlerIdFromQueueItem($queueItem);
73
74
            // Get result:
75
            $url = new Uri($parameters['url']);
76
            $result = $this->crawlStrategy->fetchUrlContents($url, $crawlerId);
77
            if ($result !== false) {
78
                $result = ['content' => json_encode($result)];
79
            }
80
        }
81 1
        return $result;
82
    }
83
84
    protected function generateCrawlerIdFromQueueItem(array $queueItem): string
85
    {
86
        return $queueItem['qid'] . ':' . md5($queueItem['qid'] . '|' . $queueItem['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
87
    }
88
}
89