Completed
Push — master ( ba97ae...9147af )
by Patrick
01:45
created

VolunteerShift::isFilled()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * A class to abstract access to a Volunter System Shift.
4
 *
5
 * This class is the primary method to access shift information.
6
 *
7
 * @property string $_id The shift's ID
8
 * @property string $departmentID The shift's department
9
 * @property string $eventID The shift's event
10
 * @property string $roleID The shift's role
11
 * @property DateTime $startTime The shifts's starting time
12
 * @property DateTime $endTime The shifts's ending time
13
 * @property boolean $enabled Is the shift available for signup
14
 * @property string $earlyLate The shift's Early Entry/Late Stay status
15
 */
16
class VolunteerShift extends VolunteerObject
17
{
18
    protected static $deptCache = array();
19
    protected static $roleCache = array();
20
    protected static $eventCache = array();
21
    protected $mod = null;
22
    protected $myStart = null;
23
    protected $myEnd = null;
24
    protected $modStart = null;
25
    protected $modEnd = null;
26
    protected $participantObj = null;
27
    protected $webParticipantName = null;
28
29
    public function __construct($shiftID, $dbData = null)
30
    {
31
        parent::__construct($shiftID, $dbData, 'shifts', '_id');
32
    }
33
34
    public function __get($propName)
35
    {
36
        switch($propName)
37
        {
38
            case 'modTime':
39
                if($this->mod === null)
40
                {
41
                    $this->mod = new \DateInterval('PT'.strval($this->role->down_time).'H');
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<VolunteerShift>. 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...
42
                }
43
                return $this->mod;
44
            case 'startTime':
45
                if($this->myStart === null)
46
                {
47
                    $this->myStart = new \DateTime($this->dbData['startTime']);
48
                }
49
                return $this->myStart;
50
            case 'startTimeWithMod':
51
                if($this->modStart === null)
52
                {
53
                    $this->modStart = clone $this->startTime;
54
                    $this->modStart->sub($this->modTime);
0 ignored issues
show
Documentation introduced by
The property modTime does not exist on object<VolunteerShift>. 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...
55
                }
56
                return $this->modStart;
57
            case 'endTime':
58
                if($this->myEnd === null)
59
                {
60
                    $this->myEnd = new \DateTime($this->dbData['endTime']);
61
                }
62
                return $this->myEnd;
63
            case 'endTimeWithMod':
64
                if($this->modEnd === null)
65
                {
66
                    $this->modEnd = clone $this->endTime;
67
                    $this->modEnd->add($this->modTime);
0 ignored issues
show
Documentation introduced by
The property modTime does not exist on object<VolunteerShift>. 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...
68
                }
69
                return $this->modEnd;
70
            case 'department':
71
                if(!isset(self::$deptCache[$this->dbData['departmentID']]))
72
                {
73
                    self::$deptCache[$this->dbData['departmentID']] = new \VolunteerDepartment($this->dbData['departmentID']);
74
                }
75
                return self::$deptCache[$this->dbData['departmentID']];
76 View Code Duplication
            case 'role':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
                if(!isset(self::$roleCache[$this->dbData['roleID']]))
78
                {
79
                    self::$roleCache[$this->dbData['roleID']] = new \VolunteerRole($this->dbData['roleID']);
80
                }
81
                return self::$roleCache[$this->dbData['roleID']];
82 View Code Duplication
            case 'event':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
                if(!isset(self::$eventCache[$this->dbData['eventID']]))
84
                {
85
                    self::$eventCache[$this->dbData['eventID']] = new \VolunteerEvent($this->dbData['eventID']);
86
                }
87
                return self::$eventCache[$this->dbData['eventID']];
88
            case 'participantObj':
89
                if($this->participantObj === null)
90
                {
91
                    if(isset($this->dbData['participant']))
92
                    {
93
                        $this->participantObj = new \VolunteerProfile($this->dbData['participant']);
94
                    }
95
                    else
96
                    {
97
                        $this->participantObj = false;
98
                    }
99
                }
100
                return $this->participantObj;
101 View Code Duplication
            case 'webParticipantName':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
                if($this->webParticipantName === null)
103
                {
104
                    if(isset($this->dbData['participant']))
105
                    {
106
                        $tmp = new \VolunteerProfile($this->dbData['participant']);
107
                        $this->webParticipantName = $tmp->getDisplayName();
108
                    }
109
                    else
110
                    {
111
                        $this->webParticipantName = "";
112
                    }
113
                }
114
                return $this->webParticipantName;
115 View Code Duplication
            case 'scheduleParticipantName':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
116
                if($this->scheduleParticipantName === null)
117
                {
118
                    if(isset($this->dbData['participant']))
119
                    {
120
                        $tmp = new \VolunteerProfile($this->dbData['participant']);
121
                        $this->scheduleParticipantName = $tmp->getDisplayName('paperName');
0 ignored issues
show
Bug introduced by
The property scheduleParticipantName 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...
122
                    }
123
                    else
124
                    {
125
                        $this->scheduleParticipantName = "";
126
                    }
127
                }
128
                return $this->scheduleParticipantName;
129
            default:
130
                return $this->dbData[$propName];
131
        }
132
    }
133
134
    public function isSame($shift)
135
    {
136
        return $this->dbData['_id'] === $shift->dbData['_id'];
137
    }
138
139
    public function overlaps($shift)
140
    {
141
        if($this->isSame($shift))
142
        {
143
            return false;
144
        }
145
        if($this->startTimeWithMod > $shift->startTimeWithMod && $this->startTimeWithMod < $shift->endTimeWithMod)
0 ignored issues
show
Bug introduced by
The property startTimeWithMod does not seem to exist. Did you mean startTime?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
146
        {
147
            return true;
148
        }
149
        if($this->endTimeWithMod < $shift->endTimeWithMod && $this->endTimeWithMod > $shift->startTimeWithMod)
0 ignored issues
show
Bug introduced by
The property endTimeWithMod does not seem to exist. Did you mean endTime?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
150
        {
151
            return true;
152
        }
153
        return false;
154
    }
155
156
    public function isFilled()
157
    {
158
         return isset($this->dbData['status']) && ($this->dbData['status'] === 'pending' || $this->dbData['status'] === 'filled' || $this->dbData['status'] === 'groupPending');
159
    }
160
161
    public function findOverlaps($uid, $shortCircuit = false)
162
    {
163
        static $userShifts = null;
164
        static $count = 0;
165
        if($userShifts === null)
166
        {
167
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
168
            $filter = new \Data\Filter("participant eq '$uid'");
169
            $userShifts = $dataTable->read($filter);
170
            $count = count($userShifts);
171
            for($i = 0; $i < $count; $i++)
172
            {
173
                $userShifts[$i] = new VolunteerShift(false, $userShifts[$i]);
174
            }
175
        }
176
        $res = array();
177
        for($i = 0; $i < $count; $i++)
178
        {
179
            if($this->overlaps($userShifts[$i]))
180
            {
181
                if($shortCircuit === true)
182
                {
183
                    return true;
184
                }
185
                array_push($res, $userShifts[$i]);
186
            }
187
        }
188
        if($shortCircuit === true)
189
        {
190
            return false;
191
        }
192
        return $res;
193
    }
194
195
    public function makeCopy($dataTable)
196
    {
197
        $tmp = $this->dbData;
198
        unset($tmp['_id']);
199
        $dataTable->create($tmp);
200
    }
201
}
202