StartGitterPool   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 89
rs 10
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 4
A start() 0 7 2
A stop() 0 13 2
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 11.10.2015 22:50
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Interfaces\Console\Commands;
12
13
14
use Illuminate\Console\Command;
15
use Illuminate\Contracts\Config\Repository;
16
use Illuminate\Contracts\Container\Container;
17
use Symfony\Component\Finder\Finder;
18
19
/**
20
 * Class StartGitterPool
21
 */
22
class StartGitterPool extends Command
23
{
24
    /**
25
     * The name and signature of the console command.
26
     *
27
     * @var string
28
     */
29
    protected $signature = 'gitter:pool {action=start}';
30
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Start gitter chat pool.';
38
39
40
    /**
41
     * @var Container
42
     */
43
    protected $container;
44
45
    /**
46
     * @var Repository
47
     */
48
    protected $config;
49
50
    /**
51
     * Execute the console command.
52
     *
53
     * @param Repository $config
54
     * @param Container $container
55
     *
56
     * @return mixed
57
     * @throws \InvalidArgumentException
58
     * @throws \RuntimeException
59
     * @throws \LogicException
60
     * @throws \Exception
61
     */
62
    public function handle(Repository $config, Container $container)
63
    {
64
        $this->container = $container;
65
        $this->config = $config;
66
67
68
        $action = $this->argument('action');
69
        switch ($action) {
70
            case 'start':
71
            case 'restart':
72
                $this->stop();
73
                $this->start();
74
                break;
75
            case 'stop':
76
                $this->stop();
77
                break;
78
            default:
79
                throw new \InvalidArgumentException('Action ' . $action . ' not found');
80
        }
81
    }
82
83
    /**
84
     * Start processes
85
     */
86
    protected function start()
87
    {
88
        foreach ($this->container['room.manager']->all() as $room) {
89
            shell_exec('nohup php artisan gitter:listen '.$room->id().' > /dev/null 2>&1 &');
90
            $this->line('Starting ' . $room->id() . ' listener.');
91
        }
92
    }
93
94
    /**
95
     * Stop all processes
96
     */
97
    protected function stop()
98
    {
99
        $finder = (new Finder())
100
            ->files()
101
            ->name('*.pid')
102
            ->in(storage_path('pids'));
103
104
        foreach ($finder as $file) {
105
            $pid = file_get_contents($file->getRealpath());
106
            shell_exec('kill ' . $pid);
107
            unlink($file->getRealpath());
108
        }
109
    }
110
}
111