Completed
Push — master ( e41b48...e132fe )
by Sergii
05:32
created

DownWAMP::stopInstances()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 10.75

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 13
loc 13
ccs 2
cts 8
cp 0.25
crap 10.75
rs 9.2
c 0
b 0
f 0
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
 * @class  DownWAMP
15
 * Down WAMP servers & clients command.
16
 *
17
 * @author Donii Sergii <[email protected]>
18
 */
19
class DownWAMP extends Command
20
{
21
    const SERVER_PID_FILE = 'servers.pids';
22
    const CLIENT_PID_FILE = 'clients.pids';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $signature = 'wamp:stop {--server-only} {--client-only}';
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected $description = 'Stop all background running server & clients
33
                                {--server-only : Stop only running server instances}
34
                                {--client-only : Stop only running client instances}
35
    ';
36
37
    /**
38
     * Handle command
39
     *
40
     * @author Donii Sergii <[email protected]>
41
     */
42 2
    public function fire()
43
    {
44 2
        $clientsOnly = $this->option('client-only');
45 2
        $serversOnly = $this->option('server-only');
46
47 2
        if (!$serversOnly) {
48 1
            $this->stopInstances(storage_path(self::CLIENT_PID_FILE));
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

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