1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\User; |
4
|
|
|
|
5
|
|
|
use Oc\Repository\Exception\RecordNotFoundException; |
6
|
|
|
use Oc\Repository\Exception\RecordsNotFoundException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UserService |
10
|
|
|
* |
11
|
|
|
* @package Oc\User |
12
|
|
|
*/ |
13
|
|
|
class UserService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var UserRepository |
17
|
|
|
*/ |
18
|
|
|
private $userRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* UserService constructor. |
22
|
|
|
* |
23
|
|
|
* @param UserRepository $userRepository |
24
|
|
|
*/ |
25
|
|
|
public function __construct(UserRepository $userRepository) |
26
|
|
|
{ |
27
|
|
|
$this->userRepository = $userRepository; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Fetches all users. |
32
|
|
|
* |
33
|
|
|
* @return UserEntity[] |
34
|
|
|
*/ |
35
|
|
|
public function fetchAll() |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
$result = $this->userRepository->fetchAll(); |
39
|
|
|
} catch (RecordsNotFoundException $e) { |
40
|
|
|
$result = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Fetches a user by its id. |
48
|
|
|
* |
49
|
|
|
* @param int $id |
50
|
|
|
* |
51
|
|
|
* @return null|UserEntity |
52
|
|
|
*/ |
53
|
|
|
public function fetchOneById($id) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$result = $this->userRepository->fetchOneById($id); |
57
|
|
|
} catch (RecordNotFoundException $e) { |
58
|
|
|
$result = null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $result; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function fetchOneBy(array $where) |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
$result = $this->userRepository->fetchOneBy($where); |
68
|
|
|
} catch (RecordNotFoundException $e) { |
69
|
|
|
$result = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a user in the database. |
77
|
|
|
* |
78
|
|
|
* @param UserEntity $entity |
79
|
|
|
* |
80
|
|
|
* @return UserEntity |
81
|
|
|
*/ |
82
|
|
|
public function create(UserEntity $entity) |
83
|
|
|
{ |
84
|
|
|
return $this->userRepository->create($entity); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Update a user in the database. |
89
|
|
|
* |
90
|
|
|
* @param UserEntity $entity |
91
|
|
|
* |
92
|
|
|
* @return UserEntity |
93
|
|
|
*/ |
94
|
|
|
public function update(UserEntity $entity) |
95
|
|
|
{ |
96
|
|
|
return $this->userRepository->update($entity); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Removes a user from the database. |
101
|
|
|
* |
102
|
|
|
* @param UserEntity $entity |
103
|
|
|
* |
104
|
|
|
* @return UserEntity |
105
|
|
|
*/ |
106
|
|
|
public function remove(UserEntity $entity) |
107
|
|
|
{ |
108
|
|
|
return $this->userRepository->remove($entity); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|