Completed
Push — master ( cf3721...4140f8 )
by Luca
05:10
created

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