Completed
Push — master ( bd5bad...380d98 )
by Marco
04:57
created

Process::__construct()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.4425

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 10
cts 13
cp 0.7692
rs 8.439
c 0
b 0
f 0
cc 6
eloc 12
nc 10
nop 3
crap 6.4425
1
<?php namespace Comodojo\Daemon;
2
3
use \Comodojo\Daemon\Events\PosixEvent;
4
use \Comodojo\Daemon\Utils\ProcessTools;
5
use \Comodojo\Daemon\Utils\Checks;
6
use \Comodojo\Daemon\Utils\PosixSignals;
7
use \Comodojo\Daemon\Traits\PidTrait;
8
use \Comodojo\Daemon\Traits\SignalsTrait;
9
use \Comodojo\Foundation\Events\Manager as EventsManager;
10
use \Comodojo\Foundation\Events\EventsTrait;
11
use \Comodojo\Foundation\Logging\Manager as LogManager;
12
use \Comodojo\Foundation\Logging\LoggerTrait;
13
use \Psr\Log\LoggerInterface;
14
use \RuntimeException;
15
use \Exception;
16
17
/**
18
 * @package     Comodojo Daemon
19
 * @author      Marco Giovinazzi <[email protected]>
20
 * @license     MIT
21
 *
22
 * LICENSE:
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
 * THE SOFTWARE.
31
 */
32
33
abstract class Process {
34
35
    use PidTrait;
36
    use EventsTrait;
37
    use LoggerTrait;
38
    use SignalsTrait;
39
40
    /**
41
     * Build the process
42
     *
43
     * @param int $niceness
44
     * @param LoggerInterface $logger;
0 ignored issues
show
Documentation introduced by
There is no parameter named $logger;. Did you maybe mean $logger?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
45
     * @param EventsManager $events;
0 ignored issues
show
Documentation introduced by
There is no parameter named $events;. Did you maybe mean $events?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
46
     */
47 1
    public function __construct($niceness = null, LoggerInterface $logger = null, EventsManager $events = null){
48
49 1
        if ( !Checks::cli() ) {
50
            throw new RuntimeException("Process can run only in cli SAPI");
51
        }
52
53 1
        if ( !Checks::signals() ) {
54
            throw new Exception("Missing pcntl signaling");
55
        }
56
57 1
        $this->logger = is_null($logger) ? LogManager::create('daemon', false)->getLogger() : $logger;
58 1
        $this->events = is_null($events) ? EventsManager::create($this->logger) : $events;
59
60
        // get current PID
61 1
        $this->pid = ProcessTools::getPid();
62
63 1
        if ( ProcessTools::setNiceness($niceness) === false ) {
64
            $this->logger->warning("Unable to set process niceness to $niceness");
65
        }
66
67
        // register signals
68 1
        $this->signals = new PosixSignals();
69 1
        $this->signals->any()->call(array($this, 'signalToEvent'));
70
71 1
    }
72
73
    /**
74
     * The generic signal handler.
75
     *
76
     * It transforms a signal into framework catchable event.
77
     *
78
     * @param int $signal
79
     * @return self
80
     */
81
    public function signalToEvent($signal) {
82
83
        if ( $this->pid == ProcessTools::getPid() ) {
84
85
            $signame = $this->signals->signame($signal);
86
87
            $this->logger->debug("Received $signame ($signal) signal, firing associated event(s)");
88
89
            $this->events->emit( new PosixEvent($signal, $this) );
90
            $this->events->emit( new PosixEvent($signame, $this) );
91
92
        }
93
94
        return $this;
95
96
    }
97
98
    /**
99
     * Stop current process execution.
100
     *
101
     * @param integer $return_code
102
     */
103
    public function end($return_code) {
104
105
        exit($return_code);
0 ignored issues
show
Coding Style Compatibility introduced by
The method end() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
106
107
    }
108
109
}
110