Completed
Push — issue/408 ( 89ca5e )
by Tomas Norre
04:36
created

ProcessRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 73.17%

Importance

Changes 0
Metric Value
dl 0
loc 123
ccs 30
cts 41
cp 0.7317
rs 10
c 0
b 0
f 0
wmc 11
lcom 2
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 30 5
A findByProcessId() 0 10 1
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $collection; (AOE\Crawler\Domain\Model\ProcessCollection) is incompatible with the return type declared by the interface TYPO3\CMS\Extbase\Persis...itoryInterface::findAll of type TYPO3\CMS\Extbase\Persis...ryResultInterface|array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
    }
85
86
    /**
87
     * @param $processId
88
     * @return object
89
     */
90
    public function findByProcessId($processId)
91
    {
92
        $query = $this->createQuery();
93
        $querySettings = $query->getQuerySettings();
94
        $querySettings->setRespectStoragePage(false);
95
        $querySettings->setIgnoreEnableFields(false);
96
        $query->setQuerySettings($querySettings);
97
        $query->matching($query->equals('process_id', $processId));
98
        return $query->execute()->getFirst();
99
    }
100
101
    /**
102
     * This method is used to count all processes in the process table.
103
     *
104
     * @param  string $where Where clause
105
     *
106
     * @return integer
107
     */
108 5
    public function countAll($where = '1 = 1')
109
    {
110 5
        return $this->countByWhere($where);
111
    }
112
113
    /**
114
     * Returns the number of active processes.
115
     *
116
     * @return integer
117
     */
118 1
    public function countActive()
119
    {
120 1
        return $this->countByWhere('active = 1 AND deleted = 0');
121
    }
122
123
    /**
124
     * Returns the number of processes that live longer than the given timestamp.
125
     *
126
     * @param  integer $ttl
127
     *
128
     * @return integer
129
     */
130 1
    public function countNotTimeouted($ttl)
131
    {
132 1
        return $this->countByWhere('deleted = 0 AND ttl > ' . intval($ttl));
133
    }
134
135
    /**
136
     * Get limit clause
137
     *
138
     * @param  integer $itemCount
139
     * @param  integer $offset
140
     *
141
     * @return string
142
     */
143 11
    public static function getLimitFromItemCountAndOffset($itemCount, $offset)
144
    {
145 11
        $itemCount = filter_var($itemCount, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'default' => 20]]);
146 11
        $offset = filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'default' => 0]]);
147 11
        $limit = $offset . ', ' . $itemCount;
148
149 11
        return $limit;
150
    }
151
152
    /**
153
     * @return void
154
     */
155
    public function deleteProcessesMarkedAsDeleted()
156
    {
157
        $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...
158
    }
159
}
160