Passed
Push — master ( 1f78dc...89a587 )
by Patrick
03:55 queued 14s
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\IsDirectChannelFilter;
0 ignored issues
show
Bug introduced by
The type ForecastAutomation\Matte...o\IsDirectChannelFilter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostPostsQueryDto;
20
use ForecastAutomation\MattermostClient\Shared\Plugin\Filter\ChannelFilterInterface;
21
use ForecastAutomation\MattermostClient\Shared\Plugin\Filter\HasMessageChannelFilter;
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(): ActivityDtoCollection
40
    {
41 1
        $channelWithActivity = $this->getFacade()->getChannel($this->channelFilterCollection);
42 1
        $filteredPostsCollection = [];
43 1
        foreach ($channelWithActivity as $channel) {
44 1
            $postsCollection = $this->getFacade()->getPosts(
45 1
                (new MattermostPostsQueryDto($channel->id, new \DateTime(date('Y-m-d'))))
46
            );
47
48 1
            $filteredPostsCollection += $this->filterPosts($postsCollection)
49
            ;
50
        }
51
52 1
        return $this->mapEventsToActivity($filteredPostsCollection);
53
    }
54
55 1
    private function filterPosts(array $postsCollection): array
56
    {
57 1
        $filteredPosts = [];
58 1
        foreach ($postsCollection as $post) {
59 1
            if ($this->hasNeedle($post['message'])) {
60 1
                $filteredPosts[] = $post;
61
            }
62
        }
63
64 1
        return $filteredPosts;
65
    }
66
67 1
    private function mapEventsToActivity(array $filteredPostsCollection): ActivityDtoCollection
68
    {
69 1
        $activityDtoArray = [];
70 1
        foreach ($filteredPostsCollection as $post) {
71 1
            $duration = self::ACTIVITY_DURATION;
72 1
            $ticketNr = $this->getNeedle($post['message']);
73 1
            if (isset($activityDtoArray[$ticketNr])) {
74
                $duration = self::ACTIVITY_DURATION + $activityDtoArray[$ticketNr]->duration;
75
            }
76 1
            $activityDtoArray[$ticketNr] = new ActivityDto(
77 1
                $ticketNr,
78 1
                sprintf('%s: %s', self::POST_SUFFIX, $ticketNr),
79 1
                new \DateTime(date('d-m-Y', (int) ($post['create_at'] / 1000))),
80
                $duration
81
            );
82
        }
83
84 1
        return new ActivityDtoCollection(...array_values($activityDtoArray));
85
    }
86
87 1
    private function getNeedle(string $target_title): string
88
    {
89 1
        $matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['GITLAB_PATTERN']);
90 1
        $resultMatch = preg_match($matchPattern, $target_title, $match);
91 1
        if (0 === $resultMatch || !isset($match[0])) {
92
            throw new \Exception('gitlab needle not found for target_title: '.$target_title);
93
        }
94
95 1
        return strtoupper($match[0]);
96
    }
97
98 1
    private function hasNeedle(string $target_title): bool
99
    {
100 1
        $matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['MATTERMOST_PATTERN']);
101 1
        $resultMatch = preg_match($matchPattern, $target_title, $match);
102 1
        if (0 === $resultMatch || !isset($match[0])) {
103
            return false;
104
        }
105
106 1
        return true;
107
    }
108
}
109