Completed
Pull Request — FVSv2 (#46)
by Patrick
02:51
created

dept_api.php ➔ getShiftsForDepartment()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 18
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
function deptGroup()
4
{
5
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
6
    $app->get('(/)', 'getDepartmentList');
7
    $app->post('(/)', 'createDepartment');
8
    $app->get('/:id(/)', 'getDepartment');
9
    $app->get('/:id/shifts(/)', 'getShiftsForDepartment');
10
    $app->get('/:id/roles(/)', 'getRolesForDepartment');
11
}
12
13
function getDepartmentList()
14
{
15
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
16
    if(!$app->user)
17
    {
18
        throw new Exception('Must be logged in', ACCESS_DENIED);
19
    }
20
    $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
21
    $ret = false;
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
    if($app->user->isInGroupNamed('VolunteerAdmins'))
23
    {
24
        if($app->odata->filter !== false && $app->odata->filter->contains('lead eq me'))
25
        {
26
            $app->odata->filter = false;
27
        }
28
        $ret = $dataTable->read($app->odata->filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
29
    }
30
    else
31
    {
32
        if($app->user->isInGroupNamed('Lead'))
33
        {
34
            $filter = false;
0 ignored issues
show
Unused Code introduced by
$filter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
            if($app->odata->filter !== false && $app->odata->filter->contains('lead eq me'))
36
            {
37
                $filter = new \Data\Filter('lead eq '.$app->getUID());
38
            }
39
            else
40
            {
41
                $filter = new \Data\Filter('lead eq '.$app->getUID().' or public eq true');
42
            }
43
            $ret = $dataTable->read($filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
44
        }
45
        else
46
        {
47
            $filter = new \Data\Filter('public eq true');
48
            $ret = $dataTable->read($filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
49
        }
50
    }
51
    echo json_encode($ret);
52
}
53
54
function createDepartment()
55
{
56
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
57
    if(!$app->user)
58
    {
59
        throw new Exception('Must be logged in', ACCESS_DENIED);
60
    }
61
    else if(!$app->user->isInGroupNamed('VolunteerAdmins'))
62
    {
63
        throw new Exception('Must be a volunteer admin to create a department', ACCESS_DENIED);
64
    }
65
    $obj = $app->getJSONBody();
66
    $dataSet = DataSetFactory::get_data_set('fvs');
0 ignored issues
show
Bug introduced by
The method get_data_set() does not seem to exist on object<DataSetFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
    $dataTable = $dataSet['departments'];
68
    echo json_encode($dataTable->create($obj));
69
}
70
71
function getDepartmentByID($id, $select = false)
72
{
73
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
74
    if(!$app->user)
75
    {
76
        throw new Exception('Must be logged in', ACCESS_DENIED);
77
    }
78
    $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
79
    $filter = new \Data\Filter("departmentID eq '$id'");
80
    $ret = $dataTable->read($filter, $select);
81
    if(empty($ret))
82
    {
83
        return false;
84
    }
85
    if($ret[0]['public'])
86
    {
87
        return $ret[0];
88
    }
89
    if($app->user->isInGroupNamed('VolunteerAdmins'))
90
    {
91
        return $ret[0];
92
    }
93
    //TODO Give lead access to department
94
    return false;
95
}
96
97
function getDepartment($id)
98
{
99
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
100
    $ret = getDepartmentByID($id, $app->odata->select);
101
    if($ret === false)
102
    {
103
        $app->notFound();
104
    }
105
    echo json_encode($ret);
106
}
107
108 View Code Duplication
function getShiftsForDepartment($id)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
{
110
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
111
    $dept = getDepartmentByID($id);
112
    if($dept === false)
113
    {
114
        $app->notFound();
115
    }
116
    $filter = new \Data\Filter('departmentID eq '.$dept['departmentID']);
117
    $dataSet = DataSetFactory::get_data_set('fvs');
0 ignored issues
show
Bug introduced by
The method get_data_set() does not seem to exist on object<DataSetFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
    $dataTable = $dataSet['shifts'];
119
    $ret = $dataTable->read($filter, $app->odata->select);
120
    if($ret === false || !isset($ret[0]))
121
    {
122
        $ret = array();
123
    }
124
    echo json_encode($ret);
125
}
126
127 View Code Duplication
function getRolesForDepartment($id)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
{
129
    global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
130
    $dept = getDepartmentByID($id);
131
    if($dept === false)
132
    {
133
        $app->notFound();
134
    }
135
    $filter = new \Data\Filter('departmentID eq '.$dept['departmentID']);
136
    $dataSet = DataSetFactory::get_data_set('fvs');
0 ignored issues
show
Bug introduced by
The method get_data_set() does not seem to exist on object<DataSetFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
    $dataTable = $dataSet['roles'];
138
    $ret = $dataTable->read($filter, $app->odata->select);
139
    if($ret === false || !isset($ret[0]))
140
    {
141
        $ret = array();
142
    }
143
    echo json_encode($ret);
144
}
145
146