Completed
Push — typo3v9 ( 6762b6...ea38b1 )
by Tomas Norre
05:52
created

getLimitFromItemCountAndOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
namespace AOE\Crawler\Domain\Repository;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2019 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
29
use AOE\Crawler\Domain\Model\Process;
30
use AOE\Crawler\Domain\Model\ProcessCollection;
31
use TYPO3\CMS\Core\Database\ConnectionPool;
32
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Class ProcessRepository
37
 *
38
 * @package AOE\Crawler\Domain\Repository
39
 */
40
class ProcessRepository extends AbstractRepository
41
{
42
    /**
43
     * @var string
44
     */
45
    protected $tableName = 'tx_crawler_process';
46
47
    /**
48
     * @var QueryBuilder
49
     */
50
    protected $queryBuilder = QueryBuilder::class;
51
52
    /**
53
     * @var array
54
     */
55
    protected $extensionSettings = [];
56
57
    /**
58
     * QueueRepository constructor.
59
     */
60 46
    public function __construct()
61
    {
62 46
        $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
63
64
        /** @var ExtensionConfigurationProvider $configurationProvider */
65 46
        $configurationProvider = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class);
66 46
        $this->extensionSettings = $configurationProvider->getExtensionConfiguration();
67 46
    }
68
69
    /**
70
     * This method is used to find all cli processes within a limit.
71
     *
72
     * @return ProcessCollection
73
     */
74 2
    public function findAll(): ProcessCollection
75
    {
76
        /** @var ProcessCollection $collection */
77 2
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
78 2
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
79
80
        $statement = $queryBuilder
81 2
            ->select('*')
82 2
            ->from($this->tableName)
83 2
            ->orderBy('ttl', 'DESC')
84 2
            ->execute();
85
86 2
        while ($row = $statement->fetch()) {
87 2
            $process = GeneralUtility::makeInstance(Process::class);
88 2
            $process->setProcessId($row['process_id']);
89 2
            $process->setActive($row['active']);
90 2
            $process->setTtl($row['ttl']);
91 2
            $process->setAssignedItemsCount($row['assigned_items_count']);
92 2
            $process->setDeleted($row['deleted']);
93 2
            $process->setSystemProcessId($row['system_process_id']);
94 2
            $collection->append($process);
95
        }
96
97 2
        return $collection;
98
    }
99
100
    /**
101
     * @param string $processId
102
     * @return mixed
103
     */
104
    public function findByProcessId($processId)
105
    {
106
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
107
        $process = $queryBuilder
108
            ->select('*')
109
            ->from($this->tableName)
110
            ->where(
111
                $queryBuilder->expr()->eq('process_id', $queryBuilder->createNamedParameter($processId, \PDO::PARAM_STR))
112
            )->execute()->fetch(0);
113
114
        return $process;
115
    }
116
117
    /**
118
     * @return ProcessCollection
119
     */
120 1
    public function findAllActive(): ProcessCollection
121
    {
122
        /** @var ProcessCollection $collection */
123 1
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
124 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
125
126
        $statement = $queryBuilder
127 1
            ->select('*')
128 1
            ->from($this->tableName)
129 1
            ->where(
130 1
                $queryBuilder->expr()->eq('active', 1),
131 1
                $queryBuilder->expr()->eq('deleted', 0)
132
            )
133 1
            ->orderBy('ttl', 'DESC')
134 1
            ->execute();
135
136 1
        while ($row = $statement->fetch()) {
137 1
            $process = new Process();
138 1
            $process->setProcessId($row['process_id']);
139 1
            $process->setActive($row['active']);
140 1
            $process->setTtl($row['ttl']);
141 1
            $process->setAssignedItemsCount($row['assigned_items_count']);
142 1
            $process->setDeleted($row['deleted']);
143 1
            $process->setSystemProcessId($row['system_process_id']);
144 1
            $collection->append($process);
145
        }
146
147 1
        return $collection;
148
    }
149
150
    /**
151
     * @param string $processId
152
     *
153
     * @return void
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 integer
170
     */
171 1
    public function countActive()
172
    {
173 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
174
        $count = $queryBuilder
175 1
            ->count('*')
176 1
            ->from($this->tableName)
177 1
            ->where(
178 1
                $queryBuilder->expr()->eq('active', 1),
179 1
                $queryBuilder->expr()->eq('deleted', 0)
180
            )
181 1
            ->execute()
182 1
            ->fetchColumn(0);
183
184 1
        return $count;
185
    }
186
187
    /**
188
     * @return array|null
189
     *
190
     * Function is moved from ProcessCleanUpHook
191
     * TODO: Check why we need both getActiveProcessesOlderThanOneHour and getActiveOrphanProcesses, the get getActiveOrphanProcesses does not really check for Orphan in this implementation.
192
     */
193 1
    public function getActiveProcessesOlderThanOneHour()
194
    {
195 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
196 1
        $activeProcesses = [];
197
        $statement = $queryBuilder
198 1
            ->select('process_id', 'system_process_id')
199 1
            ->from($this->tableName)
200 1
            ->where(
201 1
                $queryBuilder->expr()->lte('ttl', intval(time() - $this->extensionSettings['processMaxRunTime'] - 3600)),
202 1
                $queryBuilder->expr()->eq('active', 1)
203
            )
204 1
            ->execute();
205
206 1
        while ($row = $statement->fetch()) {
207 1
            $activeProcesses[] = $row;
208
        }
209
210 1
        return $activeProcesses;
211
    }
212
213
    /**
214
     * Function is moved from ProcessCleanUpHook
215
     *
216
     * @see getActiveProcessesOlderThanOneHour
217
     * @return array
218
     *
219
     */
220 1
    public function getActiveOrphanProcesses()
221
    {
222 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
223
        $statement = $queryBuilder
224 1
            ->select('process_id', 'system_process_id')
225 1
            ->from($this->tableName)
226 1
            ->where(
227 1
                $queryBuilder->expr()->lte('ttl', intval(time() - $this->extensionSettings['processMaxRunTime'])),
228 1
                $queryBuilder->expr()->eq('active', 1)
229
            )
230 1
            ->execute()->fetchAll();
231
232 1
        return $statement;
233
    }
234
235
    /**
236
     * Returns the number of processes that live longer than the given timestamp.
237
     *
238
     * @param  integer $ttl
239
     *
240
     * @return integer
241
     */
242 1
    public function countNotTimeouted($ttl)
243
    {
244 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
245
        $count = $queryBuilder
246 1
            ->count('*')
247 1
            ->from($this->tableName)
248 1
            ->where(
249 1
                $queryBuilder->expr()->eq('deleted', 0),
250 1
                $queryBuilder->expr()->gt('ttl', intval($ttl))
251
            )
252 1
            ->execute()
253 1
            ->fetchColumn(0);
254
255 1
        return $count;
256
    }
257
258
    /**
259
     * Get limit clause
260
     *
261
     * @param  integer $itemCount
262
     * @param  integer $offset
263
     *
264
     * @return string
265
     */
266 4
    public static function getLimitFromItemCountAndOffset($itemCount, $offset): string
267
    {
268 4
        $itemCount = filter_var($itemCount, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'default' => 20]]);
269 4
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'default' => 0]]);
270 4
        $limit = $offset . ', ' . $itemCount;
271
272 4
        return $limit;
273
    }
274
275
    /**
276
     * @return void
277
     */
278 1
    public function deleteProcessesWithoutItemsAssigned()
279
    {
280 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
281
        $queryBuilder
282 1
            ->delete($this->tableName)
283 1
            ->where(
284 1
                $queryBuilder->expr()->eq('assigned_items_count', 0)
285
            )
286 1
            ->execute();
287 1
    }
288
289 1
    public function deleteProcessesMarkedAsDeleted()
290
    {
291 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
292
        $queryBuilder
293 1
            ->delete($this->tableName)
294 1
            ->where(
295 1
                $queryBuilder->expr()->eq('deleted', 1)
296
            )
297 1
            ->execute();
298 1
    }
299
}
300