Completed
Push — typo3v9 ( 6762b6...ea38b1 )
by Tomas Norre
05:52
created

Process::getAssignedItemsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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