ManagesWorkItems::updateWorkItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 11
ccs 9
cts 9
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
                '$expand' => 'Links',
65 2
            ],
66 2
        ]);
67
68 2
        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 2
    public function createWorkItem(WorkItem $workItem, string $projectId): WorkItem
82
    {
83 2
        $response = $this->post(
84 2
            "{$projectId}/_apis/wit/workitems/\${$workItem->workItemType}",
85 2
            [
86 2
                'headers' => ['Content-Type' => 'application/json-patch+json'],
87 2
                'json' => $this->toNewDevOpsWorkItem($workItem),
88 2
            ]
89 2
        );
90
91 1
        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 2
    public function updateWorkItem(string $id, string $projectId, array $attributes): WorkItem
106
    {
107 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

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