Completed
Branch test-coverage (951e01)
by Tomas Norre
15:40 queued 13:43
created

getLimitFromItemCountAndOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace AOE\Crawler\Domain\Repository;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 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 TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Class ProcessRepository
34
 *
35
 * @package AOE\Crawler\Domain\Repository
36
 */
37
class ProcessRepository extends AbstractRepository
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $tableName = 'tx_crawler_process';
43
44
    /**
45
     * This method is used to find all cli processes within a limit.
46
     *
47
     * @param  string $orderField
48
     * @param  string $orderDirection
49
     * @param  integer $itemCount
50
     * @param  integer $offset
51
     * @param  string $where
52
     *
53
     * @return ProcessCollection
54
     */
55
    public function findAll($orderField = '', $orderDirection = 'DESC', $itemCount = null, $offset = null, $where = '')
56
    {
57
        /** @var ProcessCollection $collection */
58
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
59
60
        $orderField = trim($orderField);
61
        $orderField = empty($orderField) ? 'process_id' : $orderField;
62
63
        $orderDirection = strtoupper(trim($orderDirection));
64
        $orderDirection = in_array($orderDirection, ['ASC', 'DESC']) ? $orderDirection : 'DESC';
65
66
        $rows = $this->getDB()->exec_SELECTgetRows(
67
            '*',
68
            $this->tableName,
69
            $where,
70
            '',
71
            htmlspecialchars($orderField) . ' ' . htmlspecialchars($orderDirection),
72
            self::getLimitFromItemCountAndOffset($itemCount, $offset)
73
        );
74
75
        if (is_array($rows)) {
76
            foreach ($rows as $row) {
77
                $collection->append(GeneralUtility::makeInstance(Process::class, $row));
78
            }
79
        }
80
81
        return $collection;
82
    }
83
84
    /**
85
     * This method is used to count all processes in the process table.
86
     *
87
     * @param  string $where Where clause
88
     *
89
     * @return integer
90
     */
91 3
    public function countAll($where = '1 = 1')
92
    {
93 3
        return $this->countByWhere($where);
94
    }
95
96
    /**
97
     * Returns the number of active processes.
98
     *
99
     * @return integer
100
     */
101
    public function countActive()
102
    {
103
        return $this->countByWhere('active = 1 AND deleted = 0');
104
    }
105
106
    /**
107
     * Returns the number of processes that live longer than the given timestamp.
108
     *
109
     * @param  integer $ttl
110
     *
111
     * @return integer
112
     */
113
    public function countNotTimeouted($ttl)
114
    {
115
        return $this->countByWhere('deleted = 0 AND ttl > ' . intval($ttl));
116
    }
117
118
    /**
119
     * Get limit clause
120
     *
121
     * @param  integer $itemCount
122
     * @param  integer $offset
123
     *
124
     * @return string
125
     */
126
    public static function getLimitFromItemCountAndOffset($itemCount, $offset)
127
    {
128
        $itemCount = filter_var($itemCount, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'default' => 20]]);
129
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'default' => 0]]);
130
        $limit = $offset . ', ' . $itemCount;
131
132
        return $limit;
133
    }
134
}
135