Passed
Push — wip/remove-deprecations-for-v1... ( f97f5b )
by Tomas Norre
05:15
created

Process   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Test Coverage

Coverage 89.09%

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 227
ccs 49
cts 55
cp 0.8909
rs 10
wmc 26

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueueRepository() 0 3 1
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 setProcessId() 0 3 1
A getProcessId() 0 3 1
A getState() 0 10 5
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
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;
0 ignored issues
show
Bug introduced by
The trait TYPO3\CMS\Core\Compatibi...cMethodDeprecationTrait requires the property $deprecatedPublicMethods which is not provided by AOE\Crawler\Domain\Model\Process.
Loading history...
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 17
    public function __construct()
89
    {
90 17
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
91 17
        $this->queueRepository = $objectManager->get(QueueRepository::class);
92 17
    }
93
94
    /**
95
     * @return string
96
     */
97 9
    public function getProcessId()
98
    {
99 9
        return $this->processId;
100
    }
101
102
    /**
103
     * @param string $processId
104
     */
105 34
    public function setProcessId($processId): void
106
    {
107 34
        $this->processId = $processId;
108 34
    }
109
110
    /**
111
     * @return bool
112
     */
113 1
    public function isActive()
114
    {
115 1
        return $this->active;
116
    }
117
118
    /**
119
     * @param bool $active
120
     */
121 26
    public function setActive($active): void
122
    {
123 26
        $this->active = $active;
124 26
    }
125
126
    /**
127
     * @return int
128
     */
129 1
    public function getTtl()
130
    {
131 1
        return $this->ttl;
132
    }
133
134
    /**
135
     * @param int $ttl
136
     */
137 26
    public function setTtl($ttl): void
138
    {
139 26
        $this->ttl = $ttl;
140 26
    }
141
142
    /**
143
     * @return int
144
     */
145 1
    public function getAssignedItemsCount()
146
    {
147 1
        return $this->assignedItemsCount;
148
    }
149
150
    /**
151
     * @param int $assignedItemsCount
152
     */
153 26
    public function setAssignedItemsCount($assignedItemsCount): void
154
    {
155 26
        $this->assignedItemsCount = $assignedItemsCount;
156 26
    }
157
158
    /**
159
     * @return bool
160
     */
161 1
    public function isDeleted()
162
    {
163 1
        return $this->deleted;
164
    }
165
166
    /**
167
     * @param bool $deleted
168
     */
169 11
    public function setDeleted($deleted): void
170
    {
171 11
        $this->deleted = $deleted;
172 11
    }
173
174
    /**
175
     * @return string
176
     */
177 1
    public function getSystemProcessId()
178
    {
179 1
        return $this->systemProcessId;
180
    }
181
182
    /**
183
     * @param string $systemProcessId
184
     */
185 11
    public function setSystemProcessId($systemProcessId): void
186
    {
187 11
        $this->systemProcessId = $systemProcessId;
188 11
    }
189
190
    /**
191
     * Returns the difference between first and last processed item
192
     *
193
     * @return int
194
     */
195
    public function getRuntime()
196
    {
197
        $lastItem = $this->getQueueRepository()->findOldestEntryForProcess($this);
198
        $firstItem = $this->getQueueRepository()->findYoungestEntryForProcess($this);
199
        return $lastItem['exec_time'] - $firstItem['exec_time'];
200
    }
201
202
    /**
203
     * Counts the number of items which need to be processed
204
     *
205
     * @return int
206
     * @codeCoverageIgnore
207
     */
208
    public function getAmountOfItemsProcessed()
209
    {
210
        return $this->getQueueRepository()->countExecutedItemsByProcess($this);
211
    }
212
213
    /**
214
     * Counts the number of items which still need to be processed
215
     *
216
     * @return int
217
     * @codeCoverageIgnore
218
     */
219
    public function getItemsToProcess()
220
    {
221
        return $this->getQueueRepository()->countNonExecutedItemsByProcess($this);
222
    }
223
224
    /**
225
     * @codeCoverageIgnore as it's a simple addition function
226
     */
227
    public function getFinallyAssigned(): int
228
    {
229
        return $this->getItemsToProcess() + $this->getAmountOfItemsProcessed();
230
    }
231
232
    /**
233
     * Returns the Progress of a crawling process as a percentage value
234
     */
235 11
    public function getProgress(): float
236
    {
237 11
        $all = $this->getAssignedItemsCount();
238 11
        if ($all <= 0) {
239 2
            return 0.0;
240
        }
241
242 9
        $res = round((100 / $all) * $this->getAmountOfItemsProcessed());
243
244 9
        if ($res > 100.0) {
245 2
            return 100.0;
246
        }
247 7
        return $res;
248
    }
249
250
    /**
251
     * Return the processes current state
252
     *
253
     * @return string
254
     */
255 4
    public function getState()
256
    {
257 4
        if ($this->isActive() && $this->getProgress() < 100) {
258 1
            $stage = self::STATE_RUNNING;
259 3
        } elseif (! $this->isActive() && $this->getProgress() < 100) {
260 1
            $stage = self::STATE_CANCELLED;
261
        } else {
262 2
            $stage = self::STATE_COMPLETED;
263
        }
264 4
        return $stage;
265
    }
266
267
    private function getQueueRepository(): QueueRepository
268
    {
269
        return $this->queueRepository ?? GeneralUtility::makeInstance(QueueRepository::class);
270
    }
271
}
272