Completed
Push — FVSv2 ( 65de7f...ae4b85 )
by Patrick
01:34
created

Processor::findOverlaps()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 2
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Processor::isAdminForRole() 0 5 1
1
<?php
2
trait Processor
3
{
4
    protected function certCheck($requirements, $certs, $certType)
5
    {
6
        if(isset($requirements[$certType]) && $requirements[$certType])
7
        {
8
            return (!isset($certs[$certType]) || !$certs[$certType]);
9
        }
10
        return false;
11
    }
12
13
    public function canUserDoRole($user, $role)
14
    {
15
        if($role['publicly_visible'] === true)
16
        {
17
            return true;
18
        }
19
        $requirements = array();
20
        if(isset($role['requirements']))
21
        {
22
            $requirements = $role['requirements'];
23
        }
24
        $certs = array();
25
        if(isset($user['certs']))
26
        {
27
            $certs = $user['certs'];
28
        }
29
        if(count($requirements) === 0)
30
        {
31
            //Bugged role...
32
            return true;
33
        }
34
        if(isset($requirements['email_list']))
35
        {
36
            $emails = explode(',', $requirements['email_list']);
37
            if(isset($user['email']) && in_array($user['email'], $emails))
38
            {
39
                return true;
40
            }
41
            return array('whyClass' => 'INVITE', 'whyMsg' => 'Shift is invite only.');
42
        }
43
        if($this->certCheck($requirements, $certs, 'ics100'))
44
        {
45
            return array('whyClass' => 'CERT', 'whyMsg' => 'Shift requires ICS 100 and you do not have that certification');
46
        }
47
        if($this->certCheck($requirements, $certs, 'ics200'))
48
        {
49
            return array('whyClass' => 'CERT', 'whyMsg' => 'Shift requires ICS 200 and you do not have that certification');
50
        }
51
        if($this->certCheck($requirements, $certs, 'bls'))
52
        {
53
            return array('whyClass' => 'CERT', 'whyMsg' => 'Shift requires Basic Life Support certification and you do not have that certification');
54
        }
55
        return true;
56
    }
57
58
    protected function getParticipantDiplayName($uid)
59
    {
60
        static $uids = array();
61
        static $dataTable = null;
62
        if($dataTable === null)
63
        {
64
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'participants');
65
        }
66
        if(!isset($uids[$uid]))
67
        {
68
            $filter = new \Data\Filter("uid eq '$uid'");
69
            $profile = $dataTable->read($filter);
70
            if(empty($profile))
71
            {
72
                $uids[$uid] = $uid;
73
                return $uid;
74
            }
75
            $profile = $profile[0];
76
            switch($profile['webName'])
77
            {
78
                case 'anonymous':
79
                    $uids[$uid] = 'Anonymous';
80
                    break;
81
                case 'full':
82
                    $uids[$uid] = $profile['firstName'].' "'.$profile['burnerName'].'" '.$profile['lastName'];
83
                    break;
84
                case 'burnerLast':
85
                    $uids[$uid] = $profile['burnerName'].' '.$profile['lastName'];
86
                    break;
87
                case 'firstBurner':
88
                    $uids[$uid] = $profile['firstName'].' '.$profile['burnerName'];
89
                    break;
90
                case 'burner':
91
                    $uids[$uid] = $profile['burnerName'];
92
                    break;
93
            }
94
        }
95
        return $uids[$uid];
96
    }
97
98
    protected function isUserDepartmentLead($departmentID, $user)
99
    {
100
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
101
        $filter = new \Data\Filter('departmentID eq '.$departmentID);
102
        $depts = $dataTable->read($filter);
103
        if(empty($depts))
104
        {
105
            return false;
106
        }
107
        return $this->isUserDepartmentLead2($depts[0], $user);
108
    }
109
110
    protected function isUserDepartmentLead2($dept, $user)
111
    {
112
        if($user->isInGroupNamed('Leads'))
113
        {
114
            if(in_array($dept['lead'], $user->title))
115
            {
116
                return true;
117
            }
118
        }
119
        if(!isset($dept['others']))
120
        {
121
            return false;
122
        }
123
        $email = $user->mail;
124
        $otherAdmins = explode(',', $dept['others']);
125
        return in_array($email, $otherAdmins);
126
    }
127
128
    public function isAdminForShift($shift, $user)
129
    {
130
        if($this->isAdmin)
1 ignored issue
show
Bug introduced by
The property isAdmin does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
131
        {
132
            return true;
133
        }
134
        if($this->isUserDepartmentLead($shift['departmentID'], $user))
135
        {
136
            return true;
137
        }
138
        return false;
139
    }
140
141
    public function isAdminForRole($role, $user)
142
    {
143
        //Shift and Role use the same key for department ID...
144
        return $this->isAdminForShift($role, $user);
145
    }
146
147
    protected function processShift($entry, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        static $profile = null;
150
        static $eeAvailable = false;
151
        static $privateDepts = array();
152
        static $canDoRole = array();
153
        static $roles = array();
154
        if($profile === null)
155
        {
156
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'participants');
157
            $uid = $this->user->uid;
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
158
            $filter = new \Data\Filter("uid eq '$uid'");
159
            $profile = $dataTable->read($filter);
160
            if(empty($profile) && !$this->isAdmin)
161
            {
162
                return null;
163
            }
164
            $profile = $profile[0];
165
            if(isset($profile['firstName']) && isset($profile['lastName']))
166
            {
167
                $eeAvailable = true;
168
            }
169
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
170
            $filter = new \Data\Filter('public eq false');
171
            $depts = $dataTable->read($filter);
172
            foreach($depts as $dept)
0 ignored issues
show
Bug introduced by
The expression $depts of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
173
            {
174
                array_push($privateDepts, $dept['departmentID']);
175
            }
176
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
177
            $tmp = $dataTable->read();
178
            foreach($tmp as $role)
0 ignored issues
show
Bug introduced by
The expression $tmp of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
179
            {
180
               $roles[$role['short_name']] = $role;
181
            }
182
        }
183
        $shift = new \VolunteerShift(false, $entry);
184
        $entry['isAdmin'] = $this->isAdminForShift($entry, $this->user);
185
        $entry['overlap'] = $shift->findOverlaps($this->user->uid, true);
186
        if(in_array($entry['departmentID'], $privateDepts) && !$entry['isAdmin'])
187
        {
188
            return null;
189
        }
190
        $entry['available'] = true;
191
        $endTime = new DateTime($entry['endTime']);
192
        $startTime = new DateTime($entry['startTime']);
193
        $now = new DateTime();
194
        if($startTime < $now)
195
        {
196
            $entry['available'] = false;
197
            $entry['why'] = 'Shift already started';
198
        }
199
        if($endTime < $now)
200
        {
201
            $entry['available'] = false;
202
            $entry['why'] = 'Shift already ended';
203
        }
204
        if($entry['earlyLate'] != -1 && !$eeAvailable)
205
        {
206
            $entry['available'] = false;
207
            $entry['why'] = 'Shift requires early entry or late stay and you have not provided your legal name';
208
        }
209
        if(!isset($canDoRole[$entry['roleID']]))
210
        {
211
            $canDoRole[$entry['roleID']] = $this->canUserDoRole($profile, $roles[$entry['roleID']]);
212
        }
213
        if($canDoRole[$entry['roleID']] !== true)
214
        {
215
            $entry['available'] = false;
216
            $entry['why'] = $canDoRole[$entry['roleID']]['whyMsg'];
217
            $entry['whyClass'] = $canDoRole[$entry['roleID']]['whyClass'];
218
        }
219
        if(isset($entry['status']) && ($entry['status'] === 'pending' || $entry['status'] === 'filled'))
220
        {
221
            $entry['volunteer'] = $this->getParticipantDiplayName($entry['participant']);
222
            if($entry['participant'] === $profile['uid'])
223
            {
224
                $entry['available'] = false;
225
                $entry['why'] = 'Shift is already taken, by you';
226
                $entry['whyClass'] = 'MINE';
227
            }
228
            else
229
            {
230
                $entry['available'] = false;
231
                $entry['why'] = 'Shift is already taken';
232
                $entry['whyClass'] = 'TAKEN';
233
            }
234
            if(!$entry['isAdmin'])
235
            {
236
                unset($entry['participant']);
237
            }
238
        }
239
        return $entry;
240
    }
241
242
    protected function processRole($entry, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
243
    {
244
        $entry['isAdmin'] = $this->isAdminForRole($entry, $this->user);
245
        return $entry;
246
    }
247
}
248
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
249