Completed
Branch FVSv2 (febeab)
by Patrick
03:27
created

dept_api.php ➔ deptGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
}
10
11
function getDepartmentList()
12
{
13
    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...
14
    if(!$app->user)
15
    {
16
        throw new Exception('Must be logged in', ACCESS_DENIED);
17
    }
18
    $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
0 ignored issues
show
Bug introduced by
The method getDataTableByNames() 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...
19
    $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...
20
    if($app->user->isInGroupNamed('VolunteerAdmins'))
21
    {
22
        if($app->odata->filter !== false && $app->odata->filter->contains('lead eq me'))
23
        {
24
            $app->odata->filter = false;
25
        }
26
        $ret = $dataTable->read($app->odata->filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
27
    }
28
    else
29
    {
30
        if($app->user->isInGroupNamed('Lead'))
31
        {
32
            $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...
33
            if($app->odata->filter !== false && $app->odata->filter->contains('lead eq me'))
34
            {
35
                $filter = new \Data\Filter('lead eq '.$app->getUID());
36
            }
37
            else
38
            {
39
                $filter = new \Data\Filter('lead eq '.$app->getUID().' or public eq true');
40
            }
41
            $ret = $dataTable->read($filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
42
        }
43
        else
44
        {
45
            $filter = new \Data\Filter('public eq true');
46
            $ret = $dataTable->read($filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
47
        }
48
    }
49
    echo json_encode($ret);
50
}
51
52
function createDepartment()
53
{
54
    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...
55
    if(!$app->user)
56
    {
57
        throw new Exception('Must be logged in', ACCESS_DENIED);
58
    }
59
    else if(!$app->user->isInGroupNamed('VolunteerAdmins'))
60
    {
61
        throw new Exception('Must be a volunteer admin to create a department', ACCESS_DENIED);
62
    }
63
    $obj = $app->getJSONBody();
64
    $dataSet = DataSetFactory::get_data_set('fvs');
0 ignored issues
show
Deprecated Code introduced by
The method DataSetFactory::get_data_set() has been deprecated with message: 2.0.0 Utilize the getDataSetByName() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
    $dataTable = $dataSet['departments'];
66
    echo json_encode($dataTable->create($obj));
67
}
68
69
function getDepartment($id)
70
{
71
    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...
72
    if(!$app->user)
73
    {
74
        throw new Exception('Must be logged in', ACCESS_DENIED);
75
    }
76
    $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
0 ignored issues
show
Bug introduced by
The method getDataTableByNames() 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...
77
    $filter = new \Data\Filter("departmentID eq '$id'");
78
    $ret = $dataTable->read($filter, $app->odata->select);
79
    if($ret === false || !isset($ret[0]))
80
    {
81
        $app->notFound();
82
    }
83
    echo json_encode($ret[0]);
84
}
85
86