Planning::addPlageHoraire()   C
last analyzed

Complexity

Conditions 13
Paths 6

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 41
rs 5.1234
cc 13
eloc 21
nc 6
nop 1

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
namespace SDIS62\Core\Ops\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use SDIS62\Core\Common\Entity\IdentityTrait;
7
use SDIS62\Core\Ops\Exception\InvalidDateException;
8
9
class Planning
10
{
11
    use IdentityTrait;
12
13
    /**
14
     * Nom du planning.
15
     *
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * Plages Horaires.
22
     *
23
     * @var SDIS62\Core\Ops\Entity\PlageHoraire[]
24
     */
25
    protected $plages_horaires;
26
27
    /**
28
     * Création d'un planning.
29
     *
30
     * @param string $name
31
     */
32
    public function __construct($name)
33
    {
34
        $this->name            = $name;
35
        $this->plages_horaires = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<SDI...s\Entity\PlageHoraire>> of property $plages_horaires.

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...
36
    }
37
38
    /**
39
     * Ajout d'une plage horaire.
40
     *
41
     * @param SDIS62\Core\Ops\Entity\PlageHoraire $plage_horaire
42
     *
43
     * @return self
44
     */
45
    public function addPlageHoraire(PlageHoraire $plage_horaire)
46
    {
47
        // Règles d'ajout
48
        if ($plage_horaire instanceof PlageHoraire\GardePlageHoraire) {
49
            // Contrôles des gardes existantes (une dispo ne peut pas être ajoutée sur une garde)
50
            foreach ($plage_horaire->getPompier()->getGardes() as $garde) {
51
                if ($garde->includes($plage_horaire, false)) {
52
                    throw new InvalidDateException('Une garde existe aux dates de la garde à ajouter');
53
                }
54
            }
55
            // Contrôles des dispo existantes (si une garde est posé sur une dispo, on la transforme)
56
            foreach ($plage_horaire->getPompier()->getDispos() as $dispo) {
57
                if ($dispo->includes($plage_horaire, false)) {
58
                    $plage_horaire->getPompier()->getPlagesHoraires()->removeElement($dispo);
59
                    if ($dispo->getStart() < $plage_horaire->getStart()) {
60
                        new PlageHoraire\DispoPlageHoraire($this, $plage_horaire->getPompier(), $dispo->getStart(), $plage_horaire->getStart());
61
                    }
62
                    if ($dispo->getEnd() > $plage_horaire->getEnd()) {
63
                        new PlageHoraire\DispoPlageHoraire($this, $plage_horaire->getPompier(), $plage_horaire->getEnd(), $dispo->getEnd());
64
                    }
65
                }
66
            }
67
        } elseif ($plage_horaire instanceof PlageHoraire\DispoPlageHoraire) {
68
            // Contrôles des gardes existantes (une dispo ne peut pas être ajoutée sur une garde)
69
            foreach ($plage_horaire->getPompier()->getGardes() as $garde) {
70
                if ($garde->includes($plage_horaire, false)) {
71
                    throw new InvalidDateException('Une garde existe aux dates de la dispo');
72
                }
73
            }
74
            // Contrôles des dispo existantes (une dispo ne peut pas être ajoutée sur une autre dispo)
75
            foreach ($plage_horaire->getPompier()->getDispos() as $dispo) {
76
                if ($dispo->includes($plage_horaire, false)) {
77
                    throw new InvalidDateException('Une disponibilité existe aux dates de la dispo');
78
                }
79
            }
80
        }
81
82
        $this->plages_horaires->add($plage_horaire);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->plages_horaires (of type array<integer,object<SDI...s\Entity\PlageHoraire>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
83
84
        return $this;
85
    }
86
87
    /**
88
     * Récupération des plages horaires du planning.
89
     *
90
     * @return SDIS62\Core\Ops\Entity\PlageHoraire[]
91
     */
92
    public function getPlagesHoraires()
93
    {
94
        return $this->plages_horaires;
95
    }
96
97
    /**
98
     * Get the value of Gardes.
99
     *
100
     * @return SDIS62\Core\Ops\Entity\PlageHoraire\Garde[]
101
     */
102
    public function getGardes()
103
    {
104
        $gardes = [];
105
106
        foreach ($this->plages_horaires as $plage_horaire) {
107
            if ($plage_horaire instanceof PlageHoraire\GardePlageHoraire) {
108
                $gardes[] = $plage_horaire;
109
            }
110
        }
111
112
        return $gardes;
113
    }
114
115
    /**
116
     * Get the value of Dispos.
117
     *
118
     * @return SDIS62\Core\Ops\Entity\PlageHoraire\Dispo[]
119
     */
120
    public function getDispos()
121
    {
122
        $dispos = [];
123
124
        foreach ($this->plages_horaires as $plage_horaire) {
125
            if ($plage_horaire instanceof PlageHoraire\GardePlageHoraire) {
126
                $dispos[] = $plage_horaire;
127
            }
128
        }
129
130
        return $dispos;
131
    }
132
133
    /**
134
     * Get the value of Nom du planning.
135
     *
136
     * @return string
137
     */
138
    public function getName()
139
    {
140
        return $this->name;
141
    }
142
143
    /**
144
     * Set the value of Nom du planning.
145
     *
146
     * @param string name
147
     *
148
     * @return self
149
     */
150
    public function setName($name)
151
    {
152
        $this->name = $name;
153
154
        return $this;
155
    }
156
}
157