Passed
Push — master ( 20d3fa...58226a )
by Patrick
03:16 queued 13s
created

GitlabActivityPlugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 44
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 4 1
A mapEventsToActivity() 0 20 4
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 1
                $ticketNr = $this->getNeedle($event->target_title);
43 1
                if (isset($activityDtoArray[$ticketNr])) {
44
                    $duration = self::ACTIVITY_DURATION + $activityDtoArray[$ticketNr]->duration;
45
                }
46 1
                $activityDtoArray[$ticketNr] = new ActivityDto(
47 1
                    $ticketNr,
48 1
                    sprintf('%s: %s (%s)', self::ACTIVITY_SUFFIX, $event->target_title, $event->action_name),
49 1
                    new \DateTime($event->created_at),
50
                    $duration
51
                );
52
            }
53
        }
54
55 1
        return new ActivityDtoCollection(...array_values($activityDtoArray));
56
    }
57
58 1
    private function getNeedle(string $target_title): string
59
    {
60 1
        $matchPattern = sprintf('(%s-[0-9]{1,})i', $_ENV['GITLAB_PATTERN']);
61 1
        $resultMatch = preg_match($matchPattern, $target_title, $match);
62 1
        if (0 === $resultMatch || !isset($match[0])) {
63
            throw new \Exception('gitlab needle not found for target_title: '.$target_title);
64
        }
65
66 1
        return strtoupper($match[0]);
67
    }
68
}
69