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

ProcessService::startRequiredProcesses()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
nc 17
nop 0
dl 0
loc 26
ccs 0
cts 0
cp 0
crap 72
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Service;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2020 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
32
use AOE\Crawler\Controller\CrawlerController;
33
use AOE\Crawler\Domain\Repository\ProcessRepository;
34
use AOE\Crawler\Domain\Repository\QueueRepository;
35
use AOE\Crawler\Exception\ProcessException;
36
use AOE\Crawler\Utility\PhpBinaryUtility;
37
use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
38
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
39
use TYPO3\CMS\Core\Core\Environment;
40
use TYPO3\CMS\Core\Utility\CommandUtility;
41
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
42
use TYPO3\CMS\Core\Utility\GeneralUtility;
43
use TYPO3\CMS\Extbase\Object\ObjectManager;
44
45
/**
46
 * Class ProcessService
47
 *
48
 * @package AOE\Crawler\Service
49
 * @ignoreAnnotation("noRector")
50
 */
51
class ProcessService
52
{
53
    use PublicMethodDeprecationTrait;
0 ignored issues
show
Bug introduced by
The trait TYPO3\CMS\Core\Compatibi...cMethodDeprecationTrait requires the property $deprecatedPublicMethods which is not provided by AOE\Crawler\Service\ProcessService.
Loading history...
54
    use PublicPropertyDeprecationTrait;
0 ignored issues
show
Bug introduced by
The trait TYPO3\CMS\Core\Compatibi...ropertyDeprecationTrait requires the property $deprecatedPublicProperties which is not provided by AOE\Crawler\Service\ProcessService.
Loading history...
55
56
    /**
57
     * @var int
58
     */
59
    private $timeToLive;
60
61
    /**
62
     * @var \AOE\Crawler\Domain\Repository\ProcessRepository
63
     */
64
    private $processRepository;
65
66
    /**
67
     * @var array
68
     */
69
    private $extensionSettings;
70
71
    /**
72
     * the constructor
73
     */
74 8
    public function __construct()
75
    {
76 8
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
77 8
        $this->processRepository = $objectManager->get(ProcessRepository::class);
78 8
        $this->queueRepository = $objectManager->get(QueueRepository::class);
0 ignored issues
show
Bug Best Practice introduced by
The property queueRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79 8
        $this->extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
80 8
        $this->timeToLive = (int) $this->extensionSettings['processMaxRunTime'];
81 8
        $this->countInARun = (int) $this->extensionSettings['countInARun'];
0 ignored issues
show
Bug Best Practice introduced by
The property countInARun does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82 8
        $this->processLimit = (int) $this->extensionSettings['processLimit'];
0 ignored issues
show
Bug Best Practice introduced by
The property processLimit does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83 8
        $this->verbose = (bool) $this->extensionSettings['processVerbose'];
0 ignored issues
show
Bug Best Practice introduced by
The property verbose does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84 8
    }
85
86
    /**
87
     * starts new process
88
     * @throws ProcessException if no crawler process was started
89
     */
90 1
    public function startProcess(): bool
91
    {
92 1
        $ttl = (time() + $this->timeToLive - 1);
93 1
        $current = $this->processRepository->countNotTimeouted($ttl);
94
95
        // Check whether OS is Windows
96 1
        if (Environment::isWindows()) {
97
            $completePath = 'start ' . $this->getCrawlerCliPath();
98
        } else {
99 1
            $completePath = '(' . $this->getCrawlerCliPath() . ' &) > /dev/null';
100
        }
101
102 1
        $fileHandler = CommandUtility::exec($completePath);
103 1
        if ($fileHandler === false) {
104
            throw new ProcessException('could not start process!');
105
        }
106 1
        for ($i = 0; $i < 10; $i++) {
107 1
            if ($this->processRepository->countNotTimeouted($ttl) > $current) {
108 1
                return true;
109
            }
110
            sleep(1);
111
        }
112
        throw new ProcessException('Something went wrong: process did not appear within 10 seconds.');
113
    }
114
115
    /**
116
     * Returns the path to start the crawler from the command line
117
     */
118 7
    public function getCrawlerCliPath(): string
119
    {
120 7
        $phpPath = PhpBinaryUtility::getPhpBinary();
121 7
        $typo3BinaryPath = ExtensionManagementUtility::extPath('core') . 'bin/';
122 7
        $cliPart = 'typo3 crawler:processQueue';
123
        // Don't like the spacing, but don't have an better idea for now
124 7
        $scriptPath = $phpPath . ' ' . $typo3BinaryPath . $cliPart;
125
126 7
        if (Environment::isWindows()) {
127
            $scriptPath = str_replace('/', '\\', $scriptPath);
128
        }
129
130 7
        return ltrim($scriptPath);
131
    }
132
}
133