Completed
Push — master ( 470265...410c93 )
by Patrick
01:39
created

VolunteerShift::overlaps()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 1
dl 0
loc 16
rs 9.1111
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 'participantObj':
74
                if($this->participantObj === null)
75
                {
76
                    if(isset($this->dbData['participant']))
77
                    {
78
                        $this->participantObj = new \VolunteerProfile($this->dbData['participant']);
0 ignored issues
show
Bug introduced by
The property participantObj 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...
79
                    }
80
                    else
81
                    {
82
                        $this->participantObj = false;
83
                    }
84
                }
85
                return $this->participantObj;
86 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...
87
                if($this->webParticipantName === null)
88
                {
89
                    if(isset($this->dbData['participant']))
90
                    {
91
                        $tmp = new \VolunteerProfile($this->dbData['participant']);
92
                        $this->webParticipantName = $tmp->getDisplayName();
93
                    }
94
                    else
95
                    {
96
                        $this->webParticipantName = "";
97
                    }
98
                }
99
                return $this->webParticipantName;
100 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...
101
                if($this->scheduleParticipantName === null)
102
                {
103
                    if(isset($this->dbData['participant']))
104
                    {
105
                        $tmp = new \VolunteerProfile($this->dbData['participant']);
106
                        $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...
107
                    }
108
                    else
109
                    {
110
                        $this->scheduleParticipantName = "";
111
                    }
112
                }
113
                return $this->scheduleParticipantName;
114
            default:
115
                return $this->dbData[$propName];
116
        }
117
    }
118
119
    public function isSame($shift)
120
    {
121
        return $this->dbData['_id'] === $shift->dbData['_id'];
122
    }
123
124
    public function overlaps($shift)
125
    {
126
        if($this->isSame($shift))
127
        {
128
            return false;
129
        }
130
        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...
131
        {
132
            return true;
133
        }
134
        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...
135
        {
136
            return true;
137
        }
138
        return false;
139
    }
140
141
    public function isFilled()
142
    {
143
         return isset($this->dbData['status']) && ($this->dbData['status'] === 'pending' || $this->dbData['status'] === 'filled' || $this->dbData['status'] === 'groupPending');
144
    }
145
146
    public function findOverlaps($uid, $shortCircuit = false)
147
    {
148
        static $userShifts = null;
149
        static $count = 0;
150
        if($userShifts === null)
151
        {
152
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
153
            $filter = new \Data\Filter("participant eq '$uid'");
154
            $userShifts = $dataTable->read($filter);
155
            $count = count($userShifts);
156
            for($i = 0; $i < $count; $i++)
157
            {
158
                $userShifts[$i] = new VolunteerShift(false, $userShifts[$i]);
159
            }
160
        }
161
        $res = array();
162
        for($i = 0; $i < $count; $i++)
163
        {
164
            if($this->overlaps($userShifts[$i]))
165
            {
166
                if($shortCircuit === true)
167
                {
168
                    return true;
169
                }
170
                array_push($res, $userShifts[$i]);
171
            }
172
        }
173
        if($shortCircuit === true)
174
        {
175
            return false;
176
        }
177
        return $res;
178
    }
179
}
180