|
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\Shared\Plugin; |
|
13
|
|
|
|
|
14
|
|
|
use ForecastAutomation\Activity\Shared\Dto\ActivityDto; |
|
15
|
|
|
use ForecastAutomation\Activity\Shared\Dto\ActivityDtoCollection; |
|
16
|
|
|
use ForecastAutomation\Activity\Shared\Plugin\ActivityPluginInterface; |
|
17
|
|
|
use ForecastAutomation\Kernel\Shared\Plugin\AbstractPlugin; |
|
18
|
|
|
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostChannelFilterQueryDto; |
|
19
|
|
|
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostPostsQueryDto; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @method \ForecastAutomation\MattermostClient\MattermostClientFacade getFacade() |
|
23
|
|
|
*/ |
|
24
|
|
|
class MattermostActivityPlugin extends AbstractPlugin implements ActivityPluginInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public const POST_SUFFIX = 'Abstimmung'; |
|
27
|
|
|
public const ACTIVITY_DURATION = 15; |
|
28
|
|
|
|
|
29
|
1 |
|
public function collect(): ActivityDtoCollection |
|
30
|
|
|
{ |
|
31
|
1 |
|
$channelWithActivity = $this->getFacade()->getChannel( |
|
32
|
1 |
|
new MattermostChannelFilterQueryDto(new \DateTime(date('Y-m-d'))) |
|
33
|
|
|
); |
|
34
|
1 |
|
$filteredPostsCollection = []; |
|
35
|
1 |
|
foreach ($channelWithActivity as $channel) { |
|
36
|
1 |
|
$postsCollection = $this->getFacade()->getPosts( |
|
37
|
1 |
|
(new MattermostPostsQueryDto($channel->id, new \DateTime(date('Y-m-d')))) |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
1 |
|
$filteredPostsCollection += $this->filterPosts($postsCollection) |
|
41
|
|
|
; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return $this->mapEventsToActivity($filteredPostsCollection); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
private function filterPosts(array $postsCollection): array |
|
48
|
|
|
{ |
|
49
|
1 |
|
$filteredPosts = []; |
|
50
|
1 |
|
foreach ($postsCollection as $post) { |
|
51
|
1 |
|
if ($this->hasNeedle($post['message'])) { |
|
52
|
1 |
|
$filteredPosts[] = $post; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $filteredPosts; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
private function mapEventsToActivity(array $filteredPostsCollection): ActivityDtoCollection |
|
60
|
|
|
{ |
|
61
|
1 |
|
$activityDtoArray = []; |
|
62
|
1 |
|
foreach ($filteredPostsCollection as $post) { |
|
63
|
1 |
|
$duration = self::ACTIVITY_DURATION; |
|
64
|
1 |
|
$ticketNr = $this->getNeedle($post['message']); |
|
65
|
1 |
|
if (isset($activityDtoArray[$ticketNr])) { |
|
66
|
|
|
$duration = self::ACTIVITY_DURATION + $activityDtoArray[$ticketNr]->duration; |
|
67
|
|
|
} |
|
68
|
1 |
|
$activityDtoArray[$ticketNr] = new ActivityDto( |
|
69
|
1 |
|
$ticketNr, |
|
70
|
1 |
|
sprintf('%s: %s', self::POST_SUFFIX, $ticketNr), |
|
71
|
1 |
|
new \DateTime(date('d-m-Y', (int) ($post['create_at'] / 1000))), |
|
72
|
|
|
$duration |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return new ActivityDtoCollection(...array_values($activityDtoArray)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
private function getNeedle(string $target_title): string |
|
80
|
|
|
{ |
|
81
|
1 |
|
$matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['GITLAB_PATTERN']); |
|
82
|
1 |
|
$resultMatch = preg_match($matchPattern, $target_title, $match); |
|
83
|
1 |
|
if (0 === $resultMatch || !isset($match[0])) { |
|
84
|
|
|
throw new \Exception('gitlab needle not found for target_title: '.$target_title); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return strtoupper($match[0]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
private function hasNeedle(string $target_title): bool |
|
91
|
|
|
{ |
|
92
|
1 |
|
$matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['MATTERMOST_PATTERN']); |
|
93
|
1 |
|
$resultMatch = preg_match($matchPattern, $target_title, $match); |
|
94
|
1 |
|
if (0 === $resultMatch || !isset($match[0])) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|