Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

Room   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 158
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 11 2
A __construct() 0 8 1
A createSubscribersStorage() 0 13 2
A createMiddlewaresStorage() 0 13 2
A listen() 0 12 1
A logException() 0 8 1
A onMessage() 0 8 2
A onClose() 0 4 1
A onError() 0 4 1
A write() 0 12 2
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 09.10.2015 17:08
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 Domains;
12
13
use Interfaces\Gitter\Client;
14
use Interfaces\Gitter\Http\Stream;
15
use InvalidArgumentException;
16
use Interfaces\Gitter\Middleware\Storage as Middlewares;
17
use Interfaces\Gitter\Subscriber\Storage as Subscribers;
18
19
/**
20
 * Class Room
21
 *
22
 * @property int $id
23
 */
24
class Room
25
{
26
    /**
27
     * @param $roomId
28
     * @return mixed
29
     */
30
    public static function getId($roomId)
31
    {
32
        $rooms = \Config::get('gitter.rooms');
33
        $alias = mb_strtolower(trim($roomId));
34
35
        if (array_key_exists($alias, $rooms)) {
36
            return $rooms[$alias];
37
        }
38
39
        return $roomId;
40
    }
41
42
    /**
43
     * @var Client
44
     */
45
    protected $client;
46
47
    /**
48
     * @var string
49
     */
50
    public $id;
51
52
    /**
53
     * @var Middlewares
54
     */
55
    protected $middlewares;
56
57
    /**
58
     * Room constructor.
59
     * @param string $roomId
60
     */
61
    public function __construct($roomId)
62
    {
63
        $this->client = \App::make(Client::class);
64
        $this->id = static::getId($roomId);
65
        $this->middlewares = $this->createMiddlewaresStorage();
66
67
        $this->createSubscribersStorage();
68
    }
69
70
    /**
71
     * Create subscribers storage
72
     * @return Subscribers
73
     */
74
    protected function createSubscribersStorage()
75
    {
76
        $container = \App::make('app');
77
        $subscribers = \Config::get('gitter.subscribers');
78
79
80
        $storage = new Subscribers($container);
81
        foreach ($subscribers as $subscriber) {
82
            $storage->add($subscriber);
83
        }
84
85
        return $storage;
86
    }
87
88
    /**
89
     * Create middlewares storage
90
     * @return Middlewares
91
     */
92
    protected function createMiddlewaresStorage()
93
    {
94
        $container = \App::make('app');
95
        $middlewares = \Config::get('gitter.middlewares');
96
97
98
        $storage = new Middlewares($container);
99
        foreach ($middlewares as $middleware => $priority) {
100
            $storage->add($middleware, $priority);
101
        }
102
103
        return $storage;
104
    }
105
106
    /**
107
     * @throws InvalidArgumentException
108
     * @return Client
109
     */
110
    public function listen()
111
    {
112
        $client = $this->client
113
            ->stream('messages', ['roomId' => $this->id])
114
            ->on(Stream::EVENT_MESSAGE, function ($stream, $data) {
115
                $this->onMessage(Message::fromGitterObject($data));
116
            })
117
            ->on(Stream::EVENT_END, [$this, 'onClose'])
118
            ->on(Stream::EVENT_ERROR, [$this, 'onError']);
119
120
        return $client;
121
    }
122
123
    /**
124
     * @param \Exception $e
125
     */
126
    protected function logException(\Exception $e)
127
    {
128
        \Log::error(
129
            $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() . "\n" .
130
            $e->getTraceAsString() . "\n" .
131
            str_repeat('=', 80) . "\n"
132
        );
133
    }
134
135
    /**
136
     * @param Message $message
137
     */
138
    public function onMessage(Message $message)
139
    {
140
        try {
141
            $this->middlewares->handle($message);
142
        } catch (\Exception $e) {
143
            $this->logException($e);
144
        }
145
    }
146
147
    /**
148
     * @TODO I do not know if it works
149
     * @param Stream $stream
150
     */
151
    public function onClose(Stream $stream)
152
    {
153
        $stream->reconnect();
154
    }
155
156
    /**
157
     * @param Stream $stream
158
     * @param \Exception $e
159
     */
160
    public function onError(Stream $stream, \Exception $e)
161
    {
162
        $this->logException($e);
163
    }
164
165
    /**
166
     * @param $text
167
     * @return $this
168
     */
169
    public function write($text)
170
    {
171
        if (\Config::get('gitter.output')) {
172
            $client = \App::make(Client::class);
173
174
            $client->request('message.send', ['roomId' => $this->id], [
175
                'text' => (string)$text,
176
            ], 'POST');
177
        }
178
179
        return $this;
180
    }
181
}
182