Completed
Push — master ( 6a3171...e461ad )
by Tomas Norre
07:14
created

ProcessRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 5
dl 0
loc 108
ccs 30
cts 33
cp 0.9091
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 30 5
A countAll() 0 4 1
A countActive() 0 4 1
A countNotTimeouted() 0 4 1
A getLimitFromItemCountAndOffset() 0 8 1
A deleteProcessesMarkedAsDeleted() 0 4 1
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 7
    public function findAll($orderField = '', $orderDirection = 'DESC', $itemCount = null, $offset = null, $where = '')
56
    {
57
        /** @var ProcessCollection $collection */
58 7
        $collection = GeneralUtility::makeInstance(ProcessCollection::class);
59
60 7
        $orderField = trim($orderField);
61 7
        $orderField = empty($orderField) ? 'process_id' : $orderField;
62
63 7
        $orderDirection = strtoupper(trim($orderDirection));
64 7
        $orderDirection = in_array($orderDirection, ['ASC', 'DESC']) ? $orderDirection : 'DESC';
65
66 7
        $rows = $this->getDB()->exec_SELECTgetRows(
0 ignored issues
show
Deprecated Code introduced by
The method AOE\Crawler\Domain\Repos...ractRepository::getDB() has been deprecated with message: since crawler v6.5.1, will be removed in crawler v9.0.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67 7
            '*',
68 7
            $this->tableName,
69 7
            $where,
70 7
            '',
71 7
            htmlspecialchars($orderField) . ' ' . htmlspecialchars($orderDirection),
72 7
            self::getLimitFromItemCountAndOffset($itemCount, $offset)
73
        );
74
75 7
        if (is_array($rows)) {
76 7
            foreach ($rows as $row) {
77 7
                $process = new Process();
78 7
                $process->setProcessId($row['process_id']);
79 7
                $collection->append($process);
80
            }
81
        }
82
83 7
        return $collection;
84
    }
85
86
    /**
87
     * This method is used to count all processes in the process table.
88
     *
89
     * @param  string $where Where clause
90
     *
91
     * @return integer
92
     */
93 5
    public function countAll($where = '1 = 1')
94
    {
95 5
        return $this->countByWhere($where);
96
    }
97
98
    /**
99
     * Returns the number of active processes.
100
     *
101
     * @return integer
102
     */
103 1
    public function countActive()
104
    {
105 1
        return $this->countByWhere('active = 1 AND deleted = 0');
106
    }
107
108
    /**
109
     * Returns the number of processes that live longer than the given timestamp.
110
     *
111
     * @param  integer $ttl
112
     *
113
     * @return integer
114
     */
115 1
    public function countNotTimeouted($ttl)
116
    {
117 1
        return $this->countByWhere('deleted = 0 AND ttl > ' . intval($ttl));
118
    }
119
120
    /**
121
     * Get limit clause
122
     *
123
     * @param  integer $itemCount
124
     * @param  integer $offset
125
     *
126
     * @return string
127
     */
128 11
    public static function getLimitFromItemCountAndOffset($itemCount, $offset)
129
    {
130 11
        $itemCount = filter_var($itemCount, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'default' => 20]]);
131 11
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'default' => 0]]);
132 11
        $limit = $offset . ', ' . $itemCount;
133
134 11
        return $limit;
135
    }
136
137
    /**
138
     * @return void
139
     */
140
    public function deleteProcessesMarkedAsDeleted()
141
    {
142
        $this->getDB()->exec_DELETEquery('tx_crawler_process', 'deleted = 1');
0 ignored issues
show
Deprecated Code introduced by
The method AOE\Crawler\Domain\Repos...ractRepository::getDB() has been deprecated with message: since crawler v6.5.1, will be removed in crawler v9.0.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
143
    }
144
}
145