Completed
Push — master ( 26a050...b0c7d0 )
by Amine
06:54
created

Pause   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.65%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 10
c 4
b 0
f 2
lcom 1
cbo 0
dl 0
loc 78
ccs 22
cts 23
cp 0.9565
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getStart() 0 4 1
A getEnd() 0 4 1
A getDuration() 0 9 2
A isOverlapping() 0 12 3
1
<?php
2
3
namespace AmineBenHariz\Attendance;
4
5
/**
6
 * Class Pause
7
 * @package AmineBenHariz\Attendance
8
 */
9
class Pause
10
{
11
    /**
12
     * @var \DateTime
13
     */
14
    private $start;
15
16
    /**
17
     * @var \DateTime
18
     */
19
    private $end;
20
21
    /**
22
     * Pause constructor.
23
     * @param \DateTime $start
24
     * @param \DateTime $end
25
     */
26 16
    public function __construct(\DateTime $start, \DateTime $end)
27
    {
28 16
        if ($start > $end) {
29 4
            throw new \InvalidArgumentException("Pause can't start after ending");
30
        }
31
32 12
        if ($start->format('Y-m-d') !== $end->format('Y-m-d')) {
33 4
            throw new \InvalidArgumentException("Pause must end in the same day");
34
        }
35
36 8
        $this->start = $start;
37 8
        $this->end = $end;
38 8
    }
39
40
    /**
41
     * @return \DateTime
42
     */
43 24
    public function getStart()
44
    {
45 24
        return $this->start;
46
    }
47
48
    /**
49
     * @return \DateTime
50
     */
51 24
    public function getEnd()
52
    {
53 24
        return $this->end;
54
    }
55
56
    /**
57
     * @return \DateInterval
58
     * @throws \Exception
59
     */
60 4
    public function getDuration()
61
    {
62 4
        $duration = $this->getStart()->diff($this->getEnd());
63 4
        if ($duration === false) {
64
            throw new \Exception('Error while calculating pause duration');
65
        }
66
67 4
        return $duration;
68
    }
69
70
    /**
71
     * @param Pause $pause
72
     * @return bool
73
     */
74 16
    public function isOverlapping(Pause $pause)
75
    {
76 16
        if ($this->getEnd() <= $pause->getStart()) {
77 8
            return false;
78
        }
79
80 16
        if ($this->getStart() >= $pause->getEnd()) {
81 8
            return false;
82
        }
83
84 8
        return true;
85
    }
86
}
87