Passed
Pull Request — master (#23)
by Thijs
05:12 queued 03:28
created

ManagesStates   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 17
dl 0
loc 62
ccs 23
cts 23
cp 1
rs 10
c 2
b 1
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findCurrentProjectProcess() 0 11 1
A states() 0 16 1
A statesForWorkItem() 0 5 1
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
     * @throws \TestMonitor\DevOps\Exceptions\InvalidDataException
18
     * @return \TestMonitor\DevOps\Resources\States[]
19
     *
20
     */
21 6
    public function states($projectId)
22
    {
23 6
        $process = $this->findCurrentProjectProcess($projectId);
24
25 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

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