Fields   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
eloc 35
c 1
b 0
f 1
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findByName() 0 13 5
A findElementId() 0 11 4
A findElementNameById() 0 11 4
A findByProject() 0 11 4
A all() 0 6 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: aappen
5
 * Date: 02.10.18
6
 * Time: 10:55
7
 */
8
9
namespace seretos\testrail\api;
10
11
12
class Fields extends AbstractApi
13
{
14
    private $cache = null;
15
    public function all()
16
    {
17
        if ($this->cache === null) {
18
            $this->cache = $this->connector->send_get('get_case_fields');
19
        }
20
        return $this->cache;
21
    }
22
23
    public function findByProject(int $projectId) {
24
        $fields = $this->all();
25
        $resultFields = [];
26
        foreach ($fields as $field) {
27
            if ($field['configs'][0]['context']['is_global'] === true) {
28
                $resultFields[] = $field;
29
            } else if (in_array($projectId, $field['configs'][0]['context']['project_ids'])) {
30
                $resultFields[] = $field;
31
            }
32
        }
33
        return $resultFields;
34
    }
35
36
    public function findByName(string $name, int $projectId = null) {
37
        if ($projectId === null) {
38
            $fields = $this->all();
39
        } else {
40
            $fields = $this->findByProject($projectId);
41
        }
42
43
        foreach ($fields as $field) {
44
            if ($field['system_name'] === $name || $field['name'] === $name) {
45
                return $field;
46
            }
47
        }
48
        return [];
49
    }
50
51
    public function findElementId(string $field, string $value, int $projectId = null) {
52
        $field = $this->findByName($field, $projectId);
53
        if ($field !== []) {
54
            preg_match_all('/^([0-9]*),\s([A-Za-z0-9 ]*)/m', $field['configs'][0]['options']['items'], $matches, PREG_SET_ORDER, 0);
55
            foreach ($matches as $match) {
56
                if ($match[2] === $value) {
57
                    return (int)$match[1];
58
                }
59
            }
60
        }
61
        return 0;
62
    }
63
64
    public function findElementNameById(string $field, int $id, int $projectId = null) {
65
        $field = $this->findByName($field, $projectId);
66
        if ($field !== []) {
67
            preg_match_all('/^([0-9]*),\s([A-Za-z0-9 ]*)/m', $field['configs'][0]['options']['items'], $matches, PREG_SET_ORDER, 0);
68
            foreach ($matches as $match) {
69
                if ((int)$match[1] === $id) {
70
                    return $match[2];
71
                }
72
            }
73
        }
74
        return 0;
75
    }
76
}