|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Service; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Code for creating new identities |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Palladium\Mapper\Authentication as Mapper; |
|
10
|
|
|
use Palladium\Entity\Authentication as Entity; |
|
11
|
|
|
use Palladium\Exception\IdentityDuplicated; |
|
12
|
|
|
use Palladium\Exception\UserNotFound; |
|
13
|
|
|
use Palladium\Exception\IdentityNotFound; |
|
14
|
|
|
use Palladium\Exception\TokenNotFound; |
|
15
|
|
|
|
|
16
|
|
|
use Palladium\Contract\CanCreateMapper; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class Registration |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
private $mapperFactory; |
|
23
|
|
|
private $logger; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->mapperFactory = $mapperFactory; |
|
29
|
|
|
$this->logger = $logger; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function createEmailIdentity($identifier, $password) |
|
34
|
|
|
{ |
|
35
|
|
|
$identity = new Entity\EmailIdentity; |
|
36
|
|
|
|
|
37
|
|
|
$identity->setIdentifier($identifier); |
|
38
|
|
|
$identity->setPassword($password); |
|
39
|
|
|
|
|
40
|
|
|
$this->prepareNewIdentity($identity); |
|
41
|
|
|
|
|
42
|
|
|
$identity->validate(); |
|
43
|
|
|
|
|
44
|
|
|
$mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class); |
|
45
|
|
|
|
|
46
|
|
|
if ($mapper->exists($identity)) { |
|
47
|
|
|
$this->logger->warning('email already registered', [ |
|
48
|
|
|
'input' => [ |
|
49
|
|
|
'identifier' => $identifier, |
|
50
|
|
|
], |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
throw new IdentityDuplicated; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$mapper->store($identity); |
|
57
|
|
|
|
|
58
|
|
|
// process not ended, no point in logging |
|
59
|
|
|
|
|
60
|
|
|
return $identity; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public function prepareNewIdentity(Entity\EmailIdentity $identity) |
|
65
|
|
|
{ |
|
66
|
|
|
$identity->setStatus(Identity::STATUS_NEW); |
|
67
|
|
|
|
|
68
|
|
|
$identity->generateToken(); |
|
69
|
|
|
$identity->setTokenAction(Identity::ACTION_VERIFY); |
|
70
|
|
|
$identity->setTokenEndOfLife(time() + Identity::TOKEN_LIFESPAN); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
public function bindIdentityToUser(Identity $identity, HasId $user) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($user->getId() === null) { |
|
77
|
|
|
throw new UserNotFound; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$identity->setUserId($user->getId()); |
|
81
|
|
|
|
|
82
|
|
|
$identity->validate(); |
|
83
|
|
|
|
|
84
|
|
|
$mapper = $this->mapperFactory->create(Mapper\IdentityUser::class); |
|
85
|
|
|
$mapper->store($identity); |
|
86
|
|
|
|
|
87
|
|
|
$this->logger->info('new identity registered', [ |
|
88
|
|
|
'input' => [ |
|
89
|
|
|
'identifier' => $identity->getIdentifier(), |
|
90
|
|
|
], |
|
91
|
|
|
'account' => [ |
|
92
|
|
|
'user' => $identity->getUserId(), |
|
93
|
|
|
'identity' => $identity->getId(), |
|
94
|
|
|
], |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
// @TODO: add mail later |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
public function verifyEmailIdentity($token) |
|
102
|
|
|
{ |
|
103
|
|
|
$identity = new Entity\EmailIdentity; |
|
104
|
|
|
$this->retrieveIdenityByToken($identity, $token, Identity::ACTION_VERIFY); |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
if ($identity->getId() === null) { |
|
|
|
|
|
|
107
|
|
|
$this->logger->warning('no identity with given verification token', [ |
|
108
|
|
|
'input' => [ |
|
109
|
|
|
'token' => $token, |
|
110
|
|
|
], |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
throw new TokenNotFound; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$identity->setStatus(Identity::STATUS_ACTIVE); |
|
117
|
|
|
$identity->clearToken(); |
|
118
|
|
|
|
|
119
|
|
|
$mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class); |
|
120
|
|
|
$mapper->store($identity); |
|
121
|
|
|
|
|
122
|
|
|
$this->logger->info('identity verified', [ |
|
123
|
|
|
'input' => [ |
|
124
|
|
|
'token' => $token, |
|
125
|
|
|
], |
|
126
|
|
|
'account' => [ |
|
127
|
|
|
'user' => $identity->getUserId(), |
|
128
|
|
|
'identity' => $identity->getId(), |
|
129
|
|
|
], |
|
130
|
|
|
]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
View Code Duplication |
private function retrieveIdenityByToken(Entity\Identity $identity, $token, $action = Entity\Identity::ACTION_ANY) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
$identity->setToken($token); |
|
137
|
|
|
$identity->setTokenAction($action); |
|
138
|
|
|
$identity->setTokenEndOfLife(time()); |
|
139
|
|
|
|
|
140
|
|
|
$mapper = $this->mapperFactory->create(Mapper\Identity::class); |
|
141
|
|
|
$mapper->fetch($identity); |
|
142
|
|
|
|
|
143
|
|
|
return $identity; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.