TaskManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 16
loc 136
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A areThereOpenTasksLeft() 0 4 1
A addOpenTask() 0 7 1
A getOpenTask() 0 10 2
A markRunningTaskAsAborted() 8 8 1
A markRunningTaskAsFinished() 8 8 1
A markOpenTaskAsRunning() 0 7 1
A getValidRunningTaskKey() 0 12 2
A getArrayIndexKey() 0 4 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
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-07-23 
5
 */
6
7
namespace Net\Bazzline\Component\ProcessForkManager;
8
9
/**
10
 * Class TaskManager
11
 * @package Net\Bazzline\Component\ProcessForkManager
12
 */
13
class TaskManager
14
{
15
    /**
16
     * @var array|AbstractTask[]
17
     */
18
    private $abortedTasks;
19
20
    /**
21
     * @var bool
22
     */
23
    private $areThereOpenTasksLeft;
24
25
    /**
26
     * @var array|AbstractTask[]
27
     */
28
    private $finishedTasks;
29
30
    /**
31
     * @var array|AbstractTask[]
32
     */
33
    private $openTasks;
34
35
    /**
36
     * @var array|AbstractTask[]
37
     */
38
    private $runningTasks;
39
40
    public function __construct()
41
    {
42
        $this->abortedTasks = array();
43
        $this->areThereOpenTasksLeft = false;
44
        $this->finishedTasks = array();
45
        $this->openTasks = array();
46
        $this->runningTasks = array();
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    public function areThereOpenTasksLeft()
53
    {
54
        return $this->areThereOpenTasksLeft;
55
    }
56
57
    /**
58
     * @param AbstractTask $task
59
     * @return $this
60
     */
61
    public function addOpenTask(AbstractTask $task)
62
    {
63
        $this->openTasks[] = $task;
64
        $this->areThereOpenTasksLeft = true;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return null|AbstractTask
71
     */
72
    public function getOpenTask()
73
    {
74
        $task = array_shift($this->openTasks);
75
76
        if (empty($this->openTasks)) {
77
            $this->areThereOpenTasksLeft = false;
78
        }
79
80
        return $task;
81
    }
82
83
    /**
84
     * @param AbstractTask $task
85
     * @throws RuntimeException
86
     */
87 View Code Duplication
    public function markRunningTaskAsAborted(AbstractTask $task)
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...
88
    {
89
        $key = $this->getValidRunningTaskKey($task);
90
91
        $this->abortedTasks[$key] = $this->runningTasks[$key];
92
        $this->abortedTasks[$key]->markAsAborted();
93
        unset($this->runningTasks[$key]);
94
    }
95
96
    /**
97
     * @param AbstractTask $task
98
     * @throws RuntimeException
99
     */
100 View Code Duplication
    public function markRunningTaskAsFinished(AbstractTask $task)
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...
101
    {
102
        $key = $this->getValidRunningTaskKey($task);
103
104
        $this->finishedTasks[$key] = $this->runningTasks[$key];
105
        $this->finishedTasks[$key]->markAsFinished();
106
        unset($this->runningTasks[$key]);
107
    }
108
109
    /**
110
     * @param AbstractTask $task
111
     * @throws InvalidArgumentException
112
     * @todo implement validation if task is not in open|finished|aborted|running array
113
     */
114
    public function markOpenTaskAsRunning(AbstractTask $task)
115
    {
116
        $key = $this->getArrayIndexKey($task);
117
        $task->markAsRunning();
118
119
        $this->runningTasks[$key] = $task;
120
    }
121
122
    /**
123
     * @param AbstractTask $task
124
     * @return string
125
     * @throws RuntimeException
126
     */
127
    private function getValidRunningTaskKey(AbstractTask $task)
128
    {
129
        $key = $this->getArrayIndexKey($task);
130
131
        if (!isset($this->runningTasks[$key])) {
132
            throw new RuntimeException(
133
                'provided task is not running'
134
            );
135
        }
136
137
        return $key;
138
    }
139
140
    /**
141
     * @param AbstractTask $task
142
     * @return string
143
     */
144
    private function getArrayIndexKey(AbstractTask $task)
145
    {
146
        return spl_object_hash($task);
147
    }
148
}