• 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

0.0
/Classes/Controller/Backend/BackendModuleCrawlerProcessController.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler\Controller\Backend;
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\Crawler;
23
use AOE\Crawler\Domain\Repository\ProcessRepository;
24
use AOE\Crawler\Domain\Repository\QueueRepository;
25
use AOE\Crawler\Exception\ProcessException;
26
use AOE\Crawler\Hooks\CrawlerHookInterface;
27
use AOE\Crawler\Service\BackendModuleLinkService;
28
use AOE\Crawler\Service\ProcessService;
29
use AOE\Crawler\Utility\MessageUtility;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use TYPO3\CMS\Backend\Template\ModuleTemplate;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Core\Utility\MathUtility;
35

36
/**
37
 * @internal since v12.0.0
38
 */
39
final class BackendModuleCrawlerProcessController extends AbstractBackendModuleController implements BackendModuleControllerInterface
40
{
41
    public const BACKEND_MODULE = 'web_site_crawler_process';
42

43
    public function __construct(
44
        private readonly ProcessService $processService,
45
        private readonly ProcessRepository $processRepository,
46
        private readonly QueueRepository $queueRepository,
47
        private readonly BackendModuleLinkService $backendModuleLinkService,
48
        private readonly Crawler $crawler
49
    ) {
50
    }
×
51

52
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
53
    {
54
        $this->pageUid = (int) ($request->getQueryParams()['id'] ?? -1);
×
55
        $this->moduleTemplate = $this->setupView($request, $this->pageUid);
×
56
        $this->moduleTemplate->assign('currentPageId', $this->pageUid);
×
57
        $this->moduleTemplate = $this->moduleTemplate->makeDocHeaderModuleMenu(
×
58
            [
×
59
                'id' => $request->getQueryParams()['id'] ?? -1,
×
60
            ]
×
61
        );
×
62
        $this->moduleTemplate = $this->assignValues($request);
×
63
        $this->runRefreshHooks();
×
64

65
        return $this->moduleTemplate->renderResponse('Backend/ProcessOverview');
×
66
    }
67

68
    private function assignValues(ServerRequestInterface $request): ModuleTemplate
69
    {
70
        try {
71
            $this->handleProcessOverviewActions($request);
×
72
        } catch (\Throwable $e) {
×
73
            $this->isErrorDetected = true;
×
74
            MessageUtility::addErrorMessage($e->getMessage());
×
75
        }
76

77
        $mode = $request->getParsedBody()['processListMode'] ?? $request->getQueryParams()['processListMode'] ?? 'simple';
×
78
        $allProcesses = $mode === 'simple' ? $this->processRepository->findAllActive() : $this->processRepository->findAll();
×
79
        $isCrawlerEnabled = !$this->crawler->isDisabled() && !$this->isErrorDetected;
×
80
        $currentActiveProcesses = $this->processRepository->findAllActive()->count();
×
81
        $maxActiveProcesses = MathUtility::forceIntegerInRange($this->extensionSettings['processLimit'], 1, 99, 1);
×
82

83
        return $this->moduleTemplate->assignMultiple([
×
84
            'pageId' => $this->pageUid,
×
85
            'refreshLink' => $this->backendModuleLinkService->getRefreshLink($this->moduleTemplate, $this->pageUid),
×
86
            'addLink' => $this->backendModuleLinkService->getAddLink(
×
87
                $this->moduleTemplate,
×
88
                $currentActiveProcesses,
×
89
                $maxActiveProcesses,
×
90
                $isCrawlerEnabled
×
91
            ),
×
92
            'modeLink' => $this->backendModuleLinkService->getModeLink($this->moduleTemplate, $mode),
×
93
            'flushLink' => $this->backendModuleLinkService->getFlushLink($this->moduleTemplate),
×
94
            'enableDisableToggle' => $this->backendModuleLinkService->getEnableDisableLink(
×
95
                $this->moduleTemplate,
×
96
                $isCrawlerEnabled
×
97
            ),
×
98
            'processCollection' => $allProcesses,
×
99
            'cliPath' => $this->processService->getCrawlerCliPath(),
×
100
            'isCrawlerEnabled' => $isCrawlerEnabled,
×
101
            'totalUnprocessedItemCount' => $this->queueRepository->countAllPendingItems(),
×
102
            'assignedUnprocessedItemCount' => $this->queueRepository->countAllAssignedPendingItems(),
×
103
            'activeProcessCount' => $currentActiveProcesses,
×
104
            'maxActiveProcessCount' => $maxActiveProcesses,
×
105
            'mode' => $mode,
×
106
            'displayActions' => 0,
×
107
        ]);
×
108
    }
109

110
    /**
111
     * Method to handle incoming actions of the process overview
112
     *
113
     * @throws ProcessException
114
     */
115
    private function handleProcessOverviewActions(ServerRequestInterface $request): void
116
    {
117
        $action = $request->getParsedBody()['action'] ?? $request->getQueryParams()['action'] ?? null;
×
118

119
        switch ($action) {
120
            case 'stopCrawling':
×
121
                //set the cli status to disable (all processes will be terminated)
122
                $this->crawler->setDisabled(true);
×
123
                break;
×
124
            case 'addProcess':
×
125
                if ($this->processService->startProcess() === false) {
×
126
                    throw new ProcessException($this->getLanguageService()->sL(
×
127
                        'LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.newprocesserror'
×
128
                    ));
×
129
                }
130
                MessageUtility::addNoticeMessage(
×
131
                    $this->getLanguageService()->sL(
×
132
                        'LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.newprocess'
×
133
                    )
×
134
                );
×
135
                break;
×
136
            case 'flushProcess':
×
137
                $processes = $this->processRepository->findAll();
×
138
                foreach ($processes as $process) {
×
139
                    $this->processRepository->removeByProcessId($process->getProcessId());
×
140
                }
141
                break;
×
142
            case 'resumeCrawling':
×
143
            default:
144
                //set the cli status to end (all processes will be terminated)
145
                $this->crawler->setDisabled(false);
×
146
                break;
×
147
        }
148
    }
149

150
    /**
151
     * Activate hooks
152
     */
153
    private function runRefreshHooks(): void
154
    {
155
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['refresh_hooks'] ?? [] as $objRef) {
×
156
            /** @var CrawlerHookInterface $hookObj */
157
            $hookObj = GeneralUtility::makeInstance($objRef);
×
158
            if (is_object($hookObj)) {
×
159
                $hookObj->crawler_init();
×
160
            }
161
        }
162
    }
163
}
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