Completed
Push — Testing/fix ( f3936d )
by Tomas Norre
07:32
created

ProcessRepository::findAllActive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 2
rs 9.568
c 0
b 0
f 0
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\Domain\Model\Process;
29
use AOE\Crawler\Domain\Model\ProcessCollection;
30
use AOE\Crawler\Utility\ExtensionSettingUtility;
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 44
    public function __construct()
61
    {
62 44
        $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
63 44
        $this->extensionSettings = ExtensionSettingUtility::loadExtensionSettings();
64 44
    }
65
66
    /**
67
     * This method is used to find all cli processes within a limit.
68
     *
69
     * @return ProcessCollection
70
     */
71 2
    public function findAll(): ProcessCollection
72
    {
73
        /** @var ProcessCollection $collection */
74 2
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
75 2
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
76
77
        $statement = $queryBuilder
78 2
            ->select('*')
79 2
            ->from($this->tableName)
80 2
            ->orderBy('ttl', 'DESC')
81 2
            ->execute();
82
83 2
        while ($row = $statement->fetch()) {
84 2
            $collection->append(GeneralUtility::makeInstance(Process::class, $row));
85
        }
86
87 2
        return $collection;
88
    }
89
90
    /**
91
     * @return ProcessCollection
92
     */
93 1
    public function findAllActive(): ProcessCollection
94
    {
95
        /** @var ProcessCollection $collection */
96 1
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
97 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
98
99
        $statement = $queryBuilder
100 1
            ->select('*')
101 1
            ->from($this->tableName)
102 1
            ->where(
103 1
                $queryBuilder->expr()->eq('active', 1),
104 1
                $queryBuilder->expr()->eq('deleted', 0)
105
            )
106 1
            ->orderBy('ttl', 'DESC')
107 1
            ->execute();
108
109 1
        while ($row = $statement->fetch()) {
110 1
            $collection->append(GeneralUtility::makeInstance(Process::class, $row));
111
        }
112
113 1
        return $collection;
114
    }
115
116
    /**
117
     * @param int $processId
118
     *
119
     * @return void
120
     */
121 4
    public function removeByProcessId($processId): void
122
    {
123 4
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
124
125
        $queryBuilder
126 4
            ->delete($this->tableName)
127 4
            ->where(
128 4
                $queryBuilder->expr()->eq('process_id', $queryBuilder->createNamedParameter($processId))
129 4
            )->execute();
130 4
    }
131
132
    /**
133
     * Returns the number of active processes.
134
     *
135
     * @return integer
136
     */
137 1
    public function countActive()
138
    {
139 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
140
        $count = $queryBuilder
141 1
            ->count('*')
142 1
            ->from($this->tableName)
143 1
            ->where(
144 1
                $queryBuilder->expr()->eq('active', 1),
145 1
                $queryBuilder->expr()->eq('deleted', 0)
146
            )
147 1
            ->execute()
148 1
            ->fetchColumn(0);
149
150 1
        return $count;
151
    }
152
153
    /**
154
     * @return array|null
155
     *
156
     * Function is moved from ProcessCleanUpHook
157
     * TODO: Check why we need both getActiveProcessesOlderThanOneHour and getActiveOrphanProcesses, the get getActiveOrphanProcesses does not really check for Orphan in this implementation.
158
     */
159 1
    public function getActiveProcessesOlderThanOneHour()
160
    {
161 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
162 1
        $activeProcesses = [];
163
        $statement = $queryBuilder
164 1
            ->select('process_id', 'system_process_id')
165 1
            ->from($this->tableName)
166 1
            ->where(
167 1
                $queryBuilder->expr()->lte('ttl', intval(time() - $this->extensionSettings['processMaxRunTime'] - 3600)),
168 1
                $queryBuilder->expr()->eq('active', 1)
169
            )
170 1
            ->execute();
171
172 1
        while ($row = $statement->fetch()) {
173 1
            $activeProcesses[] = $row;
174
        }
175
176 1
        return $activeProcesses;
177
    }
178
179
    /**
180
     * Function is moved from ProcessCleanUpHook
181
     *
182
     * @see getActiveProcessesOlderThanOneHour
183
     * @return array
184
     *
185
     */
186 1
    public function getActiveOrphanProcesses()
187
    {
188 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
189
        $statement = $queryBuilder
190 1
            ->select('process_id', 'system_process_id')
191 1
            ->from($this->tableName)
192 1
            ->where(
193 1
                $queryBuilder->expr()->lte('ttl', intval(time() - $this->extensionSettings['processMaxRunTime'])),
194 1
                $queryBuilder->expr()->eq('active', 1)
195
            )
196 1
            ->execute()->fetchAll();
197
198 1
        return $statement;
199
    }
200
201
    /**
202
     * Returns the number of processes that live longer than the given timestamp.
203
     *
204
     * @param  integer $ttl
205
     *
206
     * @return integer
207
     */
208 1
    public function countNotTimeouted($ttl)
209
    {
210 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
211
        $count = $queryBuilder
212 1
            ->count('*')
213 1
            ->from($this->tableName)
214 1
            ->where(
215 1
                $queryBuilder->expr()->eq('deleted', 0),
216 1
                $queryBuilder->expr()->gt('ttl', intval($ttl))
217
            )
218 1
            ->execute()
219 1
            ->fetchColumn(0);
220
221 1
        return $count;
222
    }
223
224
    /**
225
     * Get limit clause
226
     *
227
     * @param  integer $itemCount
228
     * @param  integer $offset
229
     *
230
     * @return string
231
     */
232 4
    public static function getLimitFromItemCountAndOffset($itemCount, $offset): string
233
    {
234 4
        $itemCount = filter_var($itemCount, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'default' => 20]]);
235 4
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'default' => 0]]);
236 4
        $limit = $offset . ', ' . $itemCount;
237
238 4
        return $limit;
239
    }
240
241
    /**
242
     * @return void
243
     */
244 1
    public function deleteProcessesWithoutItemsAssigned()
245
    {
246 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
247
        $queryBuilder
248 1
            ->delete($this->tableName)
249 1
            ->where(
250 1
                $queryBuilder->expr()->eq('assigned_items_count', 0)
251
            )
252 1
            ->execute();
253 1
    }
254
255 1
    public function deleteProcessesMarkedAsDeleted()
256
    {
257 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
258
        $queryBuilder
259 1
            ->delete($this->tableName)
260 1
            ->where(
261 1
                $queryBuilder->expr()->eq('deleted', 1)
262
            )
263 1
            ->execute();
264 1
    }
265
266
    /**
267
     * Returns an instance of the TYPO3 database class.
268
     *
269
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
270
     * @deprecated since crawler v7.0.0, will be removed in crawler v8.0.0.
271
     */
272
    protected function getDB()
273
    {
274
        return $GLOBALS['TYPO3_DB'];
275
    }
276
}
277