Completed
Push — issue/413 ( b4128e )
by Tomas Norre
04:49
created

ProcessRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 76.09%

Importance

Changes 0
Metric Value
dl 0
loc 128
ccs 35
cts 46
cp 0.7609
rs 10
c 0
b 0
f 0
wmc 11
lcom 2
cbo 5

7 Methods

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