Completed
Push — 2.0 ( dbbe01...d43752 )
by Marco
11:55
created

Tracker::setAborted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Foundation\Base\ConfigurationTrait;
4
use \Comodojo\Foundation\Base\Configuration;
5
use \Comodojo\Foundation\Logging\LoggerTrait;
6
use \Psr\Log\LoggerInterface;
7
8
/**
9
 * @package     Comodojo Extender
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class Tracker {
25
26
    use ConfigurationTrait;
27
    use LoggerTrait;
28
29
    /**
30
     * @var array
31
     */
32
    private $queued = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $running = [];
38
39
    /**
40
     * @var array
41
     */
42
    private $completed = [];
43
44
    /**
45
     * Tracker constructor
46
     *
47
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     * @param Configuration $configuration
49
     * @param LoggerInterface $logger
50
     */
51
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
52
53
        $this->setConfiguration($configuration);
54
        $this->setLogger($logger);
55
56
    }
57
58
    public function getQueued() {
59
60
        return $this->queued;
61
62
    }
63
64
    public function setQueued(Request $request) {
65
66
        $uid = $request->getUid();
67
68
        $this->logger->debug("Adding task with uid $uid to queue");
69
70
        $this->queued[$uid] = $request;
71
72
        return $this;
73
74
    }
75
76
    public function countQueued() {
77
78
        return count($this->queued);
79
80
    }
81
82
    public function getRunning() {
83
84
        return $this->running;
85
86
    }
87
88
    public function setRunning($uid, $pid) {
89
90
        $request = $this->queued[$uid];
91
92
        $this->logger->debug("Task ".$request->getName()." (uid $uid) is starting with pid $pid");
93
94
        $request->setPid($pid);
95
        $request->setStartTimestamp(microtime(true));
96
97
        $this->running[$uid] = $request;
98
99
        unset($this->queued[$uid]);
100
101
        return $this;
102
103
    }
104
105
    public function countRunning() {
106
107
        return count($this->running);
108
109
    }
110
111
    public function getCompleted() {
112
113
        return $this->completed;
114
115
    }
116
117
    public function countCompleted() {
118
119
        return count($this->completed);
120
121
    }
122
123 View Code Duplication
    public function setCompleted($uid, Result $result) {
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...
124
125
        $request = $this->running[$uid];
126
127
        $this->logger->debug("Task ".$request->getName()." (uid $uid) completed with ".($result->success ? 'success' : 'error'));
0 ignored issues
show
Documentation introduced by
The property success does not exist on object<Comodojo\Extender\Task\Result>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
129
        $this->completed[$uid] = $result;
130
131
        unset($this->running[$uid]);
132
133
        return $this;
134
135
    }
136
137 View Code Duplication
    public function setAborted($uid, Result $result) {
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...
138
139
        $request = $this->queued[$uid];
140
141
        $this->logger->debug("Task ".$request->getName()." (uid $uid) aborted: ".$result->message);
0 ignored issues
show
Documentation introduced by
The property message does not exist on object<Comodojo\Extender\Task\Result>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
143
        $this->completed[$uid] = $result;
144
145
        unset($this->queued[$uid]);
146
147
        return $this;
148
149
    }
150
151
    public function getSucceeded() {
152
153
        return array_filter($this->completed, function($result) {
154
            return $result->success;
155
        });
156
157
    }
158
159
    public function countSucceeded() {
160
161
        return count($this->getSucceeded());
162
163
    }
164
165
    public function getFailed() {
166
167
        return array_filter($this->completed, function($result) {
168
            return !$result->success;
169
        });
170
171
    }
172
173
    public function countFailed() {
174
175
        return count($this->getFailed());
176
177
    }
178
179
    public static function create(Configuration $configuration, LoggerInterface $logger) {
180
181
        return new Tracker($configuration, $logger);
182
183
    }
184
185
}
186