Completed
Push — FVSv2 ( 9d1e1c...673865 )
by Patrick
06:55 queued 38s
created

Processor::processShift()   C

Complexity

Conditions 11
Paths 82

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 82
nop 1
dl 0
loc 63
rs 6.6606
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 = $user->certs;
25
        if(count($requirements) === 0)
26
        {
27
            //Bugged role...
28
            return true;
29
        }
30
        if(isset($requirements['email_list']))
31
        {
32
            $emails = explode(',', $requirements['email_list']);
33
            if(!$user->userInEmailList($emails))
34
            {
35
                return array('whyClass' => 'INVITE', 'whyMsg' => 'Shift is invite only.');
36
            }
37
        }
38
        if($this->certCheck($requirements, $certs, 'ics100'))
39
        {
40
            return array('whyClass' => 'CERT', 'whyMsg' => 'Shift requires ICS 100 and you do not have that certification');
41
        }
42
        if($this->certCheck($requirements, $certs, 'ics200'))
43
        {
44
            return array('whyClass' => 'CERT', 'whyMsg' => 'Shift requires ICS 200 and you do not have that certification');
45
        }
46
        if($this->certCheck($requirements, $certs, 'bls'))
47
        {
48
            return array('whyClass' => 'CERT', 'whyMsg' => 'Shift requires Basic Life Support certification and you do not have that certification');
49
        }
50
        return true;
51
    }
52
53
    protected function getParticipantDiplayName($uid)
54
    {
55
        static $uids = array();
56
        if(!isset($uids[$uid]))
57
        {
58
            $profile = new \VolunteerProfile($uid);
59
            $uids[$uid] = $profile->getDisplayName();
60
        }
61
        return $uids[$uid];
62
    }
63
64
    protected function isUserDepartmentLead($departmentID, $user)
65
    {
66
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
67
        $filter = new \Data\Filter('departmentID eq '.$departmentID);
68
        $depts = $dataTable->read($filter);
69
        if(empty($depts))
70
        {
71
            return false;
72
        }
73
        return $this->isUserDepartmentLead2($depts[0], $user);
74
    }
75
76
    protected function isUserDepartmentLead2($dept, $user)
77
    {
78
        if($user->isInGroupNamed('Leads'))
79
        {
80
            if(in_array($dept['lead'], $user->title))
81
            {
82
                return true;
83
            }
84
        }
85
        if(!isset($dept['others']))
86
        {
87
            return false;
88
        }
89
        $email = $user->mail;
90
        $otherAdmins = explode(',', $dept['others']);
91
        return in_array($email, $otherAdmins);
92
    }
93
94
    public function isAdminForShift($shift, $user)
95
    {
96
        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...
97
        {
98
            return true;
99
        }
100
        if($this->isUserDepartmentLead($shift['departmentID'], $user))
101
        {
102
            return true;
103
        }
104
        return false;
105
    }
106
107
    public function isAdminForRole($role, $user)
108
    {
109
        //Shift and Role use the same key for department ID...
110
        return $this->isAdminForShift($role, $user);
111
    }
112
113
    protected function shouldShowDepartment($deptId, $isAdmin)
114
    {
115
        static $privateDepts = null;
116
        if($privateDepts === null)
117
        {
118
            $privateDepts = VolunteerDepartment::getPrivateDepartments();
119
        }
120
        if($isAdmin)
121
        {
122
            return true;
123
        }
124
        return !in_array($deptId, $privateDepts);
125
    }
126
127
    protected function doShiftTimeChecks($shift, $entry)
128
    {
129
        $now = new DateTime();
130
        if($shift->startTime < $now)
131
        {
132
            $entry['available'] = false;
133
            $entry['why'] = 'Shift already started';
134
        }
135
        if($shift->endTime < $now)
136
        {
137
            $entry['available'] = false;
138
            $entry['why'] = 'Shift already ended';
139
        }
140
    }
141
142
    protected function processShift($entry)
143
    {
144
        static $profile = null;
145
        static $eeAvailable = false;
146
        static $canDoRole = array();
147
        static $roles = array();
148
        if($profile === null)
149
        {
150
            $profile = new \VolunteerProfile($this->user->uid);
1 ignored issue
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...
151
            $eeAvailable = $profile->isEEAvailable();
152
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
153
            $tmp = $dataTable->read();
154
            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...
155
            {
156
               $roles[$role['short_name']] = $role;
157
            }
158
        }
159
        $shift = new \VolunteerShift(false, $entry);
160
        $entry['isAdmin'] = $this->isAdminForShift($entry, $this->user);
161
        $entry['overlap'] = $shift->findOverlaps($this->user->uid, true);
162
        if(!$this->shouldShowDepartment($entry['departmentID'], $entry['isAdmin']))
163
        {
164
            return null;
165
        }
166
        $entry['available'] = true;
167
        $this->doShiftTimeChecks($shift, $entry);
168
        if($entry['earlyLate'] != -1 && !$eeAvailable)
169
        {
170
            $entry['available'] = false;
171
            $entry['why'] = 'Shift requires early entry or late stay and you have not provided your legal name';
172
        }
173
        if(!isset($canDoRole[$entry['roleID']]))
174
        {
175
            $canDoRole[$entry['roleID']] = $this->canUserDoRole($profile, $roles[$entry['roleID']]);
176
        }
177
        if($canDoRole[$entry['roleID']] !== true)
178
        {
179
            $entry['available'] = false;
180
            $entry['why'] = $canDoRole[$entry['roleID']]['whyMsg'];
181
            $entry['whyClass'] = $canDoRole[$entry['roleID']]['whyClass'];
182
        }
183
        if($shift->isFilled())
184
        {
185
            $entry['volunteer'] = $this->getParticipantDiplayName($entry['participant']);
186
            if($entry['participant'] === $profile->uid)
0 ignored issues
show
Documentation introduced by
The property uid does not exist on object<VolunteerProfile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
187
            {
188
                $entry['available'] = false;
189
                $entry['why'] = 'Shift is already taken, by you';
190
                $entry['whyClass'] = 'MINE';
191
            }
192
            else
193
            {
194
                $entry['available'] = false;
195
                $entry['why'] = 'Shift is already taken';
196
                $entry['whyClass'] = 'TAKEN';
197
            }
198
            if(!$entry['isAdmin'])
199
            {
200
                unset($entry['participant']);
201
            }
202
        }
203
        return $entry;
204
    }
205
206
    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...
207
    {
208
        $entry['isAdmin'] = $this->isAdminForRole($entry, $this->user);
209
        return $entry;
210
    }
211
}
212
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
213