Passed
Push — features/addFlushedPagesToCraw... ( 44f5f4...d700c8 )
by Tomas Norre
08:39
created

Process   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
eloc 53
dl 0
loc 252
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setAssignedItemsCount() 0 3 1
A getItemsToProcess() 0 3 1
A setActive() 0 3 1
A getAssignedItemsCount() 0 3 1
A setSystemProcessId() 0 3 1
A getProgress() 0 13 3
A setDeleted() 0 3 1
A __construct() 0 4 1
A getTtl() 0 3 1
A setTtl() 0 3 1
A isActive() 0 3 1
A getState() 0 10 5
A setProcessId() 0 3 1
A getProcessId() 0 3 1
A getFinallyAssigned() 0 3 1
A getSystemProcessId() 0 3 1
A getRuntime() 0 5 1
A isDeleted() 0 3 1
A getAmountOfItemsProcessed() 0 3 1
A getTimeForFirstItem() 0 4 1
A getTimeForLastItem() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Domain\Model;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2020 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use AOE\Crawler\Domain\Repository\QueueRepository;
32
use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
35
use TYPO3\CMS\Extbase\Object\ObjectManager;
36
37
/**
38
 * Class Process
39
 *
40
 * @package AOE\Crawler\Domain\Model
41
 * @ignoreAnnotation("noRector")
42
 */
43
class Process extends AbstractEntity
44
{
45
    use PublicMethodDeprecationTrait;
46
47
    public const STATE_RUNNING = 'running';
48
49
    public const STATE_CANCELLED = 'cancelled';
50
51
    public const STATE_COMPLETED = 'completed';
52
53
    /**
54
     * @var string
55
     */
56
    protected $processId = '';
57
58
    /**
59
     * @var bool
60
     */
61
    protected $active = false;
62
63
    /**
64
     * @var int
65
     */
66
    protected $ttl = 0;
67
68
    /**
69
     * @var int
70
     */
71
    protected $assignedItemsCount = 0;
72
73
    /**
74
     * @var bool
75
     */
76
    protected $deleted = false;
77
78
    /**
79
     * @var string
80
     */
81
    protected $systemProcessId = '';
82
83
    /**
84
     * @var QueueRepository
85
     */
86
    protected $queueRepository;
87
88
    /**
89
     * @var string[]
90
     * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector
91
     * @noRector \Rector\DeadCode\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector
92
     */
93
    private $deprecatedPublicMethods = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicMethods is not used, and could be removed.
Loading history...
94
        'getTimeForFirstItem' => 'Using Process::getTimeForFirstItem() is deprecated since 9.0.1 and will be removed in v11.x',
95
        'getTimeForLastItem' => 'Using Process::getTimeForLastItem() is deprecated since 9.0.1 and will be removed in v11.x',
96
    ];
97
98 10
    public function __construct()
99
    {
100 10
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
101 10
        $this->queueRepository = $objectManager->get(QueueRepository::class);
102 10
    }
103
104
    /**
105
     * @return string
106
     */
107 9
    public function getProcessId()
108
    {
109 9
        return $this->processId;
110
    }
111
112
    /**
113
     * @param string $processId
114
     */
115 30
    public function setProcessId($processId): void
116
    {
117 30
        $this->processId = $processId;
118 30
    }
119
120
    /**
121
     * @return bool
122
     */
123 1
    public function isActive()
124
    {
125 1
        return $this->active;
126
    }
127
128
    /**
129
     * @param bool $active
130
     */
131 22
    public function setActive($active): void
132
    {
133 22
        $this->active = $active;
134 22
    }
135
136
    /**
137
     * @return int
138
     */
139 1
    public function getTtl()
140
    {
141 1
        return $this->ttl;
142
    }
143
144
    /**
145
     * @param int $ttl
146
     */
147 22
    public function setTtl($ttl): void
148
    {
149 22
        $this->ttl = $ttl;
150 22
    }
151
152
    /**
153
     * @return int
154
     */
155 1
    public function getAssignedItemsCount()
156
    {
157 1
        return $this->assignedItemsCount;
158
    }
159
160
    /**
161
     * @param int $assignedItemsCount
162
     */
163 22
    public function setAssignedItemsCount($assignedItemsCount): void
164
    {
165 22
        $this->assignedItemsCount = $assignedItemsCount;
166 22
    }
167
168
    /**
169
     * @return bool
170
     */
171 1
    public function isDeleted()
172
    {
173 1
        return $this->deleted;
174
    }
175
176
    /**
177
     * @param bool $deleted
178
     */
179 4
    public function setDeleted($deleted): void
180
    {
181 4
        $this->deleted = $deleted;
182 4
    }
183
184
    /**
185
     * @return string
186
     */
187 1
    public function getSystemProcessId()
188
    {
189 1
        return $this->systemProcessId;
190
    }
191
192
    /**
193
     * @param string $systemProcessId
194
     */
195 4
    public function setSystemProcessId($systemProcessId): void
196
    {
197 4
        $this->systemProcessId = $systemProcessId;
198 4
    }
199
200
    /**
201
     * Returns the difference between first and last processed item
202
     *
203
     * @return int
204
     */
205 5
    public function getRuntime()
206
    {
207 5
        $lastItem = $this->queueRepository->findOldestEntryForProcess($this);
208 5
        $firstItem = $this->queueRepository->findYoungestEntryForProcess($this);
209 5
        return $lastItem['exec_time'] - $firstItem['exec_time'];
210
    }
211
212
    /**
213
     * @deprecated
214
     */
215 2
    public function getTimeForLastItem(): int
216
    {
217 2
        $entry = $this->queueRepository->findOldestEntryForProcess($this);
218 2
        return $entry['exec_time'] ?? 0;
219
    }
220
221
    /**
222
     * @deprecated
223
     */
224 2
    public function getTimeForFirstItem(): int
225
    {
226 2
        $entry = $this->queueRepository->findYoungestEntryForProcess($this);
227 2
        return $entry['exec_time'] ?? 0;
228
    }
229
230
    /**
231
     * Counts the number of items which need to be processed
232
     *
233
     * @return int
234
     * @codeCoverageIgnore
235
     */
236
    public function getAmountOfItemsProcessed()
237
    {
238
        return $this->queueRepository->countExecutedItemsByProcess($this);
239
    }
240
241
    /**
242
     * Counts the number of items which still need to be processed
243
     *
244
     * @return int
245
     * @codeCoverageIgnore
246
     */
247
    public function getItemsToProcess()
248
    {
249
        return $this->queueRepository->countNonExecutedItemsByProcess($this);
250
    }
251
252
    /**
253
     * @codeCoverageIgnore as it's a simple addition function
254
     */
255
    public function getFinallyAssigned(): int
256
    {
257
        return $this->getItemsToProcess() + $this->getAmountOfItemsProcessed();
258
    }
259
260
    /**
261
     * Returns the Progress of a crawling process as a percentage value
262
     *
263
     * @return float
264
     */
265 5
    public function getProgress()
266
    {
267 5
        $all = $this->getAssignedItemsCount();
268 5
        if ($all <= 0) {
269 2
            return 0;
270
        }
271
272 3
        $res = round((100 / $all) * $this->getAmountOfItemsProcessed());
273
274 3
        if ($res > 100.0) {
275 1
            return 100.0;
276
        }
277 2
        return $res;
278
    }
279
280
    /**
281
     * Return the processes current state
282
     *
283
     * @return string
284
     */
285 4
    public function getState()
286
    {
287 4
        if ($this->isActive() && $this->getProgress() < 100) {
288 1
            $stage = self::STATE_RUNNING;
289 3
        } elseif (! $this->isActive() && $this->getProgress() < 100) {
290 1
            $stage = self::STATE_CANCELLED;
291
        } else {
292 2
            $stage = self::STATE_COMPLETED;
293
        }
294 4
        return $stage;
295
    }
296
}
297