|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Del\Service; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Del\Criteria\UserCriteria; |
|
7
|
|
|
use Del\Entity\Person; |
|
|
|
|
|
|
8
|
|
|
use Del\Entity\User as UserEntity; |
|
9
|
|
|
use Del\Exception\UserException; |
|
10
|
|
|
use Del\Repository\User as UserRepository; |
|
11
|
|
|
use Del\Service\Person as PersonService; |
|
12
|
|
|
use Del\Value\User\State; |
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
class User |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var EntityManager $em */ |
|
19
|
|
|
protected $em; |
|
20
|
|
|
|
|
21
|
|
|
/** @var PersonService */ |
|
22
|
|
|
private $personSvc; |
|
23
|
|
|
|
|
24
|
3 |
|
public function __construct(EntityManager $em, PersonService $personSvc) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
3 |
|
$this->em = $em; |
|
27
|
3 |
|
$this->personSvc = $personSvc; |
|
28
|
3 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $data |
|
32
|
|
|
* @return UserEntity |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function createFromArray(array $data) |
|
35
|
|
|
{ |
|
36
|
3 |
|
$user = new UserEntity(); |
|
37
|
3 |
|
$user->setPerson(new Person()); |
|
38
|
3 |
|
isset($data['id']) ? $user->setId($data['id']) : null; |
|
39
|
3 |
|
isset($data['email']) ? $user->setEmail($data['email']) : null; |
|
40
|
3 |
|
isset($data['password']) ? $user->setPassword($data['password']) : null; |
|
41
|
3 |
|
isset($data['state']) ? $user->setState(new State($data['state'])) : null; |
|
42
|
3 |
|
isset($data['registrationDate']) ? $user->setRegistrationDate(new DateTime($data['registrationDate'])) : null; |
|
43
|
3 |
|
isset($data['lastLogin']) ? $user->setLastLogin(new DateTime($data['lastLogin'])) : null; |
|
44
|
3 |
|
return $user; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function toArray(UserEntity $user) |
|
54
|
|
|
{ |
|
55
|
|
|
return array |
|
56
|
|
|
( |
|
57
|
1 |
|
'id' => $user->getID(), |
|
58
|
1 |
|
'email' => $user->getEmail(), |
|
59
|
1 |
|
'person' => $user->getPerson(), |
|
60
|
1 |
|
'password' => $user->getPassword(), |
|
61
|
1 |
|
'state' => $user->getState()->getValue(), |
|
62
|
1 |
|
'registrationDate' => is_null($user->getRegistrationDate()) ? null : $user->getRegistrationDate()->format('Y-m-d H:i:s'), |
|
63
|
1 |
|
'lastLoginDate' => is_null($user->getLastLoginDate()) ? null : $user->getLastLoginDate()->format('Y-m-d H:i:s'), |
|
64
|
1 |
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param UserEntity $user |
|
69
|
|
|
* @return UserEntity |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function saveUser(UserEntity $user) |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->getRepository()->save($user); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return UserRepository |
|
78
|
|
|
*/ |
|
79
|
1 |
|
protected function getRepository() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->em->getRepository('Del\Entity\User'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function registerUser(array $data) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!$data['email'] || !$data['password'] || !$data['confirm']) { |
|
87
|
|
|
throw new InvalidArgumentException(); |
|
88
|
|
|
} |
|
89
|
|
|
if ($data['password'] !== $data['confirm']) { |
|
90
|
|
|
throw new UserException(UserException::WRONG_PASSWORD); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$criteria = new UserCriteria(); |
|
94
|
|
|
$criteria->setEmail($data['email']); |
|
95
|
|
|
$user = $this->getRepository()->findByCriteria($criteria); |
|
96
|
|
|
if(!is_null($user)) { |
|
97
|
|
|
throw new UserException(UserException::USER_EXISTS); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$person = new Person(); |
|
101
|
|
|
$user = new UserEntity(); |
|
102
|
|
|
$state = new State(State::STATE_UNACTIVATED); |
|
103
|
|
|
$user->setPerson($person) |
|
104
|
|
|
->setEmail($data['email']) |
|
105
|
|
|
->setRegistrationDate(new DateTime()) |
|
106
|
|
|
->setState($state); |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: