Passed
Pull Request — master (#21)
by Thijs
15:18
created

TransformsWorkItems::fromDevOpsWorkItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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