Passed
Pull Request — master (#19)
by Thijs
11:57
created

TransformsWebhooks::fromDevOpsWebhooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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