JiraActivityPlugin::collect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
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\JiraClient\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 GuzzleHttp\Promise\Promise;
19
use GuzzleHttp\Promise\PromiseInterface;
20
21
/**
22
 * @method \ForecastAutomation\JiraClient\JiraClientFacade getFacade()
23
 */
24
class JiraActivityPlugin extends AbstractPlugin implements ActivityPluginInterface
25
{
26
    public const ACTIVITY_DURATION = 30;
27
    public const COMMENT_IDENTIFIER = 'Ticket Bearbeitung';
28
29 1
    public function collect(): PromiseInterface
30
    {
31 1
        $wrapPromise = new Promise(
32 1
            function () use (&$wrapPromise) {
33 1
                $comments = $this->getFacade()->getComments(date('Y-m-d 00:00'));
34 1
                $wrapPromise->resolve($this->createActivityDtoCollection($comments));
35 1
            }
36
        );
37
38 1
        return $wrapPromise;
39
    }
40
41 1
    private function createActivityDtoCollection(array $jiraComments): ActivityDtoCollection
42
    {
43 1
        $activityDtoArray = [];
44 1
        foreach ($jiraComments as $jiraTicketNumber => $jiraComment) {
45 1
            $activityDtoArray[] = new ActivityDto(
46 1
                $jiraTicketNumber,
47 1
                sprintf(
48 1
                    '%s: %s - %s',
49 1
                    self::COMMENT_IDENTIFIER,
50
                    $jiraTicketNumber,
51 1
                    sprintf('%s...', substr(preg_replace('/\[[^)]+\]/', '', $jiraComment[0]->body), 0, 60))
52
                ),
53 1
                $jiraComment[0]->updated,
54 1
                self::ACTIVITY_DURATION * \count($jiraComment)
55
            );
56
        }
57
58 1
        return new ActivityDtoCollection(...$activityDtoArray);
59
    }
60
}
61