|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SDIS62\Core\Ops\Service; |
|
4
|
|
|
|
|
5
|
|
|
use SDIS62\Core\Ops\Entity\Coordinates; |
|
6
|
|
|
use SDIS62\Core\Ops\Entity\Pompier; |
|
7
|
|
|
use SDIS62\Core\Ops\Entity\Statut; |
|
8
|
|
|
use SDIS62\Core\Ops\Exception\InvalidPompierException; |
|
9
|
|
|
use SDIS62\Core\Ops\Repository\CentreRepositoryInterface; |
|
10
|
|
|
use SDIS62\Core\Ops\Repository\PompierRepositoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class PompierService |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Initialisation du service avec les repository utilisés. |
|
16
|
|
|
* |
|
17
|
|
|
* @param SDIS62\Core\Ops\Repository\PompierRepositoryInterface $pompier_repository |
|
18
|
|
|
* @param SDIS62\Core\Ops\Repository\CentreRepositoryInterface $centre_repository |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(PompierRepositoryInterface $pompier_repository, |
|
21
|
|
|
CentreRepositoryInterface $centre_repository |
|
22
|
|
|
) { |
|
23
|
|
|
$this->pompier_repository = $pompier_repository; |
|
|
|
|
|
|
24
|
|
|
$this->centre_repository = $centre_repository; |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Retourne un pompier correspondant au matricule spécifié. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $matricule |
|
31
|
|
|
* |
|
32
|
|
|
* @return SDIS62\Core\Ops\Entity\Pompier |
|
33
|
|
|
*/ |
|
34
|
|
|
public function find($matricule) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->pompier_repository->find($matricule); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retourne un pompier correspondant au nom spécifié. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @param int $count Par défaut: 20 |
|
44
|
|
|
* @param int $page Par défaut: 1 |
|
45
|
|
|
* |
|
46
|
|
|
* @return SDIS62\Core\Ops\Entity\Pompier |
|
47
|
|
|
*/ |
|
48
|
|
|
public function findAllByName($name, $count = 20, $page = 1) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->pompier_repository->findAllByName($name, $count, $page); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sauvegarde d'un pompier. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $data |
|
57
|
|
|
* @param array $id_pompier Optionnel |
|
|
|
|
|
|
58
|
|
|
* |
|
59
|
|
|
* @return SDIS62\Core\Ops\Entity\Pompier |
|
60
|
|
|
*/ |
|
61
|
|
|
public function save($data) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!empty($data['matricule'])) { |
|
64
|
|
|
$pompier = $this->pompier_repository->find($data['matricule']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (empty($pompier)) { |
|
68
|
|
|
$centre = $this->centre_repository->find($data['centre']); |
|
69
|
|
|
|
|
70
|
|
|
if (empty($centre)) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
switch ($data['type']) { |
|
75
|
|
|
case 'pompier' : |
|
76
|
|
|
$pompier = new Pompier($data['name'], $data['matricule'], $centre); |
|
77
|
|
|
break; |
|
78
|
|
|
case 'specialiste' : |
|
79
|
|
|
$pompier = new Pompier\SpecialistePompier($data['name'], $data['matricule'], $centre); |
|
80
|
|
|
break; |
|
81
|
|
|
default: |
|
82
|
|
|
throw new InvalidPompierException(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (!empty($data['centre'])) { |
|
87
|
|
|
$pompier->setCentre($this->centre_repository->find($data['centre'])); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (!empty($data['name'])) { |
|
91
|
|
|
$pompier->setName($data['name']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!empty($data['specialites'])) { |
|
95
|
|
|
$pompier->setSpecialites($data['specialites']); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!empty($data['phone_number'])) { |
|
99
|
|
|
$pompier->setPhoneNumber($data['phone_number']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!empty($data['statut'])) { |
|
103
|
|
|
$pompier->setStatut(Statut::getByName($data['statut'])); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (array_key_exists('pro', $data)) { |
|
107
|
|
|
$pompier->setPro($data['pro'] === true); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (!empty($data['coordinates']) && is_array($data['coordinates']) && count($data['coordinates']) >= 2) { |
|
111
|
|
|
$pompier->setCoordinates(new Coordinates( |
|
112
|
|
|
$data['coordinates'][0], |
|
113
|
|
|
$data['coordinates'][1], |
|
114
|
|
|
array_key_exists(2, $data['coordinates']) ? $data['coordinates'][2] : null |
|
115
|
|
|
)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->pompier_repository->save($pompier); |
|
119
|
|
|
|
|
120
|
|
|
return $pompier; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Suppression d'un pompier. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $matricule |
|
127
|
|
|
* |
|
128
|
|
|
* @return SDIS62\Core\Ops\Entity\Pompier |
|
129
|
|
|
*/ |
|
130
|
|
|
public function delete($matricule) |
|
131
|
|
|
{ |
|
132
|
|
|
$pompier = $this->find($matricule); |
|
133
|
|
|
|
|
134
|
|
|
if (empty($pompier)) { |
|
135
|
|
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->pompier_repository->delete($pompier); |
|
139
|
|
|
|
|
140
|
|
|
return $pompier; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: