Completed
Push — master ( e8e767...e951ab )
by Kirill
02:50
created

Client::notify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace Gitter;
9
10
use Monolog\Logger;
11
use Psr\Log\NullLogger;
12
use Gitter\Resources\Rooms;
13
use Gitter\Resources\Users;
14
use Gitter\Support\Loggable;
15
use Psr\Log\LoggerInterface;
16
use Gitter\Resources\Common;
17
use Gitter\Resources\Groups;
18
use Gitter\Resources\Messages;
19
use Serafim\Properties\Properties;
20
use React\EventLoop\LoopInterface;
21
use Gitter\Support\AdaptersStorage;
22
use Gitter\Resources\ResourceInterface;
23
use React\EventLoop\Factory as LoopFactory;
24
25
/**
26
 * Class Client
27
 * @package Gitter
28
 *
29
 * @property-read string $token
30
 * @property-read LoopInterface $loop
31
 * @property-read AdaptersStorage $adapters
32
 *
33
 * @property-read Common|ResourceInterface $request
34
 *
35
 * @property-read Rooms|ResourceInterface $rooms
36
 * @property-read Users|ResourceInterface $users
37
 * @property-read Groups|ResourceInterface $groups
38
 * @property-read Messages|ResourceInterface $messages
39
 */
40
class Client implements Loggable
41
{
42
    use Properties;
43
44
    /**
45
     * @var string
46
     */
47
    const VERSION = '3.0.2';
48
49
    /**
50
     * @var string
51
     */
52
    protected $token;
53
54
    /**
55
     * @var LoopInterface
56
     */
57
    protected $loop;
58
59
    /**
60
     * @var AdaptersStorage
61
     */
62
    protected $adapters;
63
64
    /**
65
     * @var LoggerInterface
66
     */
67
    private $logger;
68
69
    /**
70
     * @var array|ResourceInterface[]
71
     */
72
    private $resources = [];
73
74
    /**
75
     * Client constructor.
76
     * @param string $token
77
     * @param LoggerInterface $logger
78
     */
79
    public function __construct(string $token, LoggerInterface $logger = null)
80
    {
81
        $this->token = $token;
82
        $this->loop  = LoopFactory::create();
83
84
        if ($logger === null) {
85
            $logger = new NullLogger();
86
        }
87
88
        $this->logger   = $logger;
89
        $this->adapters = new AdaptersStorage($this);
90
91
        $this->logger->info(sprintf('Gitter Client: %s', static::VERSION));
92
    }
93
94
    /**
95
     * @param string $hookId
96
     * @return WebHook
97
     */
98
    public function notify(string $hookId): WebHook
99
    {
100
        return new WebHook($this, $hookId);
101
    }
102
103
    /**
104
     * @param string $message
105
     * @param int $level
106
     * @return Loggable|$this
107
     */
108
    public function log(string $message, int $level = Logger::INFO): Loggable
109
    {
110
        $this->logger->log($level, $message);
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return void
117
     */
118
    public function connect()
119
    {
120
        $this->logger->info('Starting');
121
        $this->loop->run();
122
    }
123
124
    /**
125
     * @return void
126
     */
127
    public function disconnect()
128
    {
129
        $this->logger->info('Stopping');
130
        $this->loop->stop();
131
    }
132
133
    /**
134
     * @return Messages|ResourceInterface
135
     */
136
    protected function getMessages(): Messages
137
    {
138
        return $this->resource(Messages::class);
139
    }
140
141
    /**
142
     * @return Groups|ResourceInterface
143
     */
144
    protected function getGroups(): Groups
145
    {
146
        return $this->resource(Groups::class);
147
    }
148
149
    /**
150
     * @return Rooms|ResourceInterface
151
     */
152
    protected function getRooms(): Rooms
153
    {
154
        return $this->resource(Rooms::class);
155
    }
156
157
    /**
158
     * @return Users|ResourceInterface
159
     */
160
    protected function getUsers(): Users
161
    {
162
        return $this->resource(Users::class);
163
    }
164
165
    /**
166
     * @return Common|ResourceInterface
167
     */
168
    protected function getRequest(): Common
169
    {
170
        return $this->resource(Common::class);
171
    }
172
173
    /**
174
     * @param string $name
175
     * @return ResourceInterface
176
     */
177
    private function resource(string $name)
178
    {
179
        if (!array_key_exists($name, $this->resources)) {
180
            $this->resources[$name] = new $name($this);
181
        }
182
183
        return $this->resources[$name];
184
    }
185
}
186