1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of GitterBot package. |
5
|
|
|
* |
6
|
|
|
* @author Serafim <[email protected]> |
7
|
|
|
* @date 24.09.2015 15:27 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\Console\Commands; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use App\Room; |
17
|
|
|
use App\Gitter\Client; |
18
|
|
|
use Carbon\Carbon; |
19
|
|
|
use Illuminate\Console\Command; |
20
|
|
|
use Illuminate\Contracts\Config\Repository; |
21
|
|
|
use Illuminate\Contracts\Container\Container; |
22
|
|
|
use Illuminate\Support\Facades\Log; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class StartGitterBot |
27
|
|
|
* @package App\Console\Commands |
28
|
|
|
*/ |
29
|
|
|
class StartGitterBot extends Command |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The name and signature of the console command. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $signature = 'gitter:listen {room}'; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The console command description. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $description = 'Start gitter bot thread for target room.'; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Container |
49
|
|
|
*/ |
50
|
|
|
protected $container; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $pid; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Execute the console command. |
60
|
|
|
* |
61
|
|
|
* @param Repository $config |
62
|
|
|
* @param Container $container |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
* @throws \InvalidArgumentException |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
* @throws \LogicException |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
|
public function handle(Repository $config, Container $container) |
71
|
|
|
{ |
72
|
|
|
$client = Client::make($config->get('gitter.token'), $this->argument('room')); |
73
|
|
|
$stream = $container->make(Room::class)->listen(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->info(sprintf('KarmaBot %s started at %s', Client::VERSION, Carbon::now())); |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
$this->makePidFile(); |
79
|
|
|
$client->run(); |
80
|
|
|
$this->removePidFile(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create pid file |
85
|
|
|
*/ |
86
|
|
|
protected function makePidFile() |
87
|
|
|
{ |
88
|
|
|
$this->pid = storage_path('pids/' . date('Y_m_d_tis_') . microtime(1) . '.pid'); |
89
|
|
|
file_put_contents($this->pid, getmypid()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Delete pid file |
94
|
|
|
*/ |
95
|
|
|
protected function removePidFile() |
96
|
|
|
{ |
97
|
|
|
if (is_file($this->pid)) { |
98
|
|
|
unlink($this->pid); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.