Completed
Push — master ( 6a2707...50ed85 )
by Luca
01:32
created

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