Completed
Push — 2.0 ( 0d93f2...14c778 )
by Marco
16:42
created

Niceness::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 2
nc 2
nop 1
1
<?php namespace Comodojo\Extender\Components;
2
3
use \Psr\Log\LoggerInterface;
4
use \Exception;
5
6
/**
7
 * Lock file manager (static methods)
8
 *
9
 * @package     Comodojo extender
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program 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 Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class Niceness {
30
31
    private $logger;
32
33
    private $niceness = 0;
34
35
    public function __contruct(LoggerInterface $logger) {
36
37
        $this->logger = $logger;
38
39
    }
40
41
    public function get($pid = null) {
42
43
        return is_null($pid) ? $this->niceness : pcntl_getpriority($pid);
44
45
    }
46
47
    public function set($niceness = null, $pid = null) {
48
49
        $niceness = self::getNiceness($niceness);
50
51
        if ( is_null($pid) ) {
52
53
            $nice = proc_nice($niceness);
54
55
            if ( $nice === false ) {
56
57
                $this->logger->warning("Unable to set parent process niceness to $niceness");
58
59
            } else {
60
61
                $this->niceness = $niceness;
62
63
            }
64
65
        } else {
66
67
            $nice = pcntl_setpriority($pid, $$niceness);
68
69
            if ( $nice == false ) $logger->warning("Unable to set child process $pid niceness to $niceness");
0 ignored issues
show
Bug introduced by
The variable $logger does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
70
71
        }
72
73
        return $nice;
74
75
    }
76
77 View Code Duplication
    private static function getNiceness($niceness=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...
78
79
        return filter_var($niceness, FILTER_VALIDATE_INT, array(
80
            'options' => array(
81
                'default' => 0,
82
                'min_range' => -10,
83
                'max_range' => 10
84
            )
85
        ));
86
87
    }
88
89
}
90