Completed
Push — FVSv2 ( daaaca...306d39 )
by Patrick
01:42
created

VolunteerShift::__get()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 13
nop 1
dl 0
loc 46
rs 6.6166
c 0
b 0
f 0

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