Completed
Push — testing/model-process ( c5606f )
by Tomas Norre
06:16
created

Process::getTimeForLastItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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\Object\ObjectManager;
31
32
/**
33
 * Class Process
34
 *
35
 * @package AOE\Crawler\Domain\Model
36
 */
37
class Process
38
{
39
    const STATE_RUNNING = 'running';
40
    const STATE_CANCELLED = 'cancelled';
41
    const STATE_COMPLETED = 'completed';
42
43
    /**
44
     * @var array
45
     */
46
    protected $row;
47
48
    /**
49
     * @var QueueRepository
50
     */
51
    protected $queueRepository;
52
53
    /**
54
     * @var ObjectManager
55
     */
56
    protected $objectManager;
57
58
    /**
59
     * @param array $row
60
     */
61 38
    public function __construct($row = [])
62
    {
63 38
        $this->row = $row;
64 38
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
65 38
        $this->queueRepository = $this->objectManager->get(QueueRepository::class);
66 16
    }
67
68
    /**
69
     * Returns the activity state for this process
70
     *
71
     * @param void
72
     * @return boolean
73
     */
74
    public function getActive()
75
    {
76
        return $this->row['active'];
77
    }
78
79
    /**
80
     * Returns the identifier for the process
81
     *
82
     * @return string
83
     */
84 14
    public function getProcess_id()
85
    {
86 14
        return $this->row['process_id'];
87
    }
88
89
    /**
90
     * Returns the timestamp of the exectime for the first relevant queue item.
91
     * This can be used to determine the runtime
92
     *
93
     * @return int
94
     */
95 1
    public function getTimeForFirstItem()
96
    {
97 1
        $entry = $this->queueRepository->findYoungestEntryForProcess($this);
98 1
        return $entry->getExecutionTime();
99
    }
100
101
    /**
102
     * Returns the timestamp of the exectime for the last relevant queue item.
103
     * This can be used to determine the runtime
104
     *
105
     * @return int
106
     */
107 1
    public function getTimeForLastItem()
108
    {
109 1
        $entry = $this->queueRepository->findOldestEntryForProcess($this);
110 1
        return $entry->getExecutionTime();
111
    }
112
113
    /**
114
     * Returns the difference between first and last processed item
115
     *
116
     * @return int
117
     */
118
    public function getRuntime()
119
    {
120
        return $this->getTimeForLastItem() - $this->getTimeForFirstItem();
121
    }
122
123
    /**
124
     * Returns the ttl of the process
125
     *
126
     * @return int
127
     */
128
    public function getTTL()
129
    {
130
        return $this->row['ttl'];
131
    }
132
133
    /**
134
     * Counts the number of items which need to be processed
135
     *
136
     * @return int
137
     */
138
    public function countItemsProcessed()
139
    {
140
        return $this->queueRepository->countExecutedItemsByProcess($this);
141
    }
142
143
    /**
144
     * Counts the number of items which still need to be processed
145
     *
146
     * @return int
147
     */
148
    public function countItemsToProcess()
149
    {
150
        return $this->queueRepository->countNonExecutedItemsByProcess($this);
151
    }
152
153
    /**
154
     * Returns the Progress of a crawling process as a percentage value
155
     *
156
     * @return float
157
     */
158
    public function getProgress()
159
    {
160
        $all = $this->countItemsAssigned();
161
        if ($all <= 0) {
162
            return 0;
163
        }
164
165
        $res = round((100 / $all) * $this->countItemsProcessed());
166
167
        if ($res > 100.0) {
168
            return 100.0;
169
        }
170
        return $res;
171
    }
172
173
    /**
174
     * Returns the number of assigned entries
175
     *
176
     * @return int
177
     */
178
    public function countItemsAssigned()
179
    {
180
        return $this->row['assigned_items_count'];
181
    }
182
183
    /**
184
     * Return the processes current state
185
     *
186
     * @return string
187
     */
188
    public function getState()
189
    {
190
        if ($this->getActive() && $this->getProgress() < 100) {
191
            $stage = self::STATE_RUNNING;
192
        } elseif (!$this->getActive() && $this->getProgress() < 100) {
193
            $stage = self::STATE_CANCELLED;
194
        } else {
195
            $stage = self::STATE_COMPLETED;
196
        }
197
        return $stage;
198
    }
199
200
    /**
201
     * Returns the properties of the object as array
202
     *
203
     * @return array
204
     */
205
    public function getRow()
206
    {
207
        return $this->row;
208
    }
209
}
210