PlageHoraire   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 18
c 5
b 2
f 1
lcom 3
cbo 5
dl 0
loc 167
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getStart() 0 4 1
A getEnd() 0 4 1
A contains() 0 4 2
B includes() 0 13 6
A getType() 0 8 2
A getPompier() 0 4 1
A getPlanning() 0 4 1
A getSpecialites() 0 8 2
1
<?php
2
3
namespace SDIS62\Core\Ops\Entity;
4
5
use Datetime;
6
use SDIS62\Core\Common\Entity\IdentityTrait;
7
use SDIS62\Core\Ops\Exception\InvalidDateException;
8
use SDIS62\Core\Ops\Exception\InvalidPlageHoraireTypeException;
9
10
abstract class PlageHoraire
11
{
12
    use IdentityTrait;
13
14
    /**
15
     * Début de la plage horaire.
16
     *
17
     * @var Datetime
18
     */
19
    protected $start;
20
21
    /**
22
     * Fin de la plage horaire.
23
     *
24
     * @var Datetime
25
     */
26
    protected $end;
27
28
    /**
29
     * Planning.
30
     *
31
     * @var SDIS62\Core\Ops\Entity\Planning
32
     */
33
    protected $planning;
34
35
    /**
36
     * Pompier concerné.
37
     *
38
     * @var SDIS62\Core\Ops\Entity\Pompier
39
     */
40
    protected $pompier;
41
42
    /**
43
     * Spécialités de la plage horaire.
44
     *
45
     * @var array
46
     */
47
    protected $specialites;
48
49
    /**
50
     * Ajout d'une plage horaire.
51
     *
52
     * @param SDIS62\Core\Ops\Entity\Planning $planning
53
     * @param SDIS62\Core\Ops\Entity\Pompier  $pompier
54
     * @param Datetime                        $start
55
     * @param Datetime                        $end
56
     * @param array                           $specialites
57
     */
58
    public function __construct(Planning $planning, Pompier $pompier, Datetime $start, Datetime $end, array $specialites = [])
59
    {
60
        $this->start       = $start;
61
        $this->end         = $end;
62
        $this->specialites = $specialites;
63
64
        if ($this->start->diff($this->end)->invert == 1) {
65
            throw new InvalidDateException('Events usually start before they end');
66
        }
67
68
        $this->planning = $planning;
0 ignored issues
show
Documentation Bug introduced by
It seems like $planning of type object<SDIS62\Core\Ops\Entity\Planning> is incompatible with the declared type object<SDIS62\Core\Ops\E...re\Ops\Entity\Planning> of property $planning.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
        $this->pompier  = $pompier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pompier of type object<SDIS62\Core\Ops\Entity\Pompier> is incompatible with the declared type object<SDIS62\Core\Ops\E...ore\Ops\Entity\Pompier> of property $pompier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
71
        $this->planning->addPlageHoraire($this);
72
        $this->pompier->addPlageHoraire($this);
73
    }
74
75
    /**
76
     * Get the value of Début de la garde.
77
     *
78
     * @return Datetime
79
     */
80
    public function getStart()
81
    {
82
        return $this->start;
83
    }
84
85
    /**
86
     * Get the value of Fin de la garde.
87
     *
88
     * @return Datetime
89
     */
90
    public function getEnd()
91
    {
92
        return $this->end;
93
    }
94
95
    /**
96
     * Check si le Datetime est contenu dans la plage horaire.
97
     *
98
     * @param DateTime $date
99
     *
100
     * @return bool
101
     */
102
    public function contains(DateTime $date)
103
    {
104
        return $this->start <= $date && $date < $this->end;
105
    }
106
107
    /**
108
     * Retourne vrai si la plage horaire est contenu dans la place horaire actuelle.
109
     *
110
     * @param PlageHoraire $plage
111
     * @param bool         $strict
112
     *
113
     * @return bool
114
     */
115
    public function includes(PlageHoraire $plage, $strict = true)
116
    {
117
        if (true === $strict) {
118
            return $this->getStart() <= $plage->getStart() && $this->getEnd() >= $plage->getEnd();
119
        }
120
121
        return
122
            $this->includes($plage, true) ||
123
            $plage->includes($this, true) ||
124
            $this->contains($plage->getStart()) ||
125
            $this->contains($plage->getEnd())
126
        ;
127
    }
128
129
    /**
130
     * Get the value of Type.
131
     *
132
     * @return string
133
     */
134
    final public function getType()
135
    {
136
        if (empty($this->type)) {
0 ignored issues
show
Bug introduced by
The property type 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...
137
            throw new InvalidPlageHoraireTypeException(get_class($this).' doit avoir un $type');
138
        }
139
140
        return $this->type;
141
    }
142
143
    /**
144
     * Get the value of Pompier concerné.
145
     *
146
     * @return SDIS62\Core\Ops\Entity\Pompier
147
     */
148
    public function getPompier()
149
    {
150
        return $this->pompier;
151
    }
152
153
    /**
154
     * Get the value of Planning.
155
     *
156
     * @return SDIS62\Core\Ops\Entity\Planning
157
     */
158
    public function getPlanning()
159
    {
160
        return $this->planning;
161
    }
162
163
    /**
164
     * Spécialitées de la plage horaire.
165
     *
166
     * @return array
167
     */
168
    public function getSpecialites()
169
    {
170
        if (!($this->getPompier() instanceof Pompier\SpecialistePompier)) {
171
            return [];
172
        }
173
174
        return array_intersect(array_values($this->specialites), $this->getPompier()->getSpecialites());
175
    }
176
}
177