Passed
Push — master ( c86b68...6e4e21 )
by Patrick
01:30 queued 12s
created

MattermostActivityPlugin::getNeedle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
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\MattermostPostsQueryDto;
19
use ForecastAutomation\MattermostClient\Shared\Plugin\Filter\ChannelFilterInterface;
20
use GuzzleHttp\Promise\PromiseInterface;
21
use GuzzleHttp\Promise\Utils;
22
23
/**
24
 * @method \ForecastAutomation\MattermostClient\MattermostClientFacade getFacade()
25
 */
26
class MattermostActivityPlugin extends AbstractPlugin implements ActivityPluginInterface
27
{
28
    public const POST_SUFFIX = 'Abstimmung';
29
    public const ACTIVITY_DURATION = 15;
30
31
    private array $channelFilterCollection;
32
33 1
    public function __construct(ChannelFilterInterface ...$channelFilterCollection)
34
    {
35 1
        $this->channelFilterCollection = $channelFilterCollection;
36 1
        parent::__construct();
37 1
    }
38
39 1
    public function collect(): PromiseInterface
40
    {
41 1
        return $this->getFacade()
42 1
            ->getChannel($this->channelFilterCollection)
43 1
            ->then(
44 1
                function (array $channels) {
45 1
                    $postPromises = [];
46 1
                    foreach ($channels as $channel) {
47 1
                        $postPromises[] = $this->getFacade()->getPosts(
48 1
                            (new MattermostPostsQueryDto($channel->id, new \DateTime(date('Y-m-d'))))
49
                        );
50
                    }
51
52 1
                    $postsCollection = Utils::all($postPromises)->wait();
53
54 1
                    return $this->mapEventsToActivity($this->filterPosts($postsCollection));
55 1
                }
56
            )
57
        ;
58
    }
59
60 1
    private function filterPosts(array $postsCollection): array
61
    {
62 1
        $filteredPosts = [];
63
64 1
        foreach ($postsCollection as $postpackCollection) {
65 1
            foreach ($postpackCollection as $post) {
66 1
                if ($this->hasNeedle($post['message'])) {
67 1
                    $filteredPosts[] = $post;
68
                }
69
            }
70
        }
71
72 1
        return $filteredPosts;
73
    }
74
75 1
    private function mapEventsToActivity(array $filteredPostsCollection): ActivityDtoCollection
76
    {
77 1
        $activityDtoArray = [];
78 1
        foreach ($filteredPostsCollection as $post) {
79 1
            $duration = self::ACTIVITY_DURATION;
80 1
            $ticketNr = $this->getNeedle($post['message']);
81 1
            if (isset($activityDtoArray[$ticketNr])) {
82
                $duration = self::ACTIVITY_DURATION + $activityDtoArray[$ticketNr]->duration;
83
            }
84 1
            $activityDtoArray[$ticketNr] = new ActivityDto(
85 1
                $ticketNr,
86 1
                sprintf('%s: %s', self::POST_SUFFIX, $ticketNr),
87 1
                new \DateTime(date('d-m-Y', (int) ($post['create_at'] / 1000))),
88
                $duration
89
            );
90
        }
91
92 1
        return new ActivityDtoCollection(...array_values($activityDtoArray));
93
    }
94
95 1
    private function getNeedle(string $target_title): string
96
    {
97 1
        $matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['GITLAB_PATTERN']);
98 1
        $resultMatch = preg_match($matchPattern, $target_title, $match);
99 1
        if (0 === $resultMatch || ! isset($match[0])) {
100
            throw new \Exception('gitlab needle not found for target_title: '.$target_title);
101
        }
102
103 1
        return strtoupper($match[0]);
104
    }
105
106 1
    private function hasNeedle(string $target_title): bool
107
    {
108 1
        $matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['MATTERMOST_PATTERN']);
109 1
        $resultMatch = preg_match($matchPattern, $target_title, $match);
110 1
        if (0 === $resultMatch || ! isset($match[0])) {
111
            return false;
112
        }
113
114 1
        return true;
115
    }
116
}
117