|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of forecast.it.fill project. |
|
7
|
|
|
* (c) Patrick Jaja <[email protected]> |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ForecastAutomation\MattermostClient\Business; |
|
13
|
|
|
|
|
14
|
|
|
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostConfigDto; |
|
15
|
|
|
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostPostsQueryDto; |
|
16
|
|
|
use ForecastAutomation\MattermostClient\Shared\Plugin\Filter\ChannelFilterInterface; |
|
17
|
|
|
use GuzzleHttp\Client; |
|
18
|
|
|
|
|
19
|
|
|
class MattermostApi |
|
20
|
|
|
{ |
|
21
|
|
|
private const AUTH_API = '/api/v4/users/login'; |
|
22
|
|
|
private const CHANNEL_API = '/api/v4/users/me/teams/%s/channels'; |
|
23
|
|
|
private const POSTS_API = '/api/v4/channels/%s/posts'; |
|
24
|
|
|
|
|
25
|
|
|
public static string $token = ''; |
|
26
|
|
|
|
|
27
|
2 |
|
public function __construct(private Client $guzzleClient, private MattermostConfigDto $mattermostConfigDto) |
|
28
|
|
|
{ |
|
29
|
2 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function getChannel(array $channelFilterCollection): array |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->auth(); |
|
34
|
1 |
|
$res = $this->guzzleClient->request( |
|
35
|
1 |
|
'GET', |
|
36
|
1 |
|
sprintf(self::CHANNEL_API, $this->mattermostConfigDto->teamId), |
|
37
|
|
|
[ |
|
38
|
|
|
'headers' => [ |
|
39
|
1 |
|
'Authorization' => 'Bearer '.static::$token, |
|
40
|
1 |
|
'Content-Type' => 'application/json', |
|
41
|
|
|
], |
|
42
|
|
|
] |
|
43
|
|
|
); |
|
44
|
1 |
|
$channelArray = json_decode((string) $res->getBody(), null, 512, JSON_THROW_ON_ERROR); |
|
45
|
|
|
|
|
46
|
1 |
|
return $this->applyChannelFilter($channelArray, $channelFilterCollection); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function getPosts(MattermostPostsQueryDto $postsQueryDto): array |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->auth(); |
|
52
|
1 |
|
$res = $this->guzzleClient->request( |
|
53
|
1 |
|
'GET', |
|
54
|
1 |
|
sprintf(self::POSTS_API, $postsQueryDto->channelId), |
|
55
|
|
|
[ |
|
56
|
1 |
|
'query' => ['since' => (int) $postsQueryDto->since->format('U') * 1000], |
|
57
|
|
|
'headers' => [ |
|
58
|
1 |
|
'Authorization' => 'Bearer '.static::$token, |
|
59
|
1 |
|
'Content-Type' => 'application/json', |
|
60
|
|
|
], |
|
61
|
|
|
], |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
1 |
|
return json_decode((string) $res->getBody(), true, 512, JSON_THROW_ON_ERROR)['posts']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
private function auth(): string |
|
68
|
|
|
{ |
|
69
|
2 |
|
if ('' !== static::$token) { |
|
70
|
2 |
|
return static::$token; |
|
71
|
|
|
} |
|
72
|
|
|
$res = $this->guzzleClient->request( |
|
73
|
|
|
'POST', |
|
74
|
|
|
self::AUTH_API, |
|
75
|
|
|
[ |
|
76
|
|
|
'body' => json_encode( |
|
77
|
|
|
[ |
|
78
|
|
|
'login_id' => $this->mattermostConfigDto->username, |
|
79
|
|
|
'password' => $this->mattermostConfigDto->password, |
|
80
|
|
|
], |
|
81
|
|
|
JSON_THROW_ON_ERROR |
|
82
|
|
|
), |
|
83
|
|
|
], |
|
84
|
|
|
); |
|
85
|
|
|
$token = $res->getHeader('token'); |
|
86
|
|
|
if (0 === \count($token)) { |
|
87
|
|
|
throw new \Exception('could not auth to mattermost'); |
|
88
|
|
|
} |
|
89
|
|
|
static::$token = $token[0]; |
|
90
|
|
|
|
|
91
|
|
|
return static::$token; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
private function applyChannelFilter( |
|
95
|
|
|
array $channelArray, |
|
96
|
|
|
array $channelFilterCollection |
|
97
|
|
|
): array { |
|
98
|
1 |
|
foreach ($channelFilterCollection as $channelFilter) { |
|
99
|
1 |
|
$channelArray = $channelFilter->apply($channelArray); |
|
100
|
|
|
} |
|
101
|
1 |
|
return $channelArray; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|