Completed
Push — typo3v9 ( efc308...b63611 )
by Tomas Norre
06:24
created

QueueExecutor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 41.94%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 13
cts 31
cp 0.4194
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A executeQueueItem() 0 33 4
A generateCrawlerIdFromQueueItem() 0 4 1
1
<?php
2
declare(strict_types=1);
3
namespace AOE\Crawler;
4
5
/*
6
 * This file is part of the TYPO3 Crawler Extension.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
19
use AOE\Crawler\CrawlStrategy\CallbackExecutionStrategy;
20
use AOE\Crawler\CrawlStrategy\GuzzleExecutionStrategy;
21
use AOE\Crawler\CrawlStrategy\SubProcessExecutionStrategy;
22
use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
23
use AOE\Crawler\Controller\CrawlerController;
24
use AOE\Crawler\Event\EventDispatcher;
25
use TYPO3\CMS\Core\Http\Uri;
26
use TYPO3\CMS\Core\SingletonInterface;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * Fetches a URL based on the selected strategy or via a callback.
31
 */
32
class QueueExecutor implements SingletonInterface
33
{
34
    /**
35
     * @var GuzzleExecutionStrategy|SubProcessExecutionStrategy
36
     */
37
    protected $selectedStrategy;
38
39
    /**
40
     * @var array
41
     */
42
    protected $extensionSettings;
43
44 29
    public function __construct(ExtensionConfigurationProvider $configurationProvider = null)
45
    {
46 29
        $configurationProvider = $configurationProvider ?? GeneralUtility::makeInstance(ExtensionConfigurationProvider::class);
47 29
        $settings = $configurationProvider->getExtensionConfiguration();
48 29
        $this->extensionSettings = is_array($settings) ? $settings : [];
49 29
        if ($this->extensionSettings['makeDirectRequests']) {
50 1
            $this->selectedStrategy = GeneralUtility::makeInstance(SubProcessExecutionStrategy::class);
51
        } else {
52 28
            $this->selectedStrategy = GeneralUtility::makeInstance(GuzzleExecutionStrategy::class);
53
        }
54 29
    }
55
56
    /**
57
     * Takes a queue record and fetches the contents of the URL.
58
     * In the future, updating the queue item & additional signal/slot/events should also happen in here.
59
     *
60
     * @param array $queueItem
61
     * @param CrawlerController $crawlerController
62
     * @return array|bool|mixed|string
63
     */
64 1
    public function executeQueueItem(array $queueItem, CrawlerController $crawlerController)
65
    {
66
        // Decode parameters:
67 1
        $parameters = unserialize($queueItem['parameters'] ?? '');
68 1
        $result = 'ERROR';
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69 1
        if (!is_array($parameters)) {
70 1
            return 'ERROR';
71
        }
72
        if ($parameters['_CALLBACKOBJ']) {
73
            $className = $parameters['_CALLBACKOBJ'];
74
            unset($parameters['_CALLBACKOBJ']);
75
            $result = GeneralUtility::makeInstance(CallbackExecutionStrategy::class)
76
                ->fetchByCallback($className, $parameters, $crawlerController);
77
            $result = ['content' => serialize($result)];
78
        } else {
79
            // Regular FE request
80
            $crawlerId = $this->generateCrawlerIdFromQueueItem($queueItem);
81
82
            // Get result:
83
            $url = new Uri($parameters['url']);
84
            $result = $this->selectedStrategy->fetchUrlContents($url, $crawlerId);
85
            if ($result !== false) {
86
                $result = ['content' => serialize($result)];
87
            }
88
89
            EventDispatcher::getInstance()->post(
90
                'urlCrawled',
91
                $queueItem['set_id'],
92
                ['url' => $parameters['url'], 'result' => $result]
93
            );
94
        }
95
        return $result;
96
    }
97
98
    protected function generateCrawlerIdFromQueueItem(array $queueItem): string
99
    {
100
        return $queueItem['qid'] . ':' . md5($queueItem['qid'] . '|' . $queueItem['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
101
    }
102
}
103