Issues (61)

src/Commands/DownWAMP.php (1 issue)

Labels
Severity
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 4
    public function fire()
43
    {
44 4
        $clientsOnly = $this->option('client-only');
45 4
        $serversOnly = $this->option('server-only');
46
47 4
        if (!$serversOnly) {
48 3
            $this->stopInstances(storage_path(self::CLIENT_PID_FILE));
0 ignored issues
show
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 4
        if (!$clientsOnly) {
52 3
            $this->stopInstances(storage_path(self::SERVER_PID_FILE));
53
        }
54 4
    }
55
56
    /**
57
     * Handle command alias
58
     *
59
     * @author Donii Sergii <[email protected]>
60
     */
61 4
    public function handle()
62
    {
63 4
        $this->fire();
64 4
    }
65
66
    /**
67
     * Stop all process by pids from file $file
68
     *
69
     * @param string $filename
70
     *
71
     * @author Donii Sergii <[email protected]>
72
     */
73 4
    private function stopInstances($filename)
74
    {
75 4
        if (file_exists($filename)) {
76 2
            $content = explode(PHP_EOL, file_get_contents($filename));
77
78 2
            if (count($content)) {
79 2
                foreach ($content as $pid) {
80 2
                    $pid = (int) trim($pid);
81
82 2
                    posix_kill($pid, 9);
83
                }
84
            }
85 2
            unlink($filename);
86
        }
87 4
    }
88
}
89