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 Interfaces\Console\Commands; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use Domains\Room; |
17
|
|
|
use Interfaces\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
|
|
|
*/ |
28
|
|
|
class StartGitterBot extends Command |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The name and signature of the console command. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $signature = 'gitter:listen {room}'; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The console command description. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $description = 'Start gitter bot thread for target room.'; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Container |
48
|
|
|
*/ |
49
|
|
|
protected $container; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $pid; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Execute the console command. |
59
|
|
|
* |
60
|
|
|
* @param Repository $config |
61
|
|
|
* @param Container $container |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
* @throws \RuntimeException |
66
|
|
|
* @throws \LogicException |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function handle(Repository $config, Container $container) |
70
|
|
|
{ |
71
|
|
|
$client = Client::make($config->get('gitter.token'), $this->argument('room')); |
72
|
|
|
$stream = $container->make(Room::class)->listen(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->info(sprintf('KarmaBot %s started at %s', Client::VERSION, Carbon::now())); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
$this->makePidFile(); |
78
|
|
|
$client->run(); |
79
|
|
|
$this->removePidFile(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create pid file |
84
|
|
|
*/ |
85
|
|
|
protected function makePidFile() |
86
|
|
|
{ |
87
|
|
|
$this->pid = storage_path('pids/' . date('Y_m_d_tis_') . microtime(1) . '.pid'); |
88
|
|
|
file_put_contents($this->pid, getmypid()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Delete pid file |
93
|
|
|
*/ |
94
|
|
|
protected function removePidFile() |
95
|
|
|
{ |
96
|
|
|
if (is_file($this->pid)) { |
97
|
|
|
unlink($this->pid); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.