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

ManagesWorkItems::workitem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
     */
23 4
    public function workitem(string $id, string $projectId): WorkItem
24
    {
25 4
        $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
27 1
        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
     * @return \TestMonitor\DevOps\Resources\WorkItem[]
40
     */
41 6
    public function workitems(string $projectId, WIQL $query = null, int $limit = 50): array
42
    {
43
        // Retrieve work items using WIQL
44 6
        $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 6
            'json' => [
46 6
                'query' => $query instanceof WIQL ? $query->getQuery() : (new WIQL)->getQuery(),
47 6
                '$top' => $limit,
48 6
            ],
49 6
        ]);
50
51
        // Return an empty array when there are no results
52 3
        if (empty($results['workItems'])) {
53 1
            return [];
54
        }
55
56
        // Gather work item ID's
57 2
        $ids = array_column($results['workItems'], 'id');
58
59
        // Fetch work items by their ID's
60 2
        $response = $this->get("{$projectId}/_apis/wit/workitems/", [
61 2
            'query' => [
62 2
                'ids' => implode(',', $ids),
63 2
                'api-version' => $this->apiVersion,
64 2
            ],
65 2
        ]);
66
67 2
        return $this->fromDevOpsWorkItems($response['value']);
68
    }
69
70
    /**
71
     * Create a new work item.
72
     *
73
     * @param \TestMonitor\DevOps\Resources\WorkItem $workItem
74
     * @param string $projectId
75
     *
76
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
77
     *
78
     * @return WorkItem
79
     */
80 2
    public function createWorkItem(WorkItem $workItem, string $projectId): WorkItem
81
    {
82 2
        $response = $this->post(
83 2
            "{$projectId}/_apis/wit/workitems/\${$workItem->workItemType}",
84 2
            [
85 2
                'headers' => ['Content-Type' => 'application/json-patch+json'],
86 2
                'json' => $this->toNewDevOpsWorkItem($workItem),
87 2
            ]
88 2
        );
89
90 1
        return $this->fromDevOpsWorkItem($response);
91
    }
92
93
    /**
94
     * Updates an existing work item.
95
     *
96
     * @param string $id
97
     * @param string $projectId
98
     * @param array $attributes
99
     *
100
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
101
     *
102
     * @return WorkItem
103
     */
104 2
    public function updateWorkItem(string $id, string $projectId, array $attributes): WorkItem
105
    {
106 2
        $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

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