Passed
Pull Request — master (#23)
by Thijs
02:34 queued 51s
created

ManagesStates::states()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 1
rs 10
c 2
b 1
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 6
    public function states($projectId)
21
    {
22 6
        $process = $this->findCurrentProjectProcess($projectId);
23
24 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

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