Process::open()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Nyx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2016 Luke Steadman
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 *
25
 * @package Nyx
26
 */
27
namespace Nyx;
28
29
class Process implements ProcessInterface, OutputableInterface
30
{
31
    const PROCESS_STOPPED = 0;
32
    const PROCESS_RUNNING = 1;
33
    const PROCESS_SIGNALED = 2;
34
35
    /**
36
     * @var resource
37
     */
38
    protected $process;
39
40
    /**
41
     * @var CommandInterface
42
     */
43
    protected $command;
44
45
    /**
46
     * @var OutputInterface
47
     */
48
    protected $output;
49
50
    /**
51
     * Process constructor.
52
     *
53
     * @param CommandInterface $command
54
     */
55 12
    public function __construct(CommandInterface $command)
56
    {
57 12
        $this->command = $command;
58 12
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function setCommand(CommandInterface $command)
64
    {
65 1
        $this->command = $command;
66 1
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 9
    public function getCommand()
72
    {
73 9
        return $this->command;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 7
    public function open()
80
    {
81 7
        $command = $this->command->toString();
82 7
        $this->getOutput()->write("[+] New process with command: " . $command);
83
84
        // Command options
85 7
        $in  = array_values($this->command->getOption('in', array('pipe', 'r')));
86 7
        $out = array_values($this->command->getOption('out', array('file', '/tmp/nyx.log', 'a')));
87 7
        $err = array_values($this->command->getOption('err', array('file', '/tmp/nyx-error.log', 'a')));
88
89 7
        $this->process = proc_open(
90 7
            $command,
91
            array(
92 7
            0 => $in,
93 7
            1 => $out,
94
            2 => $err
95 7
            ),
96
            $pipes
97 7
        );
98 7
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 4
    public function close()
104
    {
105 4
        if (is_resource($this->process)) {
106 4
            proc_close($this->process);
107 4
        }
108 4
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 2
    public function isRunning()
114
    {
115 2
        if (is_resource($this->process)) {
116 2
            $status = proc_get_status($this->process);
117 2
            return array_key_exists('running', $status) && (bool)$status['running'];
118
        }
119
120 1
        return false;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function errors()
127
    {
128
        return array();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 2
    public function status()
135
    {
136 2
        if ($this->isRunning()) {
137 2
            return static::PROCESS_RUNNING;
138
        }
139
140 1
        if (is_resource($this->process)) {
141
            $status = proc_get_status($this->process);
142
143
            if ($status['stopped'] === true) {
144
                return static::PROCESS_STOPPED;
145
            }
146
        }
147
148 1
        return static::PROCESS_SIGNALED;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 11
    public function setOutput(OutputInterface $output)
155
    {
156 11
        $this->output = $output;
157 11
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 8
    public function getOutput()
163
    {
164 8
        if (is_null($this->output)) {
165 1
            $this->output = new Console();
166 1
        }
167
168 8
        return $this->output;
169
    }
170
}
171