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

Process::getActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
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\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 20
    public function __construct($row = [])
62
    {
63 20
        $this->row = $row;
64 20
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
65 20
        $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 1
    public function getActive()
75
    {
76 1
        return $this->row['active'];
77
    }
78
79
    /**
80
     * Returns the identifier for the process
81
     *
82
     * @return string
83
     */
84 15
    public function getProcess_id()
85
    {
86 15
        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 5
    public function getRuntime()
119
    {
120 5
        return $this->getTimeForLastItem() - $this->getTimeForFirstItem();
121
    }
122
123
    /**
124
     * Returns the ttl of the process
125
     *
126
     * @return int
127
     */
128 1
    public function getTTL()
129
    {
130 1
        return $this->row['ttl'];
131
    }
132
133
    /**
134
     * Counts the number of items which need to be processed
135
     *
136
     * @return int
137
     * @codeCoverageIgnore
138
     */
139
    public function countItemsProcessed()
140
    {
141
        return $this->queueRepository->countExecutedItemsByProcess($this);
142
    }
143
144
    /**
145
     * Counts the number of items which still need to be processed
146
     *
147
     * @return int
148
     * @codeCoverageIgnore
149
     */
150
    public function countItemsToProcess()
151
    {
152
        return $this->queueRepository->countNonExecutedItemsByProcess($this);
153
    }
154
155
    /**
156
     * Returns the Progress of a crawling process as a percentage value
157
     *
158
     * @return float
159
     */
160 5
    public function getProgress()
161
    {
162 5
        $all = $this->countItemsAssigned();
163 5
        if ($all <= 0) {
164 2
            return 0;
165
        }
166
167 3
        $res = round((100 / $all) * $this->countItemsProcessed());
168
169 3
        if ($res > 100.0) {
170 1
            return 100.0;
171
        }
172 2
        return $res;
173
    }
174
175
    /**
176
     * Returns the number of assigned entries
177
     *
178
     * @return int
179
     */
180 1
    public function countItemsAssigned()
181
    {
182 1
        return $this->row['assigned_items_count'];
183
    }
184
185
    /**
186
     * Return the processes current state
187
     *
188
     * @return string
189
     */
190 4
    public function getState()
191
    {
192 4
        if ($this->getActive() && $this->getProgress() < 100) {
193 1
            $stage = self::STATE_RUNNING;
194 3
        } elseif (!$this->getActive() && $this->getProgress() < 100) {
195 1
            $stage = self::STATE_CANCELLED;
196
        } else {
197 2
            $stage = self::STATE_COMPLETED;
198
        }
199 4
        return $stage;
200
    }
201
202
    /**
203
     * Returns the properties of the object as array
204
     *
205
     * @return array
206
     */
207 1
    public function getRow()
208
    {
209 1
        return $this->row;
210
    }
211
212
    /**
213
     * @param array $row
214
     *
215
     * @return void
216
     */
217 19
    public function setRow(array $row)
218
    {
219 19
        $this->row = $row;
220 19
    }
221
}
222