Completed
Push — 2.0 ( ee0168...76e968 )
by Marco
11:26
created

Locker   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 156
Duplicated Lines 58.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 91
loc 156
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A isQueued() 13 13 1
A isStarting() 19 19 1
A isCompleted() 23 23 2
A isAborted() 23 23 1
A queued() 0 5 1
A running() 0 5 1
A completed() 0 5 1
A free() 0 9 1
A release() 0 7 2
A dump() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Psr\Log\LoggerInterface;
5
6
/**
7
 * @package     Comodojo Extender
8
 * @author      Marco Giovinazzi <[email protected]>
9
 * @license     MIT
10
 *
11
 * LICENSE:
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
 * THE SOFTWARE.
20
 */
21
22
class Locker {
23
24
    use ConfigurationTrait;
25
    use LoggerTrait;
26
27
    private $running_jobs = [];
28
29
    private $completed_jobs = [];
30
31
    private $queued_jobs = [];
32
33
    private $lock_file = 'extender.tasks.locker';
0 ignored issues
show
Unused Code introduced by
The property $lock_file is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
36
37
        $this->setConfiguration($configuration);
38
        $this->setLogger($logger);
39
40
        $lock_file = $configuration->get('queue-file');
41
        if ( $lock_file !== null ) $this->$lock_file = $lock_file;
42
43
    }
44
45 View Code Duplication
    public function isQueued(Job $job) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
47
        $this->logger->debug('Adding job '.$job->name.' (uid '.$job->uid.') to queue');
48
49
        $uid = $job->uid;
50
51
        $this->queued_jobs[$uid] = $job;
52
53
        $this->dump();
54
55
        return true;
56
57
    }
58
59 View Code Duplication
    public function isStarting($uid, $pid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
61
        $job = $this->queued_jobs[$uid];
62
63
        $this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') is starting with pid '.$pid);
64
65
        $job->pid = $pid;
66
67
        $job->start_timestamp = microtime(true);
68
69
        $this->running_jobs[$uid] = $job;
70
71
        unset($this->queued_jobs[$uid]);
72
73
        $this->dump();
74
75
        return $this;
76
77
    }
78
79 View Code Duplication
    public function isCompleted($uid, $success, $result, $wid = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
81
        $job = $this->running_jobs[$uid];
82
83
        $this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') completed with '.($success ? 'success' : 'error'));
84
85
        $job->success = $success;
86
87
        $job->result = $result;
88
89
        $job->wid = $wid;
90
91
        $job->end_timestamp = microtime(true);
92
93
        $this->completed_jobs[$uid] = $job;
94
95
        unset($this->running_jobs[$uid]);
96
97
        $this->dump();
98
99
        return $this;
100
101
    }
102
103 View Code Duplication
    public function isAborted($uid, $error) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
105
        $job = $this->running_jobs[$uid];
106
107
        $this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') aborted, reason: '.$error);
108
109
        $job->success = false;
110
111
        $job->result = $error;
112
113
        $job->wid = null;
114
115
        $job->end_timestamp = microtime(true);
116
117
        $this->completed_jobs[$uid] = $job;
118
119
        unset($this->queued_jobs[$uid]);
120
121
        $this->dump();
122
123
        return $this;
124
125
    }
126
127
    public function queued() {
128
129
        return $this->queued_jobs;
130
131
    }
132
133
    public function running() {
134
135
        return $this->running_jobs;
136
137
    }
138
139
    public function completed() {
140
141
        return $this->completed_jobs;
142
143
    }
144
145
    public function free() {
146
147
        $this->queued_jobs = array();
148
        $this->running_jobs = array();
149
        $this->completed_jobs = array();
150
151
        $this->dump();
152
153
    }
154
155
    public function release() {
156
157
        $lock = file_exists($this->queue_file) ? unlink($this->queue_file) : true;
158
159
        return $lock;
160
161
    }
162
163 View Code Duplication
    private function dump() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
165
        $data = array(
166
            'QUEUED' => count($this->queued_jobs),
167
            'RUNNING' => count($this->running_jobs),
168
            'COMPLETED' => count($this->completed_jobs)
169
        );
170
171
        $content = serialize($data);
172
173
        return file_put_contents($this->queue_file, $content);
174
175
    }
176
177
}
178