Completed
Push — master ( 38803a...a6786f )
by Thijs
29s queued 13s
created

TransformsWebhooks::fromDevOpsWebhooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

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
ccs 5
cts 5
cp 1
crap 1
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
     * @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