Completed
Push — master ( df6d12...eab9e2 )
by Patrick
01:41 queued 10s
created

VolunteerShift::isSame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
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 $roleCache = array();
19
    protected $mod = null;
20
    protected $myStart = null;
21
    protected $myEnd = null;
22
    protected $modStart = null;
23
    protected $modEnd = null;
24
    protected $webParticipantName = null;
25
26
    public function __construct($shiftID, $dbData = null)
27
    {
28
        parent::__construct($shiftID, $dbData, 'shifts', '_id');
29
    }
30
31
    public function __get($propName)
32
    {
33
        switch($propName)
34
        {
35
            case 'modTime':
36
                if($this->mod === null)
37
                {
38
                    $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...
39
                }
40
                return $this->mod;
41
            case 'startTime':
42
                if($this->myStart === null)
43
                {
44
                    $this->myStart = new \DateTime($this->dbData['startTime']);
45
                }
46
                return $this->myStart;
47
            case 'startTimeWithMod':
48
                if($this->modStart === null)
49
                {
50
                    $this->modStart = clone $this->startTime;
51
                    $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...
52
                }
53
                return $this->modStart;
54
            case 'endTime':
55
                if($this->myEnd === null)
56
                {
57
                    $this->myEnd = new \DateTime($this->dbData['endTime']);
58
                }
59
                return $this->myEnd;
60
            case 'endTimeWithMod':
61
                if($this->modEnd === null)
62
                {
63
                    $this->modEnd = clone $this->endTime;
64
                    $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...
65
                }
66
                return $this->modEnd;
67
            case 'role':
68
                if(!isset(self::$roleCache[$this->dbData['roleID']]))
69
                {
70
                    self::$roleCache[$this->dbData['roleID']] = new \VolunteerRole($this->dbData['roleID']);
71
                }
72
                return self::$roleCache[$this->dbData['roleID']];
73
            case 'webParticipantName':
74
                if($this->webParticipantName === null)
75
                {
76
                    if(isset($this->dbData['participant']))
77
                    {
78
                        $tmp = new \VolunteerProfile($this->dbData['participant']);
79
                        $this->webParticipantName = $tmp->getDisplayName();
80
                    }
81
                    else
82
                    {
83
                        $this->webParticipantName = "";
84
                    }
85
                }
86
                return $this->webParticipantName;
87
            default:
88
                return $this->dbData[$propName];
89
        }
90
    }
91
92
    public function isSame($shift)
93
    {
94
        return $this->dbData['_id'] === $shift->dbData['_id'];
95
    }
96
97
    public function overlaps($shift)
98
    {
99
        if($this->isSame($shift))
100
        {
101
            return false;
102
        }
103
        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...
104
        {
105
            return true;
106
        }
107
        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...
108
        {
109
            return true;
110
        }
111
        return false;
112
    }
113
114
    public function isFilled()
115
    {
116
         return isset($this->dbData['status']) && ($this->dbData['status'] === 'pending' || $this->dbData['status'] === 'filled' || $this->dbData['status'] === 'groupPending');
117
    }
118
119
    public function findOverlaps($uid, $shortCircuit = false)
120
    {
121
        static $userShifts = null;
122
        static $count = 0;
123
        if($userShifts === null)
124
        {
125
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
126
            $filter = new \Data\Filter("participant eq '$uid'");
127
            $userShifts = $dataTable->read($filter);
128
            $count = count($userShifts);
129
            for($i = 0; $i < $count; $i++)
130
            {
131
                $userShifts[$i] = new VolunteerShift(false, $userShifts[$i]);
132
            }
133
        }
134
        $res = array();
135
        for($i = 0; $i < $count; $i++)
136
        {
137
            if($this->overlaps($userShifts[$i]))
138
            {
139
                if($shortCircuit === true)
140
                {
141
                    return true;
142
                }
143
                array_push($res, $userShifts[$i]);
144
            }
145
        }
146
        if($shortCircuit === true)
147
        {
148
            return false;
149
        }
150
        return $res;
151
    }
152
}
153