Completed
Push — master ( 673385...35aecc )
by Luca
02:14
created

Driver::getSchemeModel()   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\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
     * @throws \Exception
74
     */
75
    public function __construct(Container $container)
76
    {
77
        $driverOptions = $this->defaultOptions;
78
        if (isset($container['driver'])) {
79
            $driverOptions = array_merge($driverOptions, $container['driver']);
80
        }
81
        $container['driver'] = $driverOptions;
82
        $container['client'] = new Client($container);
83
84
        $this->container = $container;
85
    }
86
87
    /**
88
     * @return \Psr\Http\Message\ResponseInterface
89
     */
90
    public function authenticate()
91
    {
92
        $driverOptions = $this->container['driver'];
93
        $requestOptions = [
94
            'login_id' => $driverOptions['login_id'],
95
            'password' => $driverOptions['password']
96
        ];
97
98
        $response = $this->getUserModel()->loginToUserAccount($requestOptions);
99
100
        if ($response->getStatusCode() == 200) {
101
            $token = $response->getHeader('Token')[0];
102
            $this->container['client']->setToken($token);
103
        }
104
105
        return $response;
106
    }
107
108
    /**
109
     * @param $className
110
     * @return mixed
111
     */
112
    private function getModel($className)
113
    {
114
        if (!isset($this->models[$className])) {
115
            $this->models[$className] = new $className($this->container['client']);
116
        }
117
118
        return $this->models[$className];
119
    }
120
121
    /**
122
     * @return UserModel
123
     */
124
    public function getUserModel()
125
    {
126
        return $this->getModel(UserModel::class);
127
    }
128
129
    /**
130
     * @return TeamModel
131
     */
132
    public function getTeamModel()
133
    {
134
        return $this->getModel(TeamModel::class);
135
    }
136
137
    /**
138
     * @return ChannelModel
139
     */
140
    public function getChannelModel()
141
    {
142
        return $this->getModel(ChannelModel::class);
143
    }
144
145
    /**
146
     * @return PostModel
147
     */
148
    public function getPostModel()
149
    {
150
        return $this->getModel(PostModel::class);
151
    }
152
153
    /**
154
     * @return FileModel
155
     */
156
    public function getFileModel()
157
    {
158
        return $this->getModel(FileModel::class);
159
    }
160
161
    /**
162
     * @param $userId
163
     * @return PreferenceModel
164
     */
165
    public function getPreferenceModel($userId)
166
    {
167
        if (!isset($this->models[PreferenceModel::class])) {
168
            $this->models[PreferenceModel::class] = new PreferenceModel($this->container['client'], $userId);
169
        }
170
171
        return $this->models[PreferenceModel::class];
172
    }
173
174
    /**
175
     * @return WebhookModel
176
     */
177
    public function getWebhookModel()
178
    {
179
        return $this->getModel(WebhookModel::class);
180
    }
181
182
    /**
183
     * @return SystemModel
184
     */
185
    public function getSystemModel()
186
    {
187
        return $this->getModel(SystemModel::class);
188
    }
189
190
    /**
191
     * @return ComplianceModel
192
     */
193
    public function getComplianceModel()
194
    {
195
        return $this->getModel(ComplianceModel::class);
196
    }
197
198
    /**
199
     * @return CommandModel
200
     */
201
    public function getCommandModel()
202
    {
203
        return $this->getModel(CommandModel::class);
204
    }
205
206
    /**
207
     * @return ClusterModel
208
     */
209
    public function getClusterModel()
210
    {
211
        return $this->getModel(ClusterModel::class);
212
    }
213
214
    /**
215
     * @return BrandModel
216
     */
217
    public function getBrandModel()
218
    {
219
        return $this->getModel(BrandModel::class);
220
    }
221
222
    /**
223
     * @return LDAPModel
224
     */
225
    public function getLDAPModel()
226
    {
227
        return $this->getModel(LDAPModel::class);
228
    }
229
230
    /**
231
     * @return OAuthModel
232
     */
233
    public function getOAuthModel()
234
    {
235
        return $this->getModel(OAuthModel::class);
236
    }
237
238
    /**
239
     * @return SAMLModel
240
     */
241
    public function getSAMLModel()
242
    {
243
        return $this->getModel(SAMLModel::class);
244
    }
245
246
    /**
247
     * @return ElasticsearchModel
248
     */
249
    public function getElasticsearchModel()
250
    {
251
        return $this->getModel(ElasticsearchModel::class);
252
    }
253
254
    /**
255
     * @return EmojiModel
256
     */
257
    public function getEmojiModel()
258
    {
259
        return $this->getModel(EmojiModel::class);
260
    }
261
262
    /**
263
     * @return ReactionModel
264
     */
265
    public function getReactionModel()
266
    {
267
        return $this->getModel(ReactionModel::class);
268
    }
269
270
    /**
271
     * @return DataRetentionModel
272
     */
273
    public function getDataRetentionModel()
274
    {
275
        return $this->getModel(DataRetentionModel::class);
276
    }
277
278
    /**
279
     * @return JobModel
280
     */
281
    public function getJobModel()
282
    {
283
        return $this->getModel(JobModel::class);
284
    }
285
286
    /**
287
     * @return PluginModel
288
     */
289
    public function getPluginModel()
290
    {
291
        return $this->getModel(PluginModel::class);
292
    }
293
294
    /**
295
     * @return RoleModel
296
     */
297
    public function getRoleModel()
298
    {
299
        return $this->getModel(RoleModel::class);
300
    }
301
302
    /**
303
     * @return SchemeModel
304
     */
305
    public function getSchemeModel()
306
    {
307
        return $this->getModel(SchemeModel::class);
308
    }
309
}
310