Completed
Push — master ( 79a7c0...e39ede )
by Luca
01:15
created

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