Completed
Push — FVSv2 ( 9a40f1...ab906d )
by Patrick
01:31
created

Processor::shouldShowDepartment()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
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 View Code Duplication
    protected function isUserDepartmentLead($departmentID, $user)
0 ignored issues
show
Duplication introduced by
This method 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...
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 shouldShowDepartment($deptId, $isAdmin)
148
    {
149
        static $privateDepts = null;
150
        if($privateDepts === null)
151
        {
152
            $privateDepts = VolunteerDepartment::getPrivateDepartments();
153
        }
154
        if($isAdmin)
155
        {
156
            return true;
157
        }
158
        return !in_array($deptId, $privateDepts);
159
    }
160
161 View Code Duplication
    protected function getParticipantProfile($uid)
0 ignored issues
show
Duplication introduced by
This method 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...
162
    {
163
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'participants');
164
        $filter = new \Data\Filter("uid eq '$uid'");
165
        $profile = $dataTable->read($filter);
166
        if(empty($profile))
167
        {
168
            return null;
169
        }
170
        return $profile[0];
171
    }
172
173
    protected function processShift($entry)
174
    {
175
        static $profile = null;
176
        static $eeAvailable = false;
177
        static $canDoRole = array();
178
        static $roles = array();
179
        if($profile === null)
180
        {
181
            $profile = $this->getParticipantProfile($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...
182
            if(isset($profile['firstName']) && isset($profile['lastName']))
183
            {
184
                $eeAvailable = true;
185
            }
186
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
187
            $tmp = $dataTable->read();
188
            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...
189
            {
190
               $roles[$role['short_name']] = $role;
191
            }
192
        }
193
        $shift = new \VolunteerShift(false, $entry);
194
        $entry['isAdmin'] = $this->isAdminForShift($entry, $this->user);
195
        $entry['overlap'] = $shift->findOverlaps($this->user->uid, true);
196
        if(!$this->shouldShowDepartment($entry['departmentID'], $entry['isAdmin']))
197
        {
198
            return null;
199
        }
200
        $entry['available'] = true;
201
        $endTime = new DateTime($entry['endTime']);
202
        $startTime = new DateTime($entry['startTime']);
203
        $now = new DateTime();
204
        if($startTime < $now)
205
        {
206
            $entry['available'] = false;
207
            $entry['why'] = 'Shift already started';
208
        }
209
        if($endTime < $now)
210
        {
211
            $entry['available'] = false;
212
            $entry['why'] = 'Shift already ended';
213
        }
214
        if($entry['earlyLate'] != -1 && !$eeAvailable)
215
        {
216
            $entry['available'] = false;
217
            $entry['why'] = 'Shift requires early entry or late stay and you have not provided your legal name';
218
        }
219
        if(!isset($canDoRole[$entry['roleID']]))
220
        {
221
            $canDoRole[$entry['roleID']] = $this->canUserDoRole($profile, $roles[$entry['roleID']]);
222
        }
223
        if($canDoRole[$entry['roleID']] !== true)
224
        {
225
            $entry['available'] = false;
226
            $entry['why'] = $canDoRole[$entry['roleID']]['whyMsg'];
227
            $entry['whyClass'] = $canDoRole[$entry['roleID']]['whyClass'];
228
        }
229
        if($shift->isFilled())
230
        {
231
            $entry['volunteer'] = $this->getParticipantDiplayName($entry['participant']);
232
            if($entry['participant'] === $profile['uid'])
233
            {
234
                $entry['available'] = false;
235
                $entry['why'] = 'Shift is already taken, by you';
236
                $entry['whyClass'] = 'MINE';
237
            }
238
            else
239
            {
240
                $entry['available'] = false;
241
                $entry['why'] = 'Shift is already taken';
242
                $entry['whyClass'] = 'TAKEN';
243
            }
244
            if(!$entry['isAdmin'])
245
            {
246
                unset($entry['participant']);
247
            }
248
        }
249
        return $entry;
250
    }
251
252
    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...
253
    {
254
        $entry['isAdmin'] = $this->isAdminForRole($entry, $this->user);
255
        return $entry;
256
    }
257
}
258
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
259