Completed
Push — master ( 7235dd...b44091 )
by Sergii
05:14
created

DownWAMP   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 2 1
B stopInstances() 0 14 5
A fire() 0 10 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Donii Sergii <[email protected]>
5
 * Date: 11/7/17
6
 * Time: 3:39 PM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Illuminate\Console\Command;
12
13
14
/**
15
 * @class  DownWAMP
16
 * Down WAMP servers & clients command.
17
 *
18
 * @author Donii Sergii <[email protected]>
19
 */
20
class DownWAMP extends Command
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $signature = 'wamp:stop {--server-only} {--client-only}';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $description = 'Stop all background running server & clients
31
                                {--server-only : Stop only running server instances}
32
                                {--client-only : Stop only running client instances}
33
    ';
34
35
    /**
36
     * Handle command
37
     *
38
     * @author Donii Sergii <[email protected]>
39
     */
40 2
    public function fire() {
41 2
        $clientsOnly = $this->option('client-only');
42 2
        $serversOnly = $this->option('server-only');
43
44 2
        if (!$serversOnly) {
45 1
            $this->stopInstances(storage_path('clients.pids'));
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            $this->stopInstances(/** @scrutinizer ignore-call */ storage_path('clients.pids'));
Loading history...
46
        }
47
48 2
        if (!$clientsOnly) {
49 1
            $this->stopInstances(storage_path('servers.pids'));
50
        }
51 2
    }
52
53
    /**
54
     * Handle command alias
55
     *
56
     * @author Donii Sergii <[email protected]>
57
     */
58 2
    public function handle() {
59 2
        $this->fire();
60 2
    }
61
62 2
    private function stopInstances($file) {
63 2
        if (file_exists($file)) {
64 2
            $content = explode(PHP_EOL, file_get_contents($file));
65
66 2
            if (count($content)) {
67 2
                foreach ($content as $pid) {
68 2
                    $pid = (int) trim($pid);
69
70 2
                    if ($pid > 0) {
71 2
                        exec('kill ' . $pid);
72
                    }
73
                }
74
75 2
                exec('rm ' . $file);
76
            }
77
        }
78
79 2
    }
80
81
}