Passed
Pull Request — master (#35)
by
unknown
12:58
created

ManagesWorkItems   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
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 107
rs 10
ccs 12
cts 12
cp 1
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateWorkItem() 0 11 1
A workitem() 0 5 1
A workitems() 0 28 3
A createWorkItem() 0 11 1
1
<?php
2
3
namespace TestMonitor\DevOps\Actions;
4
5
use TestMonitor\DevOps\Builders\WIQL\WIQL;
6
use TestMonitor\DevOps\Resources\WorkItem;
7
use TestMonitor\DevOps\Transforms\TransformsWorkItems;
8
9
trait ManagesWorkItems
10
{
11
    use TransformsWorkItems;
12
13
    /**
14
     * Get a single work item.
15
     *
16
     * @param string $id
17
     * @param string $projectId
18
     *
19
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
20
     *
21
     * @return \TestMonitor\DevOps\Resources\WorkItem
22 4
     */
23
    public function workitem(string $id, string $projectId): WorkItem
24 4
    {
25
        $response = $this->get("{$projectId}/_apis/wit/workitems/{$id}");
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        /** @scrutinizer ignore-call */ 
26
        $response = $this->get("{$projectId}/_apis/wit/workitems/{$id}");
Loading history...
26 1
27
        return $this->fromDevOpsWorkItem($response);
28
    }
29
30
    /**
31
     * Get a list of work items.
32
     *
33
     * @param string $projectId
34
     * @param \TestMonitor\DevOps\Builders\WIQL $query
35
     * @param int $limit
36
     *
37
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
38
     *
39 2
     * @return \TestMonitor\DevOps\Resources\WorkItem[]
40
     */
41 2
    public function workitems(string $projectId, WIQL $query = null, int $limit = 50): array
42 2
    {
43 2
        // Retrieve work items using WIQL
44 2
        $results = $this->post("{$projectId}/_apis/wit/wiql", [
0 ignored issues
show
Bug introduced by
It seems like post() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $results = $this->post("{$projectId}/_apis/wit/wiql", [
Loading history...
45 2
            'json' => [
46 2
                'query' => $query instanceof WIQL ? $query->getQuery() : (new WIQL)->getQuery(),
47 2
                '$top' => $limit,
48
            ],
49 1
        ]);
50
51
        // Return an empty array when there are no results
52
        if (empty($results['workItems'])) {
53
            return [];
54
        }
55
56
        // Gather work item ID's
57
        $ids = array_column($results['workItems'], 'id');
58
59
        // Fetch work items by their ID's
60
        $response = $this->get("{$projectId}/_apis/wit/workitems/", [
61
            'query' => [
62
                'ids' => implode(',', $ids),
63
                'api-version' => $this->apiVersion,
64
                '$expand' => 'Links',
65
            ],
66
        ]);
67
68
        return $this->fromDevOpsWorkItems($response['value']);
69
    }
70
71
    /**
72
     * Create a new work item.
73
     *
74
     * @param \TestMonitor\DevOps\Resources\WorkItem $workItem
75
     * @param string $projectId
76
     *
77
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
78
     *
79
     * @return WorkItem
80
     */
81
    public function createWorkItem(WorkItem $workItem, string $projectId): WorkItem
82
    {
83
        $response = $this->post(
84
            "{$projectId}/_apis/wit/workitems/\${$workItem->workItemType}",
85
            [
86
                'headers' => ['Content-Type' => 'application/json-patch+json'],
87
                'json' => $this->toNewDevOpsWorkItem($workItem),
88
            ]
89
        );
90
91
        return $this->fromDevOpsWorkItem($response);
92
    }
93
94
    /**
95
     * Updates an existing work item.
96
     *
97
     * @param string $id
98
     * @param string $projectId
99
     * @param array $attributes
100
     *
101
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
102
     *
103
     * @return WorkItem
104
     */
105
    public function updateWorkItem(string $id, string $projectId, array $attributes): WorkItem
106
    {
107
        $response = $this->patch(
0 ignored issues
show
Bug introduced by
It seems like patch() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        /** @scrutinizer ignore-call */ 
108
        $response = $this->patch(
Loading history...
108
            "{$projectId}/_apis/wit/workitems/{$id}",
109
            [
110
                'headers' => ['Content-Type' => 'application/json-patch+json'],
111
                'json' => $this->toUpdateDevOpsWorkItem($attributes),
112
            ]
113
        );
114
115
        return $this->fromDevOpsWorkItem($response);
116
    }
117
}
118