|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SDIS62\Core\User\Service; |
|
4
|
|
|
|
|
5
|
|
|
use SDIS62\Core\User\Entity\User; |
|
6
|
|
|
use SDIS62\Core\User\Repository\UserRepositoryInterface; |
|
7
|
|
|
|
|
8
|
|
|
class UserService |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Initialisation du service avec les repository utilisés |
|
12
|
|
|
* |
|
13
|
|
|
* @param SDIS62\Core\User\Repository\UserRepositoryInterface $user_repository |
|
14
|
|
|
*/ |
|
15
|
18 |
|
public function __construct(UserRepositoryInterface $user_repository) |
|
16
|
|
|
{ |
|
17
|
18 |
|
$this->user_repository = $user_repository; |
|
|
|
|
|
|
18
|
18 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Sauvegarde d'un utilisateur (insert et update si un id est présent) |
|
22
|
|
|
* |
|
23
|
|
|
* <code> |
|
24
|
|
|
* $array = array( |
|
25
|
|
|
* 'gender' => 'male', |
|
26
|
|
|
* 'first_name' => 'Kevin', |
|
27
|
|
|
* 'last_name' => 'DUBUC', |
|
28
|
|
|
* 'email' => '[email protected]', |
|
29
|
|
|
* 'birthday' => '14-08-1988', |
|
30
|
|
|
* 'picture_url' => 'http://www.sdis62.fr/avatar.jpg' |
|
31
|
|
|
* ); |
|
32
|
|
|
* </code> |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $data Tableau des données de l'utilisateur |
|
35
|
|
|
* @return SDIS62\Core\User\Entity\User |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public function save(array $data) |
|
38
|
|
|
{ |
|
39
|
3 |
|
$user = empty($data['id']) ? new User($data['gender'], $data['first_name'], $data['last_name'], $data['email']) : $this->find($data["id"]); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
3 |
|
if (!empty($data['first_name'])) { |
|
42
|
3 |
|
$user->setFirstName($data['first_name']); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
if (!empty($data['last_name'])) { |
|
46
|
3 |
|
$user->setLastName($data['last_name']); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
if (!empty($data['gender'])) { |
|
50
|
3 |
|
$user->setGender($data['gender']); |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
if (!empty($data['email'])) { |
|
54
|
3 |
|
$user->setEmail($data['email']); |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
if (!empty($data['birthday'])) { |
|
58
|
3 |
|
$user->setBirthday($data['birthday']); |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
if (!empty($data['picture_url'])) { |
|
62
|
3 |
|
$user->setPictureUrl($data['picture_url']); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
if (!empty($data['email'])) { |
|
66
|
3 |
|
$user->setEmail($data['email']); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
$this->user_repository->save($user); |
|
70
|
|
|
|
|
71
|
3 |
|
return $user; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Suppression d'un utilisateur |
|
76
|
|
|
* |
|
77
|
|
|
* @param int $id_user |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function delete($id_user) |
|
80
|
|
|
{ |
|
81
|
3 |
|
$user = $this->find($id_user); |
|
82
|
3 |
|
$this->user_repository->delete($user); |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Récupération de l'ensemble des utilisateurs |
|
87
|
|
|
* |
|
88
|
|
|
* @return SDIS62\Core\User\Entity\User[] |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function getAll() |
|
91
|
|
|
{ |
|
92
|
3 |
|
return $this->user_repository->getAll(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Récupération de l'utilisateur à l'id spécifié |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $id_user |
|
99
|
|
|
* @return SDIS62\Core\User\Entity\User |
|
100
|
|
|
*/ |
|
101
|
6 |
|
public function find($id_user) |
|
102
|
|
|
{ |
|
103
|
6 |
|
return $this->user_repository->find($id_user); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Récupération de l'utilisateur correspondant à l'email |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $email |
|
110
|
|
|
* @return SDIS62\Core\User\Entity\User |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function findByEmail($email) |
|
113
|
|
|
{ |
|
114
|
3 |
|
return $this->user_repository->findByEmail($email); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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: