Sections   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 4 1
A create() 0 9 1
A findByName() 0 9 3
A all() 0 6 2
A get() 0 3 1
A findByNameAndParent() 0 9 4
A findByParent() 0 9 6
A delete() 0 3 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: aappen
5
 * Date: 01.10.18
6
 * Time: 16:59
7
 */
8
9
namespace seretos\testrail\api;
10
11
12
class Sections extends AbstractApi
13
{
14
    private $cache = [];
15
    public function all(int $projectId, int $suiteId)
16
    {
17
        if (!isset($this->cache[$projectId][$suiteId])) {
18
            $this->cache[$projectId][$suiteId] = $this->connector->send_get('get_sections/'.$this->encodePath($projectId).'&suite_id='.$this->encodePath($suiteId));
19
        }
20
        return $this->cache[$projectId][$suiteId];
21
    }
22
23
    public function get(int $sectionId)
24
    {
25
        return $this->connector->send_get('get_section/'.$this->encodePath($sectionId));
26
    }
27
28
    public function findByName(int $projectId, int $suiteId, string $name)
29
    {
30
        $allSections = $this->all($projectId, $suiteId);
31
        foreach ($allSections as $section) {
32
            if ($section['name'] === $name) {
33
                return $section;
34
            }
35
        }
36
        return [];
37
    }
38
39
    public function findByNameAndParent(int $projectId, int $suiteId, string $name, int $parentId = null)
40
    {
41
        $allSections = $this->all($projectId, $suiteId);
42
        foreach ($allSections as $section) {
43
            if ($section['name'] === $name && $section['parent_id'] == $parentId) {
44
                return $section;
45
            }
46
        }
47
        return [];
48
    }
49
50
    public function findByParent(int $projectId, int $suiteId, int $parentId = null) {
51
        $allSections = $this->all($projectId, $suiteId);
52
        $sections = [];
53
        foreach ($allSections as $section) {
54
            if ((!isset($section['parent_id']) && $parentId == null) || (isset($section['parent_id']) && $section['parent_id'] == $parentId)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $parentId of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
55
                $sections[] = $section;
56
            }
57
        }
58
        return $sections;
59
    }
60
61
    public function create(int $projectId, int $suiteId, string $name, string $description = null, int $parent_id = null)
62
    {
63
        $section = $this->connector->send_post('add_section/'.$this->encodePath($projectId),
64
            ['name' => $name,
65
                'description' => $description,
66
                'suite_id' => $suiteId,
67
                'parent_id' => $parent_id]);
68
        unset($this->cache[$projectId][$suiteId]);
69
        return $section;
70
    }
71
72
    /**
73
     * @param int $sectionId
74
     * @param array $parameters {
75
     * @var string $name
76
     * @var string $description
77
     * }
78
     * @return mixed
79
     */
80
    public function update(int $sectionId, array $parameters = []) {
81
        $section = $this->connector->send_post('update_section/'.$this->encodePath($sectionId), $parameters);
82
        $this->cache = [];
83
        return $section;
84
    }
85
86
    public function delete(int $sectionId) {
87
        $this->connector->send_post('delete_section/'.$this->encodePath($sectionId), []);
88
        $this->cache = [];
89
    }
90
}