Passed
Push — master ( 1f78dc...89a587 )
by Patrick
03:55 queued 14s
created

GitlabActivityPlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 48
ccs 20
cts 23
cp 0.8696
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 4 1
A mapEventsToActivity() 0 24 5
A getNeedle() 0 9 3
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\GitlabClient\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\GitlabClient\Shared\Dto\GitlabQueryDto;
18
use ForecastAutomation\Kernel\Shared\Plugin\AbstractPlugin;
19
20
/**
21
 * @method \ForecastAutomation\GitlabClient\GitlabClientFacade getFacade()
22
 */
23
class GitlabActivityPlugin extends AbstractPlugin implements ActivityPluginInterface
24
{
25
    public const ALLOWED_ACTION_NAMES = ['commented on', 'approved'];
26
    public const ACTIVITY_SUFFIX = 'Entwicklungsprozess';
27
    public const ACTIVITY_DURATION = 15;
28
29 1
    public function collect(): ActivityDtoCollection
30
    {
31 1
        return $this->mapEventsToActivity(
32 1
            $this->getFacade()->getEvents(new GitlabQueryDto(date(date('Y-m-d', strtotime('-1 day')))))
33
        );
34
    }
35
36 1
    private function mapEventsToActivity(array $events): ActivityDtoCollection
37
    {
38 1
        $activityDtoArray = [];
39 1
        foreach ($events as $event) {
40 1
            if (\in_array($event->action_name, self::ALLOWED_ACTION_NAMES, true)) {
41 1
                $duration = self::ACTIVITY_DURATION;
42
                try {
43 1
                    $ticketNr = $this->getNeedle($event->target_title);
44 1
                    if (isset($activityDtoArray[$ticketNr])) {
45
                        $duration = self::ACTIVITY_DURATION + $activityDtoArray[$ticketNr]->duration;
46
                    }
47 1
                    $activityDtoArray[$ticketNr] = new ActivityDto(
48 1
                        $ticketNr,
49 1
                        sprintf('%s: %s (%s)', self::ACTIVITY_SUFFIX, $event->target_title, $event->action_name),
50 1
                        new \DateTime($event->created_at),
51
                        $duration
52
                    );
53
                } catch (\Exception $e) {
54
                    // needle not found. no activity will be created, its ok.
55
                }
56
            }
57
        }
58
59 1
        return new ActivityDtoCollection(...array_values($activityDtoArray));
60
    }
61
62 1
    private function getNeedle(string $target_title): string
63
    {
64 1
        $matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['GITLAB_PATTERN']);
65 1
        $resultMatch = preg_match($matchPattern, $target_title, $match);
66 1
        if (0 === $resultMatch || !isset($match[0])) {
67
            throw new \Exception('gitlab needle not found for target_title: '.$target_title);
68
        }
69
70 1
        return strtoupper($match[0]);
71
    }
72
}
73