Passed
Pull Request — master (#35)
by
unknown
12:58
created

TransformsWebhooks   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDevOpsWebhooks() 0 7 1
A fromDevOpsWebhook() 0 12 1
A toDevOpsWebhook() 0 16 1
1
<?php
2
3
namespace TestMonitor\DevOps\Transforms;
4
5
use TestMonitor\DevOps\Validator;
6
use TestMonitor\DevOps\Resources\Webhook;
7
8
trait TransformsWebhooks
9
{
10
    /**
11
     * @param \TestMonitor\DevOps\Resources\Webhook $webhook
12
     * @return array
13
     */
14
    protected function toDevOpsWebhook(Webhook $webhook): array
15
    {
16
        return [
17
            'publisherId' => 'tfs',
18
            'eventType' => $webhook->eventType,
19
            'resourceVersion' => '1.0-preview.1',
20
            'consumerId' => 'webHooks',
21
            'consumerActionId' => 'httpRequest',
22
            'actionDescription' => $webhook->description,
23
            'publisherInputs' => [
24
                'projectId' => $webhook->projectId,
25
            ],
26
            'consumerInputs' => [
27
                'basicAuthUsername' => $webhook->username,
28
                'basicAuthPassword' => $webhook->password,
29
                'url' => $webhook->url,
30
            ],
31
        ];
32
    }
33
34
    /**
35
     * @param array $webhooks
36
     *
37
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
38
     *
39
     * @return \TestMonitor\DevOps\Resources\Webhook[]
40
     */
41
    protected function fromDevOpsWebhooks($webhooks): array
42
    {
43
        Validator::isArray($webhooks);
44
45
        return array_map(function ($webhook) {
46
            return $this->fromDevOpsWebhook($webhook);
47
        }, $webhooks);
48
    }
49
50
    /**
51
     * @param array $webhook
52
     *
53
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
54
     *
55
     * @return \TestMonitor\DevOps\Resources\Webhook
56
     */
57
    protected function fromDevOpsWebhook($webhook): Webhook
58
    {
59
        Validator::keysExists($webhook, ['id', 'eventType']);
60
61
        return new Webhook([
62
            'id' => $webhook['id'],
63
            'url' => $webhook['consumerInputs']['url'],
64
            'description' => $webhook['actionDescription'],
65
            'eventType' => $webhook['eventType'],
66
            'projectId' => $webhook['publisherInputs']['projectId'] ?? '',
67
            'username' => $webhook['consumerInputs']['basicAuthUsername'] ?? '',
68
            'password' => $webhook['consumerInputs']['basicAuthPassword'] ?? '',
69
        ]);
70
    }
71
}
72