1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace AppBuilder\Application\Module\BitBucket; |
6
|
|
|
|
7
|
|
|
use AppBuilder\Application\Module\HttpClient\ExternalLibraryHttpClient; |
8
|
|
|
use AppBuilder\Application\Utils\Mapper\Factory\BitbucketMapperFactory; |
9
|
|
|
use AppBuilder\Event\Application\BitbucketTicketMappedEvent; |
10
|
|
|
use AppBuilder\Event\Application\JiraTicketMappedEvent; |
11
|
|
|
use AppBuilder\Event\Application\JiraTicketMappedEventAware; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
|
15
|
|
|
class ExternalLibraryBitBucketService implements JiraTicketMappedEventAware, BitBucketService |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
private const URL_PREFIX = 'https://farpoint.get-ag.com/jira/rest/dev-status/1.0/issue/detail?issueId='; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private const URL_SUFFIX = '&applicationType=stash&dataType=pullrequest'; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $url; |
25
|
|
|
|
26
|
|
|
/** @var LoggerInterface */ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
/** @var ExternalLibraryHttpClient */ |
30
|
|
|
private $httpClient; |
31
|
|
|
|
32
|
|
|
/** @var EventDispatcherInterface */ |
33
|
|
|
private $dispatcher; |
34
|
|
|
|
35
|
1 |
|
public function __construct( |
36
|
|
|
ExternalLibraryHttpClient $httpClient, |
37
|
|
|
LoggerInterface $logger, |
38
|
|
|
EventDispatcherInterface $dispatcher |
39
|
|
|
) { |
40
|
1 |
|
$this->logger = $logger; |
41
|
1 |
|
$this->httpClient = $httpClient; |
42
|
1 |
|
$this->dispatcher = $dispatcher; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** Recives event with mapped Jira ticket. */ |
46
|
1 |
|
public function onJiraTicketMapped(JiraTicketMappedEvent $event) : void |
47
|
|
|
{ |
48
|
1 |
|
$ticketId = $event->ticket()['id']; |
49
|
1 |
|
$this->createUrl($ticketId); |
50
|
1 |
|
$this->fetchBitBucketData($ticketId); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Fetches data from BitBucket by tickets id. |
55
|
|
|
*/ |
56
|
1 |
|
public function fetchBitBucketData(int $ticketId) : void |
57
|
|
|
{ |
58
|
1 |
|
$bbFullTicket = []; |
59
|
|
|
$response = $this |
60
|
1 |
|
->httpClient |
61
|
1 |
|
->request(ExternalLibraryHttpClient::GET, $this->url); |
62
|
1 |
|
$content = $response->getBody()->getContents(); |
63
|
1 |
|
$bbFullTicket[$ticketId] = json_decode($content, true); |
64
|
1 |
|
$this->mapToBitbucketTicket($bbFullTicket, $ticketId); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Maps and filters received BitBucket data to array. |
69
|
|
|
*/ |
70
|
1 |
|
private function mapToBitbucketTicket(array $bbTicket, int $ticketId) : void |
71
|
|
|
{ |
72
|
1 |
|
$mappedTicket = []; |
73
|
1 |
|
$ticketMappers = BitbucketMapperFactory::create(); |
74
|
1 |
|
foreach ($ticketMappers as $mapper) { |
75
|
1 |
|
$mappedTicket[$ticketId][$mapper->outputKey()] = $mapper->map($bbTicket[$ticketId]); |
76
|
|
|
} |
77
|
1 |
|
$this->dispatcher->dispatch( |
78
|
1 |
|
BitbucketTicketMappedEvent::NAME, |
79
|
1 |
|
new BitbucketTicketMappedEvent($mappedTicket) |
80
|
|
|
); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Combines url with given ticket id. |
85
|
|
|
*/ |
86
|
1 |
|
private function createUrl(int $ticketId) : void |
87
|
|
|
{ |
88
|
1 |
|
$this->url = self::URL_PREFIX . $ticketId . self::URL_SUFFIX; |
89
|
1 |
|
} |
90
|
|
|
} |
91
|
|
|
|