Completed
Push — issue/336 ( 6fb6b4...518511 )
by Tomas Norre
05:49
created

Process::getProcessId()   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) 2017 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 16
    public function __construct()
85
    {
86 16
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
87 16
        $this->queueRepository = $objectManager->get(QueueRepository::class);
88 16
    }
89
90
    /**
91
     * @return array
92
     *
93
     * @deprecated since crawler v6.2.2, will be removed in crawler v7.0.0.
94
     */
95
    public function getRow()
96
    {
97
        return $this->row;
98
    }
99
100
    /**
101
     * @param array $row
102
     *
103
     * @deprecated since crawler v6.2.2, will be removed in crawler v7.0.0.
104
     */
105
    public function setRow($row)
106
    {
107
        $this->row = $row;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 16
    public function getProcessId()
114
    {
115 16
        return $this->processId;
116
    }
117
118
    /**
119
     * @param string $processId
120
     */
121 30
    public function setProcessId($processId)
122
    {
123 30
        $this->processId = $processId;
124 30
    }
125
126
    /**
127
     * @return bool
128
     */
129 1
    public function isActive()
130
    {
131 1
        return $this->active;
132
    }
133
134
    /**
135
     * @param bool $active
136
     */
137 15
    public function setActive($active)
138
    {
139 15
        $this->active = $active;
140 15
    }
141
142
    /**
143
     * @return int
144
     */
145 1
    public function getTtl()
146
    {
147 1
        return $this->ttl;
148
    }
149
150
    /**
151
     * @param int $ttl
152
     */
153 15
    public function setTtl($ttl)
154
    {
155 15
        $this->ttl = $ttl;
156 15
    }
157
158
    /**
159
     * @return int
160
     */
161 1
    public function getAssignedItemsCount()
162
    {
163 1
        return $this->assignedItemsCount;
164
    }
165
166
    /**
167
     * @param int $assignedItemsCount
168
     */
169 15
    public function setAssignedItemsCount($assignedItemsCount)
170
    {
171 15
        $this->assignedItemsCount = $assignedItemsCount;
172 15
    }
173
174
    /**
175
     * @return bool
176
     */
177 1
    public function isDeleted()
178
    {
179 1
        return $this->deleted;
180
    }
181
182
    /**
183
     * @param bool $deleted
184
     */
185 1
    public function setDeleted($deleted)
186
    {
187 1
        $this->deleted = $deleted;
188 1
    }
189
190
    /**
191
     * @return string
192
     */
193 1
    public function getSystemProcessId()
194
    {
195 1
        return $this->systemProcessId;
196
    }
197
198
    /**
199
     * @param string $systemProcessId
200
     */
201 1
    public function setSystemProcessId($systemProcessId)
202
    {
203 1
        $this->systemProcessId = $systemProcessId;
204 1
    }
205
206
    /**
207
     * Returns the timestamp of the exectime for the first relevant queue item.
208
     * This can be used to determine the runtime
209
     *
210
     * @return int
211
     *
212
     * @codeCoverageIgnore
213
     * @deprecated since crawler v6.2.2, will be removed in crawler v7.0.0.
214
     */
215
    public function getTimeForFirstItem()
216
    {
217
        $entry = $this->queueRepository->findYoungestEntryForProcess($this);
218
        return $entry->getExecTime();
219
    }
220
221
    /**
222
     * Returns the timestamp of the exectime for the last relevant queue item.
223
     * This can be used to determine the runtime
224
     *
225
     * @return int
226
     * @codeCoverageIgnore
227
     * @deprecated since crawler v6.2.2, will be removed in crawler v7.0.0.
228
     */
229
    public function getTimeForLastItem()
230
    {
231
        $entry = $this->queueRepository->findOldestEntryForProcess($this);
232
        return $entry->getExecTime();
233
    }
234
235
    /**
236
     * Returns the difference between first and last processed item
237
     *
238
     * @return int
239
     */
240 5
    public function getRuntime()
241
    {
242 5
        return $this->getTimeForLastItem() - $this->getTimeForFirstItem();
0 ignored issues
show
Deprecated Code introduced by
The method AOE\Crawler\Domain\Model...s::getTimeForLastItem() has been deprecated with message: since crawler v6.2.2, will be removed in crawler v7.0.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method AOE\Crawler\Domain\Model...::getTimeForFirstItem() has been deprecated with message: since crawler v6.2.2, will be removed in crawler v7.0.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
243
    }
244
245
    /**
246
     * Counts the number of items which need to be processed
247
     *
248
     * @return int
249
     * @codeCoverageIgnore
250
     */
251
    public function countItemsProcessed()
252
    {
253
        return $this->queueRepository->countExecutedItemsByProcess($this);
254
    }
255
256
    /**
257
     * Counts the number of items which still need to be processed
258
     *
259
     * @return int
260
     * @codeCoverageIgnore
261
     */
262
    public function countItemsToProcess()
263
    {
264
        return $this->queueRepository->countNonExecutedItemsByProcess($this);
265
    }
266
267
    /**
268
     * Returns the Progress of a crawling process as a percentage value
269
     *
270
     * @return float
271
     */
272 5
    public function getProgress()
273
    {
274 5
        $all = $this->getAssignedItemsCount();
275 5
        if ($all <= 0) {
276 2
            return 0;
277
        }
278
279 3
        $res = round((100 / $all) * $this->countItemsProcessed());
280
281 3
        if ($res > 100.0) {
282 1
            return 100.0;
283
        }
284 2
        return $res;
285
    }
286
287
    /**
288
     * Return the processes current state
289
     *
290
     * @return string
291
     */
292 4
    public function getState()
293
    {
294 4
        if ($this->isActive() && $this->getProgress() < 100) {
295 1
            $stage = self::STATE_RUNNING;
296 3
        } elseif (!$this->isActive() && $this->getProgress() < 100) {
297 1
            $stage = self::STATE_CANCELLED;
298
        } else {
299 2
            $stage = self::STATE_COMPLETED;
300
        }
301 4
        return $stage;
302
    }
303
}
304