Passed
Push — issue/568 ( 15331a )
by Tomas Norre
08:08
created

Process::getFinallyAssigned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
 */
42
class Process extends AbstractEntity
43
{
44
    use PublicMethodDeprecationTrait;
45
46
    public const STATE_RUNNING = 'running';
47
48
    public const STATE_CANCELLED = 'cancelled';
49
50
    public const STATE_COMPLETED = 'completed';
51
52
    /**
53
     * @var string
54
     */
55
    protected $processId = '';
56
57
    /**
58
     * @var bool
59
     */
60
    protected $active = false;
61
62
    /**
63
     * @var int
64
     */
65
    protected $ttl = 0;
66
67
    /**
68
     * @var int
69
     */
70
    protected $assignedItemsCount = 0;
71
72
    /**
73
     * @var bool
74
     */
75
    protected $deleted = false;
76
77
    /**
78
     * @var string
79
     */
80
    protected $systemProcessId = '';
81
82
    /**
83
     * @var QueueRepository
84
     */
85
    protected $queueRepository;
86
87
    /**
88
     * @var string[]
89
     * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector
90
     * @noRector \Rector\DeadCode\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector
91
     */
92
    private $deprecatedPublicMethods = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicMethods is not used, and could be removed.
Loading history...
93
        'getTimeForFirstItem' => 'Using Process::getTimeForFirstItem() is deprecated since 9.0.1 and will be removed in v11.x',
94
        'getTimeForLastItem' => 'Using Process::getTimeForLastItem() is deprecated since 9.0.1 and will be removed in v11.x',
95
    ];
96
97 10
    public function __construct()
98
    {
99 10
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
100 10
        $this->queueRepository = $objectManager->get(QueueRepository::class);
101 10
    }
102
103
    /**
104
     * @return string
105
     */
106 9
    public function getProcessId()
107
    {
108 9
        return $this->processId;
109
    }
110
111
    /**
112
     * @param string $processId
113
     */
114 30
    public function setProcessId($processId): void
115
    {
116 30
        $this->processId = $processId;
117 30
    }
118
119
    /**
120
     * @return bool
121
     */
122 1
    public function isActive()
123
    {
124 1
        return $this->active;
125
    }
126
127
    /**
128
     * @param bool $active
129
     */
130 22
    public function setActive($active): void
131
    {
132 22
        $this->active = $active;
133 22
    }
134
135
    /**
136
     * @return int
137
     */
138 1
    public function getTtl()
139
    {
140 1
        return $this->ttl;
141
    }
142
143
    /**
144
     * @param int $ttl
145
     */
146 22
    public function setTtl($ttl): void
147
    {
148 22
        $this->ttl = $ttl;
149 22
    }
150
151
    /**
152
     * @return int
153
     */
154 1
    public function getAssignedItemsCount()
155
    {
156 1
        return $this->assignedItemsCount;
157
    }
158
159
    /**
160
     * @param int $assignedItemsCount
161
     */
162 22
    public function setAssignedItemsCount($assignedItemsCount): void
163
    {
164 22
        $this->assignedItemsCount = $assignedItemsCount;
165 22
    }
166
167
    /**
168
     * @return bool
169
     */
170 1
    public function isDeleted()
171
    {
172 1
        return $this->deleted;
173
    }
174
175
    /**
176
     * @param bool $deleted
177
     */
178 4
    public function setDeleted($deleted): void
179
    {
180 4
        $this->deleted = $deleted;
181 4
    }
182
183
    /**
184
     * @return string
185
     */
186 1
    public function getSystemProcessId()
187
    {
188 1
        return $this->systemProcessId;
189
    }
190
191
    /**
192
     * @param string $systemProcessId
193
     */
194 4
    public function setSystemProcessId($systemProcessId): void
195
    {
196 4
        $this->systemProcessId = $systemProcessId;
197 4
    }
198
199
    /**
200
     * Returns the difference between first and last processed item
201
     *
202
     * @return int
203
     */
204 5
    public function getRuntime()
205
    {
206 5
        $lastItem = $this->queueRepository->findOldestEntryForProcess($this);
207 5
        $firstItem = $this->queueRepository->findYoungestEntryForProcess($this);
208 5
        return $lastItem['exec_time'] - $firstItem['exec_time'];
209
    }
210
211
    /**
212
     * @deprecated
213
     */
214 2
    public function getTimeForLastItem(): int
215
    {
216 2
        $entry = $this->queueRepository->findOldestEntryForProcess($this);
217 2
        return $entry['exec_time'] ?? 0;
218
    }
219
220
    /**
221
     * @deprecated
222
     */
223 2
    public function getTimeForFirstItem(): int
224
    {
225 2
        $entry = $this->queueRepository->findYoungestEntryForProcess($this);
226 2
        return $entry['exec_time'] ?? 0;
227
    }
228
229
    /**
230
     * Counts the number of items which need to be processed
231
     *
232
     * @return int
233
     * @codeCoverageIgnore
234
     */
235
    public function getAmountOfItemsProcessed()
236
    {
237
        return $this->queueRepository->countExecutedItemsByProcess($this);
238
    }
239
240
    /**
241
     * Counts the number of items which still need to be processed
242
     *
243
     * @return int
244
     * @codeCoverageIgnore
245
     */
246
    public function getItemsToProcess()
247
    {
248
        return $this->queueRepository->countNonExecutedItemsByProcess($this);
249
    }
250
251
    public function getFinallyAssigned(): int
252
    {
253
        return $this->getItemsToProcess() + $this->getAmountOfItemsProcessed();
254
    }
255
256
    /**
257
     * Returns the Progress of a crawling process as a percentage value
258
     *
259
     * @return float
260
     */
261 5
    public function getProgress()
262
    {
263 5
        $all = $this->getAssignedItemsCount();
264 5
        if ($all <= 0) {
265 2
            return 0;
266
        }
267
268 3
        $res = round((100 / $all) * $this->getAmountOfItemsProcessed());
269
270 3
        if ($res > 100.0) {
271 1
            return 100.0;
272
        }
273 2
        return $res;
274
    }
275
276
    /**
277
     * Return the processes current state
278
     *
279
     * @return string
280
     */
281 4
    public function getState()
282
    {
283 4
        if ($this->isActive() && $this->getProgress() < 100) {
284 1
            $stage = self::STATE_RUNNING;
285 3
        } elseif (! $this->isActive() && $this->getProgress() < 100) {
286 1
            $stage = self::STATE_CANCELLED;
287
        } else {
288 2
            $stage = self::STATE_COMPLETED;
289
        }
290 4
        return $stage;
291
    }
292
}
293