Completed
Push — master ( 6bb649...990b24 )
by Luca
04:42
created

Driver::getEmojiModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\SystemModel;
33
use Gnello\Mattermost\Models\TeamModel;
34
use Gnello\Mattermost\Models\UserModel;
35
use Gnello\Mattermost\Models\WebhookModel;
36
use Pimple\Container;
37
38
/**
39
 * Class Driver
40
 *
41
 * @package Gnello\Mattermost
42
 */
43
class Driver
44
{
45
    /**
46
     * Default options of the Driver
47
     *
48
     * @var array
49
     */
50
    private $defaultOptions = [
51
        'scheme'    => 'https',
52
        'basePath'  => '/api/v4',
53
        'url'       => 'localhost',
54
        'login_id'  => null,
55
        'password'  => null,
56
    ];
57
58
    /**
59
     * @var Container
60
     */
61
    private $container;
62
63
    /**
64
     * @var array
65
     */
66
    private $models = [];
67
68
    /**
69
     * Driver constructor.
70
     *
71
     * @param Container $container
72
     * @throws \Exception
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 $className
109
     * @return mixed
110
     */
111
    private function getModel($className)
112
    {
113
        if (!isset($this->models[$className])) {
114
            $this->models[$className] = new $className($this->container['client']);
115
        }
116
117
        return $this->models[$className];
118
    }
119
120
    /**
121
     * @return UserModel
122
     */
123
    public function getUserModel()
124
    {
125
        return $this->getModel(UserModel::class);
126
    }
127
128
    /**
129
     * @return TeamModel
130
     */
131
    public function getTeamModel()
132
    {
133
        return $this->getModel(TeamModel::class);
134
    }
135
136
    /**
137
     * @return ChannelModel
138
     */
139
    public function getChannelModel()
140
    {
141
        return $this->getModel(ChannelModel::class);
142
    }
143
144
    /**
145
     * @return PostModel
146
     */
147
    public function getPostModel()
148
    {
149
        return $this->getModel(PostModel::class);
150
    }
151
152
    /**
153
     * @return FileModel
154
     */
155
    public function getFileModel()
156
    {
157
        return $this->getModel(FileModel::class);
158
    }
159
160
    /**
161
     * @param $userId
162
     * @return PreferenceModel
163
     */
164
    public function getPreferenceModel($userId)
165
    {
166
        if (!isset($this->models[PreferenceModel::class])) {
167
            $this->models[PreferenceModel::class] = new PreferenceModel($this->container['client'], $userId);
168
        }
169
170
        return $this->models[PreferenceModel::class];
171
    }
172
173
    /**
174
     * @return WebhookModel
175
     */
176
    public function getWebhookModel()
177
    {
178
        return $this->getModel(WebhookModel::class);
179
    }
180
181
    /**
182
     * @return SystemModel
183
     */
184
    public function getSystemModel()
185
    {
186
        return $this->getModel(SystemModel::class);
187
    }
188
189
    /**
190
     * @return ComplianceModel
191
     */
192
    public function getComplianceModel()
193
    {
194
        return $this->getModel(ComplianceModel::class);
195
    }
196
197
    /**
198
     * @return CommandModel
199
     */
200
    public function getCommandModel()
201
    {
202
        return $this->getModel(CommandModel::class);
203
    }
204
205
    /**
206
     * @return ClusterModel
207
     */
208
    public function getClusterModel()
209
    {
210
        return $this->getModel(ClusterModel::class);
211
    }
212
213
    /**
214
     * @return BrandModel
215
     */
216
    public function getBrandModel()
217
    {
218
        return $this->getModel(BrandModel::class);
219
    }
220
221
    /**
222
     * @return LDAPModel
223
     */
224
    public function getLDAPModel()
225
    {
226
        return $this->getModel(LDAPModel::class);
227
    }
228
229
    /**
230
     * @return OAuthModel
231
     */
232
    public function getOAuthModel()
233
    {
234
        return $this->getModel(OAuthModel::class);
235
    }
236
237
    /**
238
     * @return SAMLModel
239
     */
240
    public function getSAMLModel()
241
    {
242
        return $this->getModel(SAMLModel::class);
243
    }
244
245
    /**
246
     * @return ElasticsearchModel
247
     */
248
    public function getElasticsearchModel()
249
    {
250
        return $this->getModel(ElasticsearchModel::class);
251
    }
252
253
    /**
254
     * @return EmojiModel
255
     */
256
    public function getEmojiModel()
257
    {
258
        return $this->getModel(EmojiModel::class);
259
    }
260
261
    /**
262
     * @return ReactionModel
263
     */
264
    public function getReactionModel()
265
    {
266
        return $this->getModel(ReactionModel::class);
267
    }
268
269
    /**
270
     * @return DataRetentionModel
271
     */
272
    public function getDataRetentionModel()
273
    {
274
        return $this->getModel(DataRetentionModel::class);
275
    }
276
277
    /**
278
     * @return JobModel
279
     */
280
    public function getJobModel()
281
    {
282
        return $this->getModel(JobModel::class);
283
    }
284
285
    /**
286
     * @return PluginModel
287
     */
288
    public function getPluginModel()
289
    {
290
        return $this->getModel(PluginModel::class);
291
    }
292
293
    /**
294
     * @return RoleModel
295
     */
296
    public function getRoleModel()
297
    {
298
        return $this->getModel(RoleModel::class);
299
    }
300
}
301