Test Failed
Push — 6-0 ( cfb4d5 )
by Tomas Norre
03:23
created

tx_crawler_domain_process   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 5.41%

Importance

Changes 0
Metric Value
dl 0
loc 151
rs 10
c 0
b 0
f 0
ccs 2
cts 37
cp 0.0541
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getRuntime() 0 3 1
B getState() 0 10 5
A getTimeForFirstItem() 0 6 1
A getProgress() 0 13 3
A countItemsAssigned() 0 3 1
A countItemsProcessed() 0 4 1
A getProcess_id() 0 3 1
A getActive() 0 3 1
A getTimeForLastItem() 0 6 1
A countItemsToProcess() 0 4 1
A getTTL() 0 3 1
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2009 AOE media ([email protected])
6
 *  All rights reserved
7
 *
8
 *  This script is part of the TYPO3 project. The TYPO3 project is
9
 *  free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  (at your option) any later version.
13
 *
14
 *  The GNU General Public License can be found at
15
 *  http://www.gnu.org/copyleft/gpl.html.
16
 *
17
 *  This script is distributed in the hope that it will be useful,
18
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *  GNU General Public License for more details.
21
 *
22
 *  This copyright notice MUST APPEAR in all copies of the script!
23
 ***************************************************************/
24
25
class tx_crawler_domain_process extends tx_crawler_domain_lib_abstract_dbobject
26
{
27
    const STATE_RUNNING = 'running';
28
    const STATE_CANCELLED = 'cancelled';
29
    const STATE_COMPLETED = 'completed';
30
31
    /**
32
     * @var string table name
33
     */
34
    protected static $tableName = 'tx_crawler_process';
35
36
    /**
37
     * Returns the activity state for this process
38
     *
39
     * @param void
40
     * @return boolean
41
     */
42
    public function getActive()
43
    {
44
        return $this->row['active'];
45
    }
46
47
    /**
48
     * Returns the identifier for the process
49 1
     *
50
     * @return string
51 1
     */
52
    public function getProcess_id()
53
    {
54
        return $this->row['process_id'];
55
    }
56
57
    /**
58
     * Returns the timestamp of the exectime for the first relevant queue item.
59
     * This can be used to determine the runtime
60
     *
61
     * @return int
62
     */
63
    public function getTimeForFirstItem()
64
    {
65
        $queueRepository = new tx_crawler_domain_queue_repository();
66
        $entry = $queueRepository->findYoungestEntryForProcess($this);
67
68
        return $entry->getExecutionTime();
69
    }
70
71
    /**
72
     * Returns the timestamp of the exectime for the last relevant queue item.
73
     * This can be used to determine the runtime
74
     *
75
     * @return int
76
     */
77
    public function getTimeForLastItem()
78
    {
79
        $queueRepository = new tx_crawler_domain_queue_repository();
80
        $entry = $queueRepository->findOldestEntryForProcess($this);
81
82
        return $entry->getExecutionTime();
83
    }
84
85
    /**
86
     * Returns the difference between first and last processed item
87
     *
88
     * @return int
89
     */
90
    public function getRuntime()
91
    {
92
        return $this->getTimeForLastItem() - $this->getTimeForFirstItem();
93
    }
94
95
    /**
96
     * Returns the ttl of the process
97
     *
98
     * @return int
99
     */
100
    public function getTTL()
101
    {
102
        return $this->row['ttl'];
103
    }
104
105
    /**
106
     * Counts the number of items which need to be processed
107
     *
108
     * @param void
109
     * @return int
110
     */
111
    public function countItemsProcessed()
112
    {
113
        $queueRepository = new tx_crawler_domain_queue_repository();
114
        return $queueRepository->countExecutedItemsByProcess($this);
115
    }
116
117
    /**
118
     * Counts the number of items which still need to be processed
119
     *
120
     * @param void
121
     * @return int
122
     */
123
    public function countItemsToProcess()
124
    {
125
        $queueRepository = new tx_crawler_domain_queue_repository();
126
        return $queueRepository->countNonExecutedItemsByProcess($this);
127
    }
128
129
    /**
130
     * Returns the Progress of a crawling process as a percentage value
131
     *
132
     * @param void
133
     * @return float
134
     */
135
    public function getProgress()
136
    {
137
        $all = $this->countItemsAssigned();
138
        if ($all <= 0) {
139
            return 0;
140
        }
141
142
        $res = round((100 / $all) * $this->countItemsProcessed());
143
144
        if ($res > 100.0) {
145
            return 100.0;
146
        }
147
        return $res;
148
    }
149
150
    /**
151
     * Returns the number of assigned Entrys
152
     *
153
     * @return int
154
     */
155
    public function countItemsAssigned()
156
    {
157
        return $this->row['assigned_items_count'];
158
    }
159
160
    /**
161
     * Return the processes current state
162
     *
163
     * @param void
164
     * @return string 'running'|'cancelled'|'completed'
165
     */
166
    public function getState()
167
    {
168
        if ($this->getActive() && $this->getProgress() < 100) {
169
            $stage = tx_crawler_domain_process::STATE_RUNNING;
170
        } elseif (!$this->getActive() && $this->getProgress() < 100) {
171
            $stage = tx_crawler_domain_process::STATE_CANCELLED;
172
        } else {
173
            $stage = tx_crawler_domain_process::STATE_COMPLETED;
174
        }
175
        return $stage;
176
    }
177
}
178