Completed
Push — master ( 76b355...56bea0 )
by Luca
01:20
created

Driver::getBotModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This Driver is based entirely on official documentation of the Mattermost Web
4
 * Services API and you can extend it by following the directives of the documentation.
5
 *
6
 * God bless this mess.
7
 *
8
 * @author Luca Agnello <[email protected]>
9
 * @link https://api.mattermost.com/
10
 */
11
12
namespace Gnello\Mattermost;
13
14
use Gnello\Mattermost\Models\BotModel;
15
use Gnello\Mattermost\Models\BrandModel;
16
use Gnello\Mattermost\Models\ChannelModel;
17
use Gnello\Mattermost\Models\ClusterModel;
18
use Gnello\Mattermost\Models\CommandModel;
19
use Gnello\Mattermost\Models\ComplianceModel;
20
use Gnello\Mattermost\Models\DataRetentionModel;
21
use Gnello\Mattermost\Models\ElasticsearchModel;
22
use Gnello\Mattermost\Models\EmojiModel;
23
use Gnello\Mattermost\Models\FileModel;
24
use Gnello\Mattermost\Models\JobModel;
25
use Gnello\Mattermost\Models\LDAPModel;
26
use Gnello\Mattermost\Models\OAuthModel;
27
use Gnello\Mattermost\Models\PluginModel;
28
use Gnello\Mattermost\Models\PostModel;
29
use Gnello\Mattermost\Models\PreferenceModel;
30
use Gnello\Mattermost\Models\ReactionModel;
31
use Gnello\Mattermost\Models\RoleModel;
32
use Gnello\Mattermost\Models\SAMLModel;
33
use Gnello\Mattermost\Models\SchemeModel;
34
use Gnello\Mattermost\Models\SystemModel;
35
use Gnello\Mattermost\Models\TeamModel;
36
use Gnello\Mattermost\Models\UserModel;
37
use Gnello\Mattermost\Models\WebhookModel;
38
use GuzzleHttp\Psr7\Response;
39
use Pimple\Container;
40
use Psr\Http\Message\ResponseInterface;
41
42
/**
43
 * Class Driver
44
 *
45
 * @package Gnello\Mattermost
46
 */
47
class Driver
48
{
49
    /**
50
     * Default options of the Driver
51
     *
52
     * @var array
53
     */
54
    private $defaultOptions = [
55
        'scheme' => 'https',
56
        'basePath' => '/api/v4',
57
        'url' => 'localhost',
58
        'login_id' => null,
59
        'password' => null,
60
        'token' => null,
61
    ];
62
63
    /**
64
     * @var Container
65
     */
66
    private $container;
67
68
    /**
69
     * @var array
70
     */
71
    private $models = [];
72
73
    /**
74
     * Driver constructor.
75
     *
76
     * @param Container $container
77
     */
78
    public function __construct(Container $container)
79
    {
80
        $driverOptions = $this->defaultOptions;
81
82
        if (isset($container['driver'])) {
83
            $driverOptions = array_merge($driverOptions, $container['driver']);
84
        }
85
86
        $container['driver'] = $driverOptions;
87
        $container['client'] = new Client($container);
88
89
        $this->container = $container;
90
    }
91
92
    /**
93
     * @return ResponseInterface
94
     */
95
    public function authenticate()
96
    {
97
        $driverOptions = $this->container['driver'];
98
99
        if (isset($driverOptions['token'])) {
100
101
            $this->container['client']->setToken($driverOptions['token']);
102
            $response = $this->getUserModel()->getAuthenticatedUser();
103
104
        } else if (isset($driverOptions['login_id']) && isset($driverOptions['password'])) {
105
106
            $response = $this->getUserModel()->loginToUserAccount([
107
                'login_id' => $driverOptions['login_id'],
108
                'password' => $driverOptions['password']
109
            ]);
110
111
            if ($response->getStatusCode() == 200) {
112
                $token = $response->getHeader('Token')[0];
113
                $this->container['client']->setToken($token);
114
            }
115
116
        } else {
117
118
            $response = new Response(401, [], json_encode([
119
                "id" => "missing.credentials.",
120
                "message" => "You must provide a login_id and password or a valid token.",
121
                "detailed_error" => "",
122
                "request_id" => "",
123
                "status_code" => 401,
124
            ]));
125
126
        }
127
128
        return $response;
129
    }
130
131
    /**
132
     * @param $className
133
     * @return mixed
134
     */
135
    private function getModel($className)
136
    {
137
        if (!isset($this->models[$className])) {
138
            $this->models[$className] = new $className($this->container['client']);
139
        }
140
141
        return $this->models[$className];
142
    }
143
144
    /**
145
     * @return UserModel
146
     */
147
    public function getUserModel()
148
    {
149
        return $this->getModel(UserModel::class);
150
    }
151
152
    /**
153
     * @return TeamModel
154
     */
155
    public function getTeamModel()
156
    {
157
        return $this->getModel(TeamModel::class);
158
    }
159
160
    /**
161
     * @return ChannelModel
162
     */
163
    public function getChannelModel()
164
    {
165
        return $this->getModel(ChannelModel::class);
166
    }
167
168
    /**
169
     * @return PostModel
170
     */
171
    public function getPostModel()
172
    {
173
        return $this->getModel(PostModel::class);
174
    }
175
176
    /**
177
     * @return FileModel
178
     */
179
    public function getFileModel()
180
    {
181
        return $this->getModel(FileModel::class);
182
    }
183
184
    /**
185
     * @param $userId
186
     * @return PreferenceModel
187
     */
188
    public function getPreferenceModel($userId)
189
    {
190
        if (!isset($this->models[PreferenceModel::class])) {
191
            $this->models[PreferenceModel::class] = new PreferenceModel($this->container['client'], $userId);
192
        }
193
194
        return $this->models[PreferenceModel::class];
195
    }
196
197
    /**
198
     * @return WebhookModel
199
     */
200
    public function getWebhookModel()
201
    {
202
        return $this->getModel(WebhookModel::class);
203
    }
204
205
    /**
206
     * @return SystemModel
207
     */
208
    public function getSystemModel()
209
    {
210
        return $this->getModel(SystemModel::class);
211
    }
212
213
    /**
214
     * @return ComplianceModel
215
     */
216
    public function getComplianceModel()
217
    {
218
        return $this->getModel(ComplianceModel::class);
219
    }
220
221
    /**
222
     * @return CommandModel
223
     */
224
    public function getCommandModel()
225
    {
226
        return $this->getModel(CommandModel::class);
227
    }
228
229
    /**
230
     * @return ClusterModel
231
     */
232
    public function getClusterModel()
233
    {
234
        return $this->getModel(ClusterModel::class);
235
    }
236
237
    /**
238
     * @return BrandModel
239
     */
240
    public function getBrandModel()
241
    {
242
        return $this->getModel(BrandModel::class);
243
    }
244
245
    /**
246
     * @return LDAPModel
247
     */
248
    public function getLDAPModel()
249
    {
250
        return $this->getModel(LDAPModel::class);
251
    }
252
253
    /**
254
     * @return OAuthModel
255
     */
256
    public function getOAuthModel()
257
    {
258
        return $this->getModel(OAuthModel::class);
259
    }
260
261
    /**
262
     * @return SAMLModel
263
     */
264
    public function getSAMLModel()
265
    {
266
        return $this->getModel(SAMLModel::class);
267
    }
268
269
    /**
270
     * @return ElasticsearchModel
271
     */
272
    public function getElasticsearchModel()
273
    {
274
        return $this->getModel(ElasticsearchModel::class);
275
    }
276
277
    /**
278
     * @return EmojiModel
279
     */
280
    public function getEmojiModel()
281
    {
282
        return $this->getModel(EmojiModel::class);
283
    }
284
285
    /**
286
     * @return ReactionModel
287
     */
288
    public function getReactionModel()
289
    {
290
        return $this->getModel(ReactionModel::class);
291
    }
292
293
    /**
294
     * @return DataRetentionModel
295
     */
296
    public function getDataRetentionModel()
297
    {
298
        return $this->getModel(DataRetentionModel::class);
299
    }
300
301
    /**
302
     * @return JobModel
303
     */
304
    public function getJobModel()
305
    {
306
        return $this->getModel(JobModel::class);
307
    }
308
309
    /**
310
     * @return PluginModel
311
     */
312
    public function getPluginModel()
313
    {
314
        return $this->getModel(PluginModel::class);
315
    }
316
317
    /**
318
     * @return RoleModel
319
     */
320
    public function getRoleModel()
321
    {
322
        return $this->getModel(RoleModel::class);
323
    }
324
325
    /**
326
     * @return SchemeModel
327
     */
328
    public function getSchemeModel()
329
    {
330
        return $this->getModel(SchemeModel::class);
331
    }
332
333
    /**
334
     * @return BotModel
335
     */
336
    public function getBotModel()
337
    {
338
        return $this->getModel(BotModel::class);
339
    }
340
}
341