Passed
Pull Request — master (#23)
by Thijs
05:42
created

ManagesStates::statesForWorkItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TestMonitor\DevOps\Actions;
4
5
use TestMonitor\DevOps\Support\Arrays;
6
use TestMonitor\DevOps\Transforms\TransformsStates;
7
8
trait ManagesStates
9
{
10
    use TransformsStates;
11
12
    /**
13
     * Get a list of states for a project.
14
     *
15
     * @param string $projectId
16
     * @param string $workItemType
17
     *
18
     * @return \TestMonitor\DevOps\Resources\States[]
19
     *
20
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
21 7
     */
22
    public function states($projectId)
23 7
    {
24
        $process = $this->findCurrentProjectProcess($projectId);
25 1
26
        $response = $this->get("_apis/work/processes/{$process}/workitemtypes", [
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

26
        /** @scrutinizer ignore-call */ 
27
        $response = $this->get("_apis/work/processes/{$process}/workitemtypes", [
Loading history...
27
            'query' => ['$expand' => 'states'],
28
        ]);
29
30
        $states = Arrays::unique(
31
            Arrays::flatten(
32
                array_column($response['value'], 'states')
33
            ), 'id'
34
        );
35
36
        return $this->fromDevOpsStates($states);
37
    }
38
39
    /**
40
     * Determine the process ID for the provided project.
41
     *
42
     * @param string $projectId
43
     *
44
     * @return string
45
     */
46
    protected function findCurrentProjectProcess($projectId)
47
    {
48
        $response = $this->get("_apis/projects/{$projectId}/properties", [
49
            'query' => ['api-version' => '5.0-preview.1'],
50
        ]);
51
52
        $property = current(
53
            array_filter($response['value'], fn ($property) => $property['name'] === 'System.CurrentProcessTemplateId'),
54
        );
55
56
        return $property['value'];
57
    }
58
59
    /**
60
     * Get a list of states for a project and work item type.
61
     *
62
     * @param string $projectId
63
     * @param string $workItemType
64
     *
65
     * @return \TestMonitor\DevOps\Resources\States[]
66
     */
67
    public function statesForWorkItem($projectId, $workItemType)
68
    {
69
        $response = $this->get("{$projectId}/_apis/wit/workitemtypes/{$workItemType}/states");
70
71
        return $this->fromDevOpsStates($response['value']);
72
    }
73
}
74