Completed
Push — test-coverage ( 951e01...59b971 )
by Tomas Norre
05:18
created

Process::getTimeForLastItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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