Completed
Push — master ( d0f8e4...6a2707 )
by Luca
01:22
created

Driver::getBrandModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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