Passed
Push — refactor/backendModule-ValueOb... ( 919364...53454f )
by Tomas Norre
08:09
created

markRequestedProcessesAsNotActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Domain\Repository;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2019 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\Domain\Model\Process;
33
use AOE\Crawler\Domain\Model\ProcessCollection;
34
use TYPO3\CMS\Core\Database\Connection;
35
use TYPO3\CMS\Core\Database\ConnectionPool;
36
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
use TYPO3\CMS\Extbase\Object\ObjectManager;
39
use TYPO3\CMS\Extbase\Persistence\Repository;
40
41
/**
42
 * Class ProcessRepository
43
 *
44
 * @package AOE\Crawler\Domain\Repository
45
 */
46
class ProcessRepository extends Repository
47
{
48
    /**
49
     * @var string
50
     */
51
    protected $tableName = 'tx_crawler_process';
52
53
    /**
54
     * @var QueryBuilder
55
     */
56
    protected $queryBuilder;
57
58
    /**
59
     * @var array
60
     */
61
    protected $extensionSettings = [];
62
63
    /**
64
     * QueueRepository constructor.
65
     */
66 67
    public function __construct()
67
    {
68 67
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
69 67
        parent::__construct($objectManager);
70
71 67
        $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
72
73
        /** @var ExtensionConfigurationProvider $configurationProvider */
74 67
        $configurationProvider = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class);
75 67
        $this->extensionSettings = $configurationProvider->getExtensionConfiguration();
76 67
    }
77
78
    /**
79
     * This method is used to find all cli processes within a limit.
80
     */
81 7
    public function findAll(): ProcessCollection
82
    {
83
        /** @var ProcessCollection $collection */
84 7
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
85 7
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
86
87
        $statement = $queryBuilder
88 7
            ->select('*')
89 7
            ->from($this->tableName)
90 7
            ->orderBy('ttl', 'DESC')
91 7
            ->execute();
92
93 7
        while ($row = $statement->fetch()) {
94 7
            $process = GeneralUtility::makeInstance(Process::class);
95 7
            $process->setProcessId($row['process_id']);
96 7
            $process->setActive($row['active']);
97 7
            $process->setTtl($row['ttl']);
98 7
            $process->setAssignedItemsCount($row['assigned_items_count']);
99 7
            $process->setDeleted($row['deleted']);
100 7
            $process->setSystemProcessId($row['system_process_id']);
101 7
            $collection->append($process);
102
        }
103
104 7
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collection returns the type AOE\Crawler\Domain\Model\ProcessCollection which is incompatible with the return type mandated by TYPO3\CMS\Extbase\Persis...oryInterface::findAll() of TYPO3\CMS\Extbase\Persis...ryResultInterface|array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
105
    }
106
107
    /**
108
     * @param string $processId
109
     */
110 1
    public function findByProcessId($processId)
111
    {
112 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
113
114
        return $queryBuilder
115 1
            ->select('*')
116 1
            ->from($this->tableName)
117 1
            ->where(
118 1
                $queryBuilder->expr()->eq('process_id', $queryBuilder->createNamedParameter($processId, \PDO::PARAM_STR))
119 1
            )->execute()->fetch(0);
120
    }
121
122 3
    public function findAllActive(): ProcessCollection
123
    {
124
        /** @var ProcessCollection $collection */
125 3
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
126 3
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
127
128
        $statement = $queryBuilder
129 3
            ->select('*')
130 3
            ->from($this->tableName)
131 3
            ->where(
132 3
                $queryBuilder->expr()->eq('active', 1),
133 3
                $queryBuilder->expr()->eq('deleted', 0)
134
            )
135 3
            ->orderBy('ttl', 'DESC')
136 3
            ->execute();
137
138 3
        while ($row = $statement->fetch()) {
139 3
            $process = new Process();
140 3
            $process->setProcessId($row['process_id']);
141 3
            $process->setActive($row['active']);
142 3
            $process->setTtl($row['ttl']);
143 3
            $process->setAssignedItemsCount($row['assigned_items_count']);
144 3
            $process->setDeleted($row['deleted']);
145 3
            $process->setSystemProcessId($row['system_process_id']);
146 3
            $collection->append($process);
147
        }
148
149 3
        return $collection;
150
    }
151
152
    /**
153
     * @param string $processId
154
     */
155 4
    public function removeByProcessId($processId): void
156
    {
157 4
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
158
159
        $queryBuilder
160 4
            ->delete($this->tableName)
161 4
            ->where(
162 4
                $queryBuilder->expr()->eq('process_id', $queryBuilder->createNamedParameter($processId, \PDO::PARAM_STR))
163 4
            )->execute();
164 4
    }
165
166
    /**
167
     * Returns the number of active processes.
168
     *
169
     * @return int
170
     * @deprecated Using ProcessRepository->countActive() is deprecated since 9.1.1 and will be removed in v11.x, please use ProcessRepository->findAllActive->count() instead
171
     */
172
    public function countActive()
173
    {
174
        return $this->findAllActive()->count();
175
    }
176
177
    /**
178
     * @return array|null
179
     *
180
     * Function is moved from ProcessCleanUpHook
181
     * TODO: Check why we need both getActiveProcessesOlderThanOneHour and getActiveOrphanProcesses, the get getActiveOrphanProcesses does not really check for Orphan in this implementation.
182
     */
183 1
    public function getActiveProcessesOlderThanOneHour()
184
    {
185 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
186 1
        $activeProcesses = [];
187
        $statement = $queryBuilder
188 1
            ->select('process_id', 'system_process_id')
189 1
            ->from($this->tableName)
190 1
            ->where(
191 1
                $queryBuilder->expr()->lte('ttl', intval(time() - $this->extensionSettings['processMaxRunTime'] - 3600)),
192 1
                $queryBuilder->expr()->eq('active', 1)
193
            )
194 1
            ->execute();
195
196 1
        while ($row = $statement->fetch()) {
197 1
            $activeProcesses[] = $row;
198
        }
199
200 1
        return $activeProcesses;
201
    }
202
203
    /**
204
     * Function is moved from ProcessCleanUpHook
205
     *
206
     * @return array
207
     * @see getActiveProcessesOlderThanOneHour
208
     */
209 1
    public function getActiveOrphanProcesses()
210
    {
211 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
212
213
        return $queryBuilder
214 1
            ->select('process_id', 'system_process_id')
215 1
            ->from($this->tableName)
216 1
            ->where(
217 1
                $queryBuilder->expr()->lte('ttl', intval(time() - $this->extensionSettings['processMaxRunTime'])),
218 1
                $queryBuilder->expr()->eq('active', 1)
219
            )
220 1
            ->execute()->fetchAll();
221
    }
222
223
    /**
224
     * Returns the number of processes that live longer than the given timestamp.
225
     */
226 1
    public function countNotTimeouted(int $ttl): int
227
    {
228 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
229
230
        return $queryBuilder
231 1
            ->count('*')
232 1
            ->from($this->tableName)
233 1
            ->where(
234 1
                $queryBuilder->expr()->eq('deleted', 0),
235 1
                $queryBuilder->expr()->gt('ttl', intval($ttl))
236
            )
237 1
            ->execute()
238 1
            ->fetchColumn(0);
239
    }
240
241
    /**
242
     * Get limit clause
243
     * @deprecated Using ProcessRepository::getLimitFromItemCountAndOffset() is deprecated since 9.1.1 and will be removed in v11.x, was not used, so will be removed
244
     */
245 4
    public static function getLimitFromItemCountAndOffset(int $itemCount, int $offset): string
246
    {
247 4
        $itemCount = filter_var($itemCount, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'default' => 20]]);
248 4
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'default' => 0]]);
249
250 4
        return $offset . ', ' . $itemCount;
251
    }
252
253 1
    public function deleteProcessesWithoutItemsAssigned(): void
254
    {
255 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
256
        $queryBuilder
257 1
            ->delete($this->tableName)
258 1
            ->where(
259 1
                $queryBuilder->expr()->eq('assigned_items_count', 0)
260
            )
261 1
            ->execute();
262 1
    }
263
264 1
    public function deleteProcessesMarkedAsDeleted(): void
265
    {
266 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
267
        $queryBuilder
268 1
            ->delete($this->tableName)
269 1
            ->where(
270 1
                $queryBuilder->expr()->eq('deleted', 1)
271
            )
272 1
            ->execute();
273 1
    }
274
275
    public function isProcessActive(string $processId): bool
276
    {
277
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
278
        $isActive = $queryBuilder
279
            ->select('active')
280
            ->from($this->tableName)
281
            ->where(
282
                $queryBuilder->expr()->eq('process_id', $queryBuilder->createNamedParameter($processId))
283
            )
284
            ->orderBy('ttl')
285
            ->execute()
286
            ->fetchColumn(0);
287
288
        return (bool) $isActive;
289
    }
290
291
    /**
292
     * @param $numberOfAffectedRows
293
     */
294 1
    public function updateProcessAssignItemsCount($numberOfAffectedRows, string $processId): void
295
    {
296 1
        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName)
297 1
            ->update(
298 1
                'tx_crawler_process',
299 1
                ['assigned_items_count' => (int) $numberOfAffectedRows],
300 1
                ['process_id' => $processId]
301
            );
302 1
    }
303
304 1
    public function markRequestedProcessesAsNotActive(array $processIds): void
305
    {
306 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
307 1
        $queryBuilder->update('tx_crawler_process')
308 1
            ->where(
309 1
                'NOT EXISTS (
310
                SELECT * FROM tx_crawler_queue
311
                    WHERE tx_crawler_queue.process_id = tx_crawler_process.process_id
312
                    AND tx_crawler_queue.exec_time = 0
313
                )',
314 1
                $queryBuilder->expr()->in('process_id', $queryBuilder->createNamedParameter($processIds, Connection::PARAM_STR_ARRAY)),
315 1
                $queryBuilder->expr()->eq('deleted', 0)
316
            )
317 1
            ->set('active', 0)
318 1
            ->execute();
319 1
    }
320
}
321