Shift::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 6
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Scheduler\Domain\Model\Shift;
4
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use Scheduler\Domain\Model\User\NullUser;
8
use Scheduler\Domain\Model\User\User;
9
10
class Shift
11
{
12
    private $id;
13
    private $manager;
14
    private $employee;
15
    private $break;
16
    private $startTime;
17
    private $endTime;
18
    private $coworkers;
19
    private $created;
20
    private $updated;
21
22
    public static function withManagerAndTimes(User $manager, DateTimeInterface $start, DateTimeInterface $end, $break = 0.0)
23
    {
24
        $employee = new NullUser();
25
26
        return new Shift(null, $manager, $employee, $break, $start, $end);
27
    }
28
29
    public function __construct($id, User $manager, User $employee, $break,
30
        DateTimeInterface $startTime, DateTimeInterface $endTime,
31
        DateTimeInterface $created = null, DateTimeInterface $updated = null)
32
    {
33
        if (isset($id) && ! is_int($id)) {
34
            throw new \InvalidArgumentException("The id must be an integer");
35
        }
36
37
        if (! is_float($break)) {
38
            throw new \InvalidArgumentException("The break must be a float");
39
        }
40
41
        $this->id = $id;
42
        $this->manager = $manager;
43
        $this->employee = $employee;
44
        $this->break = $break;
45
        $this->startTime = $startTime;
46
        $this->endTime = $endTime;
47
        $this->coworkers = [];
48
49
        $this->created = isset($created) ? $created : new DateTimeImmutable();
50
        $this->updated = isset($updated) ? $updated : new DateTimeImmutable();
51
    }
52
53
    public function assignTo(User $employee)
54
    {
55
        $shift = clone $this;
56
        $shift->employee = $employee;
57
        $shift->updated = new DateTimeImmutable();
58
59
        return $shift;
60
    }
61
62
    public function changeStartTime(DateTimeInterface $startTime)
63
    {
64
        $shift = clone $this;
65
        $shift->startTime = $startTime;
66
        $shift->updated = new DateTimeImmutable();
67
68
        return $shift;
69
    }
70
71
    public function changeEndTime(DateTimeInterface $endTime)
72
    {
73
        $shift = clone $this;
74
        $shift->endTime = $endTime;
75
        $shift->updated = new DateTimeImmutable();
76
77
        return $shift;
78
    }
79
80
    public function changeBreak($break)
81
    {
82
        $shift = clone $this;
83
        $shift->break = $break;
84
        $shift->updated = new DateTimeImmutable();
85
86
        return $shift;
87
    }
88
89
    public function withCoworkers(array $coworkers)
90
    {
91
        $shift = clone $this;
92
        $shift->coworkers = $coworkers;
93
        $shift->updated = new DateTimeImmutable();
94
95
        return $shift;
96
    }
97
98
    public function getId()
99
    {
100
        return $this->id;
101
    }
102
103
    public function getManager()
104
    {
105
        return $this->manager;
106
    }
107
108
    public function getEmployee()
109
    {
110
        return $this->employee;
111
    }
112
113
    public function getBreak()
114
    {
115
        return $this->break;
116
    }
117
118
    public function getStartTime()
119
    {
120
        return $this->startTime;
121
    }
122
123
    public function getEndTime()
124
    {
125
        return $this->endTime;
126
    }
127
128
    public function getHours()
129
    {
130
        $diff = $this->startTime->diff($this->endTime);
131
132
        return $diff->h + ($diff->i / 60) - $this->break;
133
    }
134
135
    public function getCreated()
136
    {
137
        return $this->created;
138
    }
139
140
    public function getUpdated()
141
    {
142
        return $this->updated;
143
    }
144
145
    public function hasCoworkers()
146
    {
147
        return (bool) count($this->coworkers);
148
    }
149
150
    public function getCoworkers()
151
    {
152
        return $this->coworkers;
153
    }
154
}
155