Passed
Push — version-matrix ( df0591 )
by Tomas Norre
13:39 queued 08:00
created

Process   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Test Coverage

Coverage 39.34%

Importance

Changes 0
Metric Value
eloc 54
c 0
b 0
f 0
dl 0
loc 255
ccs 24
cts 61
cp 0.3934
rs 10
wmc 28

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setAssignedItemsCount() 0 3 1
A getItemsToProcess() 0 3 1
A setActive() 0 3 1
A setSystemProcessId() 0 3 1
A setDeleted() 0 3 1
A getTimeForFirstItem() 0 4 1
A getTimeForLastItem() 0 4 1
A __construct() 0 4 1
A setTtl() 0 3 1
A setProcessId() 0 3 1
A getProcessId() 0 3 1
A getFinallyAssigned() 0 3 1
A getAmountOfItemsProcessed() 0 3 1
A getAssignedItemsCount() 0 3 1
A getTtl() 0 3 1
A isActive() 0 3 1
A getSystemProcessId() 0 3 1
A getRuntime() 0 5 1
A isDeleted() 0 3 1
A getQueueRepository() 0 3 1
A getProgress() 0 13 3
A getState() 0 10 5
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\Property\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 34
    public function __construct()
99
    {
100 34
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
101 34
        $this->queueRepository = $objectManager->get(QueueRepository::class);
102 34
    }
103
104
    /**
105
     * @return string
106
     */
107 14
    public function getProcessId()
108
    {
109 14
        return $this->processId;
110
    }
111
112
    /**
113
     * @param string $processId
114
     */
115 34
    public function setProcessId($processId): void
116
    {
117 34
        $this->processId = $processId;
118 34
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function isActive()
124
    {
125
        return $this->active;
126
    }
127
128
    /**
129
     * @param bool $active
130
     */
131 20
    public function setActive($active): void
132
    {
133 20
        $this->active = $active;
134 20
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function getTtl()
140
    {
141
        return $this->ttl;
142
    }
143
144
    /**
145
     * @param int $ttl
146
     */
147 20
    public function setTtl($ttl): void
148
    {
149 20
        $this->ttl = $ttl;
150 20
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function getAssignedItemsCount()
156
    {
157
        return $this->assignedItemsCount;
158
    }
159
160
    /**
161
     * @param int $assignedItemsCount
162
     */
163 20
    public function setAssignedItemsCount($assignedItemsCount): void
164
    {
165 20
        $this->assignedItemsCount = $assignedItemsCount;
166 20
    }
167
168
    /**
169
     * @return bool
170
     */
171
    public function isDeleted()
172
    {
173
        return $this->deleted;
174
    }
175
176
    /**
177
     * @param bool $deleted
178
     */
179 20
    public function setDeleted($deleted): void
180
    {
181 20
        $this->deleted = $deleted;
182 20
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getSystemProcessId()
188
    {
189
        return $this->systemProcessId;
190
    }
191
192
    /**
193
     * @param string $systemProcessId
194
     */
195 20
    public function setSystemProcessId($systemProcessId): void
196
    {
197 20
        $this->systemProcessId = $systemProcessId;
198 20
    }
199
200
    /**
201
     * Returns the difference between first and last processed item
202
     *
203
     * @return int
204
     */
205
    public function getRuntime()
206
    {
207
        $lastItem = $this->getQueueRepository()->findOldestEntryForProcess($this);
208
        $firstItem = $this->getQueueRepository()->findYoungestEntryForProcess($this);
209
        return $lastItem['exec_time'] - $firstItem['exec_time'];
210
    }
211
212
    /**
213
     * @deprecated
214
     */
215
    public function getTimeForLastItem(): int
216
    {
217
        $entry = $this->getQueueRepository()->findOldestEntryForProcess($this);
218
        return $entry['exec_time'] ?? 0;
219
    }
220
221
    /**
222
     * @deprecated
223
     */
224
    public function getTimeForFirstItem(): int
225
    {
226
        $entry = $this->getQueueRepository()->findYoungestEntryForProcess($this);
227
        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->getQueueRepository()->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->getQueueRepository()->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
    public function getProgress(): float
264
    {
265
        $all = $this->getAssignedItemsCount();
266
        if ($all <= 0) {
267
            return 0.0;
268
        }
269
270
        $res = round((100 / $all) * $this->getAmountOfItemsProcessed());
271
272
        if ($res > 100.0) {
273
            return 100.0;
274
        }
275
        return $res;
276
    }
277
278
    /**
279
     * Return the processes current state
280
     *
281
     * @return string
282
     */
283
    public function getState()
284
    {
285
        if ($this->isActive() && $this->getProgress() < 100) {
286
            $stage = self::STATE_RUNNING;
287
        } elseif (! $this->isActive() && $this->getProgress() < 100) {
288
            $stage = self::STATE_CANCELLED;
289
        } else {
290
            $stage = self::STATE_COMPLETED;
291
        }
292
        return $stage;
293
    }
294
295
    private function getQueueRepository(): QueueRepository
296
    {
297
        return $this->queueRepository ?? GeneralUtility::makeInstance(QueueRepository::class);
298
    }
299
}
300