Completed
Push — master ( 2f45a4...5d8b59 )
by Thijs
15s queued 14s
created

ManagesStates::findCurrentProjectProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 1
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
     * @return \TestMonitor\DevOps\Resources\States[]
18
     */
19 6
    public function states($projectId)
20
    {
21 6
        $process = $this->findCurrentProjectProcess($projectId);
22
23 1
        $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

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