Completed
Push — FVSv2 ( 94c8ac...9d1e1c )
by Patrick
05:29 queued 18s
created

Processor::processShift()   C

Complexity

Conditions 13
Paths 123

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 123
nop 1
dl 0
loc 66
rs 5.8818
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 doShiftTimeChecks($shift, $entry)
174
    {
175
        $now = new DateTime();
176
        if($shift->startTime < $now)
177
        {
178
            $entry['available'] = false;
179
            $entry['why'] = 'Shift already started';
180
        }
181
        if($shift->endTime < $now)
182
        {
183
            $entry['available'] = false;
184
            $entry['why'] = 'Shift already ended';
185
        }
186
    }
187
188
    protected function processShift($entry)
189
    {
190
        static $profile = null;
191
        static $eeAvailable = false;
192
        static $canDoRole = array();
193
        static $roles = array();
194
        if($profile === null)
195
        {
196
            $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...
197
            if(isset($profile['firstName']) && isset($profile['lastName']))
198
            {
199
                $eeAvailable = true;
200
            }
201
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
202
            $tmp = $dataTable->read();
203
            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...
204
            {
205
               $roles[$role['short_name']] = $role;
206
            }
207
        }
208
        $shift = new \VolunteerShift(false, $entry);
209
        $entry['isAdmin'] = $this->isAdminForShift($entry, $this->user);
210
        $entry['overlap'] = $shift->findOverlaps($this->user->uid, true);
211
        if(!$this->shouldShowDepartment($entry['departmentID'], $entry['isAdmin']))
212
        {
213
            return null;
214
        }
215
        $entry['available'] = true;
216
        $this->doShiftTimeChecks($shift, $entry);
217
        if($entry['earlyLate'] != -1 && !$eeAvailable)
218
        {
219
            $entry['available'] = false;
220
            $entry['why'] = 'Shift requires early entry or late stay and you have not provided your legal name';
221
        }
222
        if(!isset($canDoRole[$entry['roleID']]))
223
        {
224
            $canDoRole[$entry['roleID']] = $this->canUserDoRole($profile, $roles[$entry['roleID']]);
225
        }
226
        if($canDoRole[$entry['roleID']] !== true)
227
        {
228
            $entry['available'] = false;
229
            $entry['why'] = $canDoRole[$entry['roleID']]['whyMsg'];
230
            $entry['whyClass'] = $canDoRole[$entry['roleID']]['whyClass'];
231
        }
232
        if($shift->isFilled())
233
        {
234
            $entry['volunteer'] = $this->getParticipantDiplayName($entry['participant']);
235
            if($entry['participant'] === $profile['uid'])
236
            {
237
                $entry['available'] = false;
238
                $entry['why'] = 'Shift is already taken, by you';
239
                $entry['whyClass'] = 'MINE';
240
            }
241
            else
242
            {
243
                $entry['available'] = false;
244
                $entry['why'] = 'Shift is already taken';
245
                $entry['whyClass'] = 'TAKEN';
246
            }
247
            if(!$entry['isAdmin'])
248
            {
249
                unset($entry['participant']);
250
            }
251
        }
252
        return $entry;
253
    }
254
255
    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...
256
    {
257
        $entry['isAdmin'] = $this->isAdminForRole($entry, $this->user);
258
        return $entry;
259
    }
260
}
261
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
262