Completed
Pull Request — master (#48)
by Patrick
02:22
created

participant_api.php ➔ getMe()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 3
nop 0
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
function participantGroup()
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('(/)', 'getParticipants');
7
    $app->get('/me(/)', 'getMe');
8
    $app->get('/:uid(/)', 'getUid');
9
}
10
11
function getParticipants()
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
    $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...
19
    $dataTable = $dataSet['participants'];
20
    $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...
21
    if($app->user->isInGroupNamed('VolunteerAdmins'))
22
    {
23
        $ret = $dataTable->read($app->odata->filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
24
    }
25
    else
26
    {
27
        $myDept = false;
28
        if($app->user->isInGroupNamed('Lead'))
29
        {
30
            $myDept = \Volunteer\Participants::getParticipantsForLead($app->user, $app->odata->select);
31
        }
32
        $me = $dataTable->read(new \Data\Filter('uid eq '.$app->user->getUid()), $app->odata->select);
33
        if($myDept)
34
        {
35
            $depts = array_merge($me, $myDept);
36
        }
37
        $ret = $depts;
0 ignored issues
show
Bug introduced by
The variable $depts does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
38
    }
39
    echo json_encode($ret);
40
}
41
42
function getMe()
43
{
44
    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...
45
    if(!$app->user)
46
    {
47
        throw new Exception('Must be logged in', ACCESS_DENIED);
48
    }
49
    $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...
50
    $dataTable = $dataSet['participants'];
51
    $user = $dataTable->read(new \Data\Filter('uid eq '.$app->user->getUid()), $app->odata->select);
52
    if($user === false || !isset($user[0]))
53
    {
54
        //Get user from profiles.burningflipside.com
55
        $userStr = file_get_contents('https://profiles.burningflipside.com/api/v1/users/'.$app->user->getUid());
56
        $userJson = json_decode($userStr, true);
57
        $user = array();
58
        $user['displayName'] = $userJson['displayName'];
59
        $user['givenName'] = $userJson['givenName'];
60
        $user['mail'] = $userJson['mail'];
61
        $user['uid'] = $userJson['uid'];
62
        $user['sn'] = $userJson['sn'];
63
        $dataTable->create($user);
64
    }
65
    echo json_encode($user);
66
}
67
68
function getUid($uid)
69
{
70
    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...
71
    if(!$app->user)
72
    {
73
        throw new Exception('Must be logged in', ACCESS_DENIED);
74
    }
75
    $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...
76
    $dataTable = $dataSet['participants'];
77
    $user = false;
78
    //TODO add check for Lead...
79
    if($app->user->isInGroupNamed('VolunteerAdmins') || $uid === $app->user->getUid())
80
    {
81
        $user = $dataTable->read(new \Data\Filter('uid eq '.$uid), $app->odata->select);
82
    }
83
    if($user === false || !isset($user[0]))
84
    {
85
        $app->notFound();
86
    }
87
    echo json_encode($user[0]);
88
}
89
90
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
91