Completed
Pull Request — master (#17)
by
unknown
01:48
created

Driver::getSystemModel()   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
    /**
102
     * @param $className
103
     * @return mixed
104
     */
105
    private function getModel($className)
106
    {
107
        if (!isset($this->models[$className])) {
108
            $this->models[$className] = new $className($this->container['client']);
109
        }
110
111
        return $this->models[$className];
112
    }
113
114
    /**
115
     * @return UserModel
116
     */
117
    public function getUserModel()
118
    {
119
        return $this->getModel(UserModel::class);
120
    }
121
122
    /**
123
     * @return TeamModel
124
     */
125
    public function getTeamModel()
126
    {
127
        return $this->getModel(TeamModel::class);
128
    }
129
130
    /**
131
     * @return ChannelModel
132
     */
133
    public function getChannelModel()
134
    {
135
        return $this->getModel(ChannelModel::class);
136
    }
137
138
    /**
139
     * @return PostModel
140
     */
141
    public function getPostModel()
142
    {
143
        return $this->getModel(PostModel::class);
144
    }
145
146
    /**
147
     * @return FileModel
148
     */
149
    public function getFileModel()
150
    {
151
        return $this->getModel(FileModel::class);
152
    }
153
154
    /**
155
     * @param $userId
156
     * @return PreferenceModel
157
     */
158
    public function getPreferenceModel($userId)
159
    {
160
        if (!isset($this->models[PreferenceModel::class])) {
161
            $this->models[PreferenceModel::class] = new PreferenceModel($this->container['client'], $userId);
162
        }
163
164
        return $this->models[PreferenceModel::class];
165
    }
166
167
    /**
168
     * @return WebhookModel
169
     */
170
    public function getWebhookModel()
171
    {
172
        return $this->getModel(WebhookModel::class);
173
    }
174
175
    /**
176
     * @return SystemModel
177
     */
178
    public function getSystemModel()
179
    {
180
        return $this->getModel(SystemModel::class);
181
    }
182
183
    /**
184
     * @return ComplianceModel
185
     */
186
    public function getComplianceModel()
187
    {
188
        return $this->getModel(ComplianceModel::class);
189
    }
190
191
    /**
192
     * @return CommandModel
193
     */
194
    public function getCommandModel()
195
    {
196
        return $this->getModel(CommandModel::class);
197
    }
198
199
    /**
200
     * @return ClusterModel
201
     */
202
    public function getClusterModel()
203
    {
204
        return $this->getModel(ClusterModel::class);
205
    }
206
207
    /**
208
     * @return BrandModel
209
     */
210
    public function getBrandModel()
211
    {
212
        return $this->getModel(BrandModel::class);
213
    }
214
215
    /**
216
     * @return LDAPModel
217
     */
218
    public function getLDAPModel()
219
    {
220
        return $this->getModel(LDAPModel::class);
221
    }
222
223
    /**
224
     * @return OAuthModel
225
     */
226
    public function getOAuthModel()
227
    {
228
        return $this->getModel(OAuthModel::class);
229
    }
230
231
    /**
232
     * @return SAMLModel
233
     */
234
    public function getSAMLModel()
235
    {
236
        return $this->getModel(SAMLModel::class);
237
    }
238
239
    /**
240
     * @return ElasticsearchModel
241
     */
242
    public function getElasticsearchModel()
243
    {
244
        return $this->getModel(ElasticsearchModel::class);
245
    }
246
247
    /**
248
     * @return EmojiModel
249
     */
250
    public function getEmojiModel()
251
    {
252
        return $this->getModel(EmojiModel::class);
253
    }
254
255
    /**
256
     * @return ReactionModel
257
     */
258
    public function getReactionModel()
259
    {
260
        return $this->getModel(ReactionModel::class);
261
    }
262
}
263