Completed
Push — FVSv2 ( bd131d...9a40f1 )
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
class VolunteerShift
3
{
4
    protected $dbData;
5
    protected $myStart = null;
6
    protected $myEnd = null;
7
8 View Code Duplication
    public function __construct($shiftID, $dbData = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
9
    {
10
        if($dbData === null)
11
        {
12
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
13
            $filter = new \Data\Filter('_id eq '.$shiftID);
14
            $depts = $dataTable->read($filter);
15
            if(empty($depts))
16
            {
17
                throw new Exception('Unable to locate shift with ID '.$shiftID);
18
            }
19
            $dbData = $depts[0];
20
        }
21
        $this->dbData = $dbData;
22
    }
23
24
    public function __get($propName)
25
    {
26
        switch($propName)
27
        {
28
            case 'startTime':
29
                if($this->myStart === null)
30
                {
31
                    $this->myStart = new \DateTime($this->dbData['startTime']);
32
                }
33
                return $this->myStart;
34
            case 'endTime':
35
                if($this->myEnd === null)
36
                {
37
                    $this->myEnd = new \DateTime($this->dbData['endTime']);
38
                }
39
                return $this->myEnd;
40
            default:
41
                return $this->dbData[$propName];
42
        }
43
    }
44
45
    public function isSame($shift)
46
    {
47
        return $this->dbData['_id']->{'$id'} === $shift->dbData['_id']->{'$id'};
48
    }
49
50
    public function overlaps($shift)
51
    {
52
        if($this->isSame($shift))
53
        {
54
            return false;
55
        }
56
        if($this->startTime >= $shift->startTime && $this->startTime < $shift->endTime)
0 ignored issues
show
Documentation introduced by
The property startTime 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...
57
        {
58
            return true;
59
        }
60
        if($this->endTime <= $shift->endTime && $this->endTime > $shift->startTime)
0 ignored issues
show
Documentation introduced by
The property endTime 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...
61
        {
62
            return true;
63
        }
64
        return false;
65
    }
66
67
    public function isFilled()
68
    {
69
         return isset($this->dbData['status']) && ($this->dbData['status'] === 'pending' || $this->dbData['status'] === 'filled');
70
    }
71
72
    public function findOverlaps($uid, $shortCircuit = false)
73
    {
74
        static $userShifts = null;
75
        static $count = 0;
76
        if($userShifts === null)
77
        {
78
            $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
79
            $filter = new \Data\Filter("participant eq '$uid'");
80
            $userShifts = $dataTable->read($filter);
81
            $count = count($userShifts);
82
            for($i = 0; $i < $count; $i++)
83
            {
84
                $userShifts[$i] = new VolunteerShift(false, $userShifts[$i]);
85
            }
86
        }
87
        $res = array();
88
        for($i = 0; $i < $count; $i++)
89
        {
90
            if($this->overlaps($userShifts[$i]))
91
            {
92
                if($shortCircuit === true)
93
                {
94
                    return true;
95
                }
96
                array_push($res, $userShifts[$i]);
97
            }
98
        }
99
        if($shortCircuit === true)
100
        {
101
            return false;
102
        }
103
        return $res;
104
    }
105
}
106