Passed
Pull Request — master (#19)
by Thijs
02:38 queued 46s
created

TransformsWebhooks   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 61
ccs 32
cts 32
cp 1
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 2
    protected function toDevOpsWebhook(Webhook $webhook): array
15
    {
16 2
        return [
17 2
            'publisherId' => 'tfs',
18 2
            'eventType' => $webhook->eventType,
19 2
            'resourceVersion' => '1.0-preview.1',
20 2
            'consumerId' => 'webHooks',
21 2
            'consumerActionId' => 'httpRequest',
22 2
            'actionDescription' => $webhook->description,
23 2
            'publisherInputs' => [
24 2
                'projectId' => $webhook->projectId,
25 2
            ],
26 2
            'consumerInputs' => [
27 2
                'basicAuthUsername' => $webhook->username,
28 2
                'basicAuthPassword' => $webhook->password,
29 2
                'url' => $webhook->url,
30 2
            ],
31 2
        ];
32
    }
33
34
    /**
35
     * @param array $webhooks
36
     *
37
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
38
     *
39
     * @return \TestMonitor\DevOps\Resources\Webhook[]
40
     */
41 1
    protected function fromDevOpsWebhooks($webhooks): array
42
    {
43 1
        Validator::isArray($webhooks);
44
45 1
        return array_map(function ($webhook) {
46 1
            return $this->fromDevOpsWebhook($webhook);
47 1
        }, $webhooks);
48
    }
49
50
    /**
51
     * @param array $webhook
52
     *
53
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
54
     *
55
     * @return \TestMonitor\DevOps\Resources\Webhook
56
     */
57 2
    protected function fromDevOpsWebhook($webhook): Webhook
58
    {
59 2
        Validator::keysExists($webhook, ['id', 'eventType']);
60
61 2
        return new Webhook([
62 2
            'id' => $webhook['id'],
63 2
            'url' => $webhook['consumerInputs']['url'],
64 2
            'description' => $webhook['actionDescription'],
65 2
            'eventType' => $webhook['eventType'],
66 2
            'projectId' => $webhook['publisherInputs']['projectId'] ?? '',
67 2
            'username' => $webhook['consumerInputs']['basicAuthUsername'] ?? '',
68 2
            'password' => $webhook['consumerInputs']['basicAuthPassword'] ?? '',
69 2
        ]);
70
    }
71
}
72