|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\DI; |
|
6
|
|
|
|
|
7
|
|
|
use Kdyby; |
|
8
|
|
|
use Nette; |
|
9
|
|
|
use SixtyEightPublishers; |
|
10
|
|
|
|
|
11
|
|
|
final class ForgotPasswordExtensionAdapter extends SixtyEightPublishers\User\DI\AbstractExtensionAdapter implements |
|
12
|
|
|
Kdyby\Doctrine\DI\IEntityProvider, |
|
13
|
|
|
Kdyby\Doctrine\DI\ITargetEntityProvider, |
|
14
|
|
|
Kdyby\Translation\DI\ITranslationProvider |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
protected static $defaults = [ |
|
18
|
|
|
'enabled' => FALSE, |
|
19
|
|
|
'register_controls' => FALSE, |
|
20
|
|
|
'request_expiration' => SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\PasswordRequest::DEFAULT_EXPIRATION, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function processConfig(array $config, \ArrayObject $sharedData): array |
|
27
|
|
|
{ |
|
28
|
|
|
Nette\Utils\Validators::assertField($config, 'enabled', 'bool'); |
|
29
|
|
|
Nette\Utils\Validators::assertField($config, 'register_controls', 'bool'); |
|
30
|
|
|
Nette\Utils\Validators::assertField($config, 'request_expiration', 'string|int'); |
|
31
|
|
|
|
|
32
|
|
|
if (FALSE === $config['enabled']) { |
|
33
|
|
|
$this->stopPropagation(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
if (!is_subclass_of( |
|
|
|
|
|
|
37
|
|
|
$sharedData[SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::SHARED_DATA_USER_CLASS_NAME], |
|
38
|
|
|
SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IUser::class, |
|
39
|
|
|
TRUE |
|
40
|
|
|
) |
|
41
|
|
|
) { |
|
42
|
|
|
throw new SixtyEightPublishers\User\Common\Exception\ConfigurationException(sprintf( |
|
43
|
|
|
'Your User entity must implement interface %s', |
|
44
|
|
|
SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IUser::class |
|
45
|
|
|
)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $config; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function loadConfiguration(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$config = $this->getConfig(); |
|
57
|
|
|
$builder = $this->getContainerBuilder(); |
|
58
|
|
|
|
|
59
|
|
|
# services |
|
60
|
|
|
$builder->addDefinition($this->prefix('password_request_factory')) |
|
61
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestFactory::class) |
|
62
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestFactory::class); |
|
63
|
|
|
|
|
64
|
|
|
$builder->addDefinition($this->prefix('password_request_manager')) |
|
65
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestManager::class) |
|
66
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManager::class); |
|
67
|
|
|
|
|
68
|
|
|
$builder->addDefinition($this->prefix('password_request_sender')) |
|
69
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\IPasswordRequestSender::class) |
|
70
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestSender::class); |
|
71
|
|
|
|
|
72
|
|
|
# controls |
|
73
|
|
|
if (TRUE === $config['register_controls']) { |
|
74
|
|
|
$builder->addDefinition($this->prefix('control.forgot_password')) |
|
75
|
|
|
->setImplement(SixtyEightPublishers\User\ForgotPassword\Control\ForgotPassword\IForgotPasswordControlFactory::class) |
|
76
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Control\ForgotPassword\ForgotPasswordControl::class); |
|
77
|
|
|
|
|
78
|
|
|
$builder->addDefinition($this->prefix('control.reset_password')) |
|
79
|
|
|
->setImplement(SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword\IResetPasswordControlFactory::class) |
|
80
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword\ResetPasswordControl::class); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
# emails |
|
84
|
|
|
$builder->addDefinition($this->prefix('email.forgot_password_not_registered')) |
|
85
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\Mail\IForgotPasswordNotRegisteredEmail::class) |
|
86
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Mail\ForgotPasswordNotRegisteredEmail::class); |
|
87
|
|
|
|
|
88
|
|
|
$builder->addDefinition($this->prefix('email.forgot_password_reset')) |
|
89
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\Mail\IForgotPasswordResetEmail::class) |
|
90
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Mail\ForgotPasswordResetEmail::class); |
|
91
|
|
|
|
|
92
|
|
|
$builder->addDefinition($this->prefix('email.password_has_been_reset')) |
|
93
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\Mail\IPasswordHasBeenResetEmail::class) |
|
94
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmail::class); |
|
95
|
|
|
|
|
96
|
|
|
# queries |
|
97
|
|
|
$builder->addDefinition($this->prefix('query_factory.cancel_password_request_by_user')) |
|
98
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\Query\ICancelPasswordRequestsByUserQueryFactory::class) |
|
99
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Query\CancelPasswordRequestsByUserQueryFactory::class); |
|
100
|
|
|
|
|
101
|
|
|
$builder->addDefinition($this->prefix('query_factory.find_password_request_by_ids')) |
|
102
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory::class) |
|
103
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Query\FindPasswordRequestByIdsQueryFactory::class); |
|
104
|
|
|
|
|
105
|
|
|
$builder->addDefinition($this->prefix('query_factory.get_user_by_email')) |
|
106
|
|
|
->setType(SixtyEightPublishers\User\ForgotPassword\Query\IGetUserByEmailQueryFactory::class) |
|
107
|
|
|
->setFactory(SixtyEightPublishers\User\ForgotPassword\Query\GetUserByEmailQueryFactory::class); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function afterCompile(Nette\PhpGenerator\ClassType $class): void |
|
114
|
|
|
{ |
|
115
|
|
|
$config = $this->getConfig(); |
|
116
|
|
|
$initialize = $class->getMethod('initialize'); |
|
117
|
|
|
|
|
118
|
|
|
$initialize->addBody('?::setExpirationString(?);', [ |
|
119
|
|
|
new Nette\PhpGenerator\PhpLiteral(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\PasswordRequest::class), |
|
120
|
|
|
(string) $config['request_expiration'], |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/**************** interface \Kdyby\Doctrine\DI\IEntityProvider ****************/ |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getEntityMappings(): array |
|
130
|
|
|
{ |
|
131
|
|
|
return [ |
|
132
|
|
|
'SixtyEightPublishers\User\ForgotPassword\DoctrineEntity' => __DIR__ . '/../DoctrineEntity', |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/ |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getTargetEntityMappings(): array |
|
142
|
|
|
{ |
|
143
|
|
|
return [ |
|
144
|
|
|
SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest::class => SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\PasswordRequest::class, |
|
145
|
|
|
SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IUser::class => $this->getSharedData(SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::SHARED_DATA_USER_CLASS_NAME), |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/**************** interface \Kdyby\Translation\DI\ITranslationProvider ****************/ |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
View Code Duplication |
public function getTranslationResources(): array |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
$config = $this->getConfig(); |
|
157
|
|
|
|
|
158
|
|
|
return TRUE === $config['register_controls'] |
|
159
|
|
|
? [ __DIR__ . '/../locale' ] |
|
160
|
|
|
: []; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
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.