Completed
Pull Request — master (#25)
by
unknown
01:19
created

Driver::authenticate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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