Completed
Push — issue/355 ( 6f96f2...e2dfcd )
by Tomas Norre
40:06 queued 25:06
created

Process::getActive()   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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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 16
     */
57
    protected $active = false;
58 16
59 16
    /**
60 16
     * @var int
61 16
     */
62
    protected $ttl = 0;
63
64
    /**
65
     * @var int
66
     */
67
    protected $assignedItemsCount = 0;
68
69 1
    /**
70
     * @var bool
71 1
     */
72
    protected $deleted = false;
73
74
    /**
75
     * @var string
76
     */
77
    protected $systemProcessId = '';
78
79 16
    /**
80
     * @var QueueRepository
81 16
     */
82
    protected $queueRepository;
83
84
    public function __construct()
85
    {
86
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
87
        $this->queueRepository = $objectManager->get(QueueRepository::class);
88
    }
89
90 1
    /**
91
     * @return array
92 1
     *
93 1
     * @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 1
     *
103
     * @deprecated since crawler v6.2.2, will be removed in crawler v7.0.0.
104 1
     */
105 1
    public function setRow($row)
106
    {
107
        $this->row = $row;
108
    }
109
110
    /**
111
     * @return string
112
     */
113 5
    public function getProcessId()
114
    {
115 5
        return $this->processId;
116
    }
117
118
    /**
119
     * @param string $processId
120
     */
121
    public function setProcessId($processId)
122
    {
123 1
        $this->processId = $processId;
124
    }
125 1
126
    /**
127
     * @return bool
128
     */
129
    public function isActive()
130
    {
131
        return $this->active;
132
    }
133
134
    /**
135
     * @param bool $active
136
     */
137
    public function setActive($active)
138
    {
139
        $this->active = $active;
140
    }
141
142
    /**
143
     * @return int
144
     */
145
    public function getTtl()
146
    {
147
        return $this->ttl;
148
    }
149
150
    /**
151
     * @param int $ttl
152
     */
153
    public function setTtl($ttl)
154
    {
155 5
        $this->ttl = $ttl;
156
    }
157 5
158 5
    /**
159 2
     * @return int
160
     */
161
    public function getAssignedItemsCount()
162 3
    {
163
        return $this->assignedItemsCount;
164 3
    }
165 1
166
    /**
167 2
     * @param int $assignedItemsCount
168
     */
169
    public function setAssignedItemsCount($assignedItemsCount)
170
    {
171
        $this->assignedItemsCount = $assignedItemsCount;
172
    }
173
174
    /**
175 1
     * @return bool
176
     */
177 1
    public function isDeleted()
178
    {
179
        return $this->deleted;
180
    }
181
182
    /**
183
     * @param bool $deleted
184
     */
185 4
    public function setDeleted($deleted)
186
    {
187 4
        $this->deleted = $deleted;
188 1
    }
189 3
190 1
    /**
191
     * @return string
192 2
     */
193
    public function getSystemProcessId()
194 4
    {
195
        return $this->systemProcessId;
196
    }
197
198
    /**
199
     * @param string $systemProcessId
200
     */
201
    public function setSystemProcessId($systemProcessId)
202 1
    {
203
        $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 20
     * @codeCoverageIgnore
213
     * @deprecated since crawler v6.2.2, will be removed in crawler v7.0.0.
214 20
     */
215 20
    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
    public function getRuntime()
241
    {
242
        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
    public function getProgress()
273
    {
274
        $all = $this->getAssignedItemsCount();
275
        if ($all <= 0) {
276
            return 0;
277
        }
278
279
        $res = round((100 / $all) * $this->countItemsProcessed());
280
281
        if ($res > 100.0) {
282
            return 100.0;
283
        }
284
        return $res;
285
    }
286
287
    /**
288
     * Return the processes current state
289
     *
290
     * @return string
291
     */
292
    public function getState()
293
    {
294
        if ($this->isActive() && $this->getProgress() < 100) {
295
            $stage = self::STATE_RUNNING;
296
        } elseif (!$this->isActive() && $this->getProgress() < 100) {
297
            $stage = self::STATE_CANCELLED;
298
        } else {
299
            $stage = self::STATE_COMPLETED;
300
        }
301
        return $stage;
302
    }
303
}
304