Completed
Push — master ( 5696c1...fbc433 )
by Thijs
26s queued 13s
created

TransformsWorkItems::fromDevOpsWorkItems()   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 0
Metric Value
cc 1
eloc 4
c 0
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\WorkItem;
7
8
trait TransformsWorkItems
9
{
10
    /**
11
     * @param \TestMonitor\DevOps\Resources\WorkItem $workItem
12
     * @return array
13
     */
14 2
    protected function toNewDevOpsWorkItem(WorkItem $workItem): array
15
    {
16 2
        return [
17 2
            [
18 2
                'op' => 'add',
19 2
                'path' => '/fields/System.Title',
20 2
                'from' => null,
21 2
                'value' => $workItem->title,
22 2
            ],
23 2
            [
24 2
                'op' => 'add',
25 2
                'path' => '/fields/System.Description',
26 2
                'from' => null,
27 2
                'value' => $workItem->description,
28 2
            ],
29 2
            [
30 2
                'op' => 'add',
31 2
                'path' => '/fields/Microsoft.VSTS.TCM.ReproSteps',
32 2
                'from' => null,
33 2
                'value' => $workItem->stepsToReproduce,
34 2
            ],
35 2
            ...($workItem->path ? [
36 2
                [
37 2
                    'op' => 'add',
38 2
                    'path' => '/fields/System.AreaPath',
39 2
                    'from' => null,
40 2
                    'value' => $workItem->path,
41 2
                ],
42
            ] : []),
43 2
        ];
44
    }
45
46
    /**
47
     * @param array{
48
     *      title: string,
49
     *      description: string
50
     *      state: string
51
     *      stepsToReproduce: string
52
     *      path: string
53
     * } $attributes
54
     * @return array
55
     */
56 2
    protected function toUpdateDevOpsWorkItem(array $attributes): array
57
    {
58 2
        return [
59 2
            array_filter([
60 2
                ...(isset($attributes['title']) ? [
61 2
                    'op' => 'add',
62 2
                    'path' => '/fields/System.Title',
63 2
                    'value' => $attributes['title'],
64
                ] : []),
65 2
                ...(isset($attributes['description']) ? [
66 2
                    'op' => 'add',
67 2
                    'path' => '/fields/System.Description',
68 2
                    'value' => $attributes['description'],
69
                ] : []),
70 2
                ...(isset($attributes['state']) ? [
71 2
                    'op' => 'add',
72 2
                    'path' => '/fields/System.State',
73 2
                    'value' => $attributes['state'],
74
                ] : []),
75 2
                ...(isset($attributes['stepsToReproduce']) ? [
76 2
                    'op' => 'add',
77 2
                    'path' => '/fields/Microsoft.VSTS.TCM.ReproSteps',
78 2
                    'value' => $attributes['stepsToReproduce'],
79
                ] : []),
80 2
                ...(isset($attributes['path']) ? [
81 2
                    [
82 2
                        'op' => 'add',
83 2
                        'path' => '/fields/System.AreaPath',
84 2
                        'value' => $attributes['path'],
85 2
                    ],
86
                ] : []),
87 2
            ]),
88 2
        ];
89
    }
90
91
    /**
92
     * @param array $workitems
93
     *
94
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
95
     *
96
     * @return \TestMonitor\DevOps\Resources\Team[]
97
     */
98 2
    protected function fromDevOpsWorkItems($workitems): array
99
    {
100 2
        Validator::isArray($workitems);
101
102 2
        return array_map(function ($workItem) {
103 2
            return $this->fromDevOpsWorkItem($workItem);
104 2
        }, $workitems);
105
    }
106
107
    /**
108
     * @param array $workItem
109
     *
110
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
111
     *
112
     * @return \TestMonitor\DevOps\Resources\WorkItem
113
     */
114 5
    protected function fromDevOpsWorkItem(array $workItem): WorkItem
115
    {
116 5
        Validator::keyExists($workItem, 'fields');
117
118 5
        return new WorkItem([
119 5
            'id' => $workItem['id'] ?? '',
120 5
            'title' => $workItem['fields']['System.Title'],
121 5
            'description' => $workItem['fields']['System.Description'] ?? '',
122 5
            'state' => $workItem['fields']['System.State'],
123 5
            'workItemType' => $workItem['fields']['System.WorkItemType'],
124 5
            'stepsToReproduce' => $workItem['fields']['Microsoft.VSTS.TCM.ReproSteps'] ?? '',
125 5
            'path' => $workItem['fields']['System.AreaPath'] ?? '',
126 5
            'url' => $workItem['_links']['html']['href'] ?? '',
127 5
        ]);
128
    }
129
}
130