1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\DI; |
6
|
|
|
|
7
|
|
|
use Nette\Schema\Expect; |
8
|
|
|
use Nette\Schema\Schema; |
9
|
|
|
use Nette\PhpGenerator\ClassType; |
10
|
|
|
use Nette\PhpGenerator\PhpLiteral; |
11
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntity; |
12
|
|
|
use SixtyEightPublishers\User\Common\DI\CommonExtension; |
13
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\EntityMapping; |
14
|
|
|
use SixtyEightPublishers\User\DI\AbstractCompilerExtensionPass; |
15
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Entity\UserInterface; |
16
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequest; |
17
|
|
|
use SixtyEightPublishers\User\Common\Exception\ConfigurationException; |
18
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntityProviderInterface; |
19
|
|
|
use SixtyEightPublishers\DoctrineBridge\DI\EntityMappingProviderInterface; |
20
|
|
|
use SixtyEightPublishers\TranslationBridge\DI\TranslationProviderInterface; |
21
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\ForgotPasswordResetEmail; |
22
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmail; |
23
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface; |
24
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\GetUserByEmailQueryObject; |
25
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\ForgotPasswordNotRegisteredEmail; |
26
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestSender; |
27
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\ForgotPasswordResetEmailInterface; |
28
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestFactory; |
29
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManager; |
30
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface; |
31
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\FindPasswordRequestByIdsQueryObject; |
32
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword\ResetPasswordControl; |
33
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Control\ForgotPassword\ForgotPasswordControl; |
34
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\CancelPasswordRequestsByUserQueryObject; |
35
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\ForgotPasswordNotRegisteredEmailInterface; |
36
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestSenderInterface; |
37
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestFactoryInterface; |
38
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface; |
39
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\GetUserByEmailQueryObjectFactoryInterface; |
40
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\FindPasswordRequestByIdsQueryObjectFactoryInterface; |
41
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword\ResetPasswordControlFactoryInterface; |
42
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Control\ForgotPassword\ForgotPasswordControlFactoryInterface; |
43
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Query\CancelPasswordRequestsByUserQueryObjectFactoryInterface; |
44
|
|
|
|
45
|
|
|
final class ForgotPasswordExtension extends AbstractCompilerExtensionPass implements EntityMappingProviderInterface, TargetEntityProviderInterface, TranslationProviderInterface |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function startup(): void |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
parent::startup(); |
53
|
|
|
|
54
|
|
|
if (!$this->config->enabled) { |
55
|
|
|
$this->stopPropagation(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!is_subclass_of($this->sharedData[CommonExtension::SHARED_DATA_USER_CLASS_NAME], UserInterface::class, TRUE)) { |
59
|
|
|
throw new ConfigurationException(sprintf( |
60
|
|
|
'Your User entity must implement interface %s', |
61
|
|
|
UserInterface::class |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function getConfigSchema(): Schema |
70
|
|
|
{ |
71
|
|
|
return Expect::structure([ |
72
|
|
|
'enabled' => Expect::bool(FALSE), |
73
|
|
|
'register_controls' => Expect::bool(FALSE), |
74
|
|
|
'request_expiration' => Expect::anyOf(Expect::string(), Expect::int())->default(PasswordRequest::DEFAULT_EXPIRATION), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function loadConfiguration(): void |
82
|
|
|
{ |
83
|
|
|
$builder = $this->getContainerBuilder(); |
84
|
|
|
|
85
|
|
|
$builder->addDefinition($this->prefix('password_request_factory')) |
86
|
|
|
->setType(PasswordRequestFactoryInterface::class) |
87
|
|
|
->setFactory(PasswordRequestFactory::class); |
88
|
|
|
|
89
|
|
|
$builder->addDefinition($this->prefix('password_request_manager')) |
90
|
|
|
->setType(PasswordRequestManagerInterface::class) |
91
|
|
|
->setFactory(PasswordRequestManager::class); |
92
|
|
|
|
93
|
|
|
$builder->addDefinition($this->prefix('password_request_sender')) |
94
|
|
|
->setType(PasswordRequestSenderInterface::class) |
95
|
|
|
->setFactory(PasswordRequestSender::class); |
96
|
|
|
|
97
|
|
|
if ($this->config->register_controls) { |
98
|
|
|
$builder->addFactoryDefinition($this->prefix('control.forgot_password')) |
99
|
|
|
->setImplement(ForgotPasswordControlFactoryInterface::class) |
100
|
|
|
->getResultDefinition() |
101
|
|
|
->setFactory(ForgotPasswordControl::class); |
102
|
|
|
|
103
|
|
|
$builder->addFactoryDefinition($this->prefix('control.reset_password')) |
104
|
|
|
->setImplement(ResetPasswordControlFactoryInterface::class) |
105
|
|
|
->getResultDefinition() |
106
|
|
|
->setFactory(ResetPasswordControl::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$builder->addDefinition($this->prefix('email.forgot_password_not_registered')) |
110
|
|
|
->setType(ForgotPasswordNotRegisteredEmailInterface::class) |
111
|
|
|
->setFactory(ForgotPasswordNotRegisteredEmail::class); |
112
|
|
|
|
113
|
|
|
$builder->addDefinition($this->prefix('email.forgot_password_reset')) |
114
|
|
|
->setType(ForgotPasswordResetEmailInterface::class) |
115
|
|
|
->setFactory(ForgotPasswordResetEmail::class); |
116
|
|
|
|
117
|
|
|
$builder->addDefinition($this->prefix('email.password_has_been_reset')) |
118
|
|
|
->setType(PasswordHasBeenResetEmailInterface::class) |
119
|
|
|
->setFactory(PasswordHasBeenResetEmail::class); |
120
|
|
|
|
121
|
|
|
$builder->addFactoryDefinition($this->prefix('query_object_factory.cancel_password_request_by_user')) |
122
|
|
|
->setImplement(CancelPasswordRequestsByUserQueryObjectFactoryInterface::class) |
123
|
|
|
->getResultDefinition() |
124
|
|
|
->setFactory(CancelPasswordRequestsByUserQueryObject::class); |
125
|
|
|
|
126
|
|
|
$builder->addFactoryDefinition($this->prefix('query_object_factory.find_password_request_by_ids')) |
127
|
|
|
->setImplement(FindPasswordRequestByIdsQueryObjectFactoryInterface::class) |
128
|
|
|
->getResultDefinition() |
129
|
|
|
->setFactory(FindPasswordRequestByIdsQueryObject::class); |
130
|
|
|
|
131
|
|
|
$builder->addFactoryDefinition($this->prefix('query_object_factory.get_user_by_email')) |
132
|
|
|
->setImplement(GetUserByEmailQueryObjectFactoryInterface::class) |
133
|
|
|
->getResultDefinition() |
134
|
|
|
->setFactory(GetUserByEmailQueryObject::class); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function afterCompile(ClassType $class): void |
141
|
|
|
{ |
142
|
|
|
$this->getInitialization()->addBody('?::setExpirationString(?);', [ |
143
|
|
|
new PhpLiteral(PasswordRequest::class), |
144
|
|
|
(string) $this->config->request_expiration, |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function getEntityMappings(): array |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
new EntityMapping(EntityMapping::DRIVER_ANNOTATIONS, 'SixtyEightPublishers\User\ForgotPassword\Entity', __DIR__ . '/../Entity'), |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function getTargetEntities(): array |
162
|
|
|
{ |
163
|
|
|
return [ |
164
|
|
|
new TargetEntity(PasswordRequestInterface::class, PasswordRequest::class), |
165
|
|
|
new TargetEntity(UserInterface::class, $this->sharedData[CommonExtension::SHARED_DATA_USER_CLASS_NAME]), |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function getTranslationResources(): array |
173
|
|
|
{ |
174
|
|
|
return $this->config->register_controls ? [ __DIR__ . '/../translations' ] : []; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.