Total Complexity | 42 |
Total Lines | 261 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | final class User implements ModuleConfigurationInterface |
||
38 | { |
||
39 | public function addConfig(NodeBuilder $nodeBuilder): void |
||
40 | { |
||
41 | $nodeBuilder |
||
42 | ->arrayNode('user') |
||
43 | ->children() |
||
44 | ->booleanNode('enabled')->defaultValue(false)->end() |
||
45 | ->scalarNode('user_class')->end() |
||
|
|||
46 | ->booleanNode('email_confirmation')->defaultValue(true)->end() |
||
47 | ->booleanNode('signed_in_after_signup')->defaultValue(false)->end() |
||
48 | ->booleanNode('user_invites_enabled')->defaultValue(false)->end() |
||
49 | ->scalarNode('login_route')->defaultValue('parthenon_user_login')->end() |
||
50 | ->scalarNode('login_redirect_route')->defaultValue('parthenon_user_profile')->end() |
||
51 | ->scalarNode('signup_success_route')->defaultValue('parthenon_user_signed_up')->end() |
||
52 | ->booleanNode('teams_enabled')->defaultValue(false)->end() |
||
53 | ->booleanNode('teams_invites_enabled')->defaultValue(false)->end() |
||
54 | ->booleanNode('self_signup_enabled')->defaultValue(true)->end() |
||
55 | ->scalarNode('team_class')->end() |
||
56 | ->scalarNode('firewall_name')->end() |
||
57 | ->arrayNode('roles') |
||
58 | ->children() |
||
59 | ->scalarNode('default_role')->defaultValue('ROLE_USER')->end() |
||
60 | ->arrayNode('user_assignable') |
||
61 | ->useAttributeAsKey('name') |
||
62 | ->scalarPrototype()->end() |
||
63 | ->end() |
||
64 | ->arrayNode('athena_assignable') |
||
65 | ->useAttributeAsKey('name') |
||
66 | ->scalarPrototype()->end() |
||
67 | ->end() |
||
68 | ->end() |
||
69 | ->end() |
||
70 | ->arrayNode('gdpr') |
||
71 | ->children() |
||
72 | ->arrayNode('export') |
||
73 | ->children() |
||
74 | ->scalarNode('export_format')->end() |
||
75 | ->end() |
||
76 | ->end() |
||
77 | ->end() |
||
78 | ->end() |
||
79 | ->end() |
||
80 | ->end(); |
||
81 | } |
||
82 | |||
83 | public function handleDefaultParameters(ContainerBuilder $container): void |
||
84 | { |
||
85 | $container->setParameter('parthenon_user_login_route', 'parthenon_user_login'); |
||
86 | $container->setParameter('parthenon_user_signup_success_route', 'parthenon_user_signed_up'); |
||
87 | $container->setParameter('parthenon_user_users_invites_enabled', false); |
||
88 | $container->setParameter('parthenon_user_teams_enabled', false); |
||
89 | $container->setParameter('parthenon_user_team_class', null); |
||
90 | $container->setParameter('parthenon_user_teams_invites_enabled', false); |
||
91 | $container->setParameter('parthenon_user_gdpr_formatter_type', 'json'); |
||
92 | $container->setParameter('parthenon_user_roles_default_role', 'ROLE_USER'); |
||
93 | $container->setParameter('parthenon_user_roles_user_assignable_roles', []); |
||
94 | $container->setParameter('parthenon_user_roles_athena_assignable_roles', []); |
||
95 | $container->setParameter('parthenon_user_self_signup_enabled', true); |
||
96 | $container->setParameter('parthenon_user_email_confirmation', true); |
||
97 | $container->setParameter('parthenon_user_signed_in_after_signup', false); |
||
98 | $container->setParameter('parthenon_user_firewall_name', 'main'); |
||
99 | } |
||
100 | |||
101 | public function handleConfiguration(array $config, ContainerBuilder $container): void |
||
102 | { |
||
103 | if (!isset($config['user']) || !isset($config['user']['enabled']) || false == $config['user']['enabled']) { |
||
104 | return; |
||
105 | } |
||
106 | $container->setParameter('parthenon_user_enabled', true); |
||
107 | |||
108 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config')); |
||
109 | $bundles = $container->getParameter('kernel.bundles'); |
||
110 | |||
111 | $this->configureMongoDb($bundles, $loader); |
||
112 | $this->configureDoctrine($bundles, $loader); |
||
113 | |||
114 | $loader->load('services/user.xml'); |
||
115 | |||
116 | $this->configureAutotagging($container); |
||
117 | $config = $this->configureUserClass($config, $container); |
||
118 | $config = $this->configureSignupSuccessRoute($config, $container); |
||
119 | $config = $this->configureLoginRoute($config, $container); |
||
120 | $config = $this->configureUserInvitesEnabled($config, $container); |
||
121 | $config = $this->configureTeamsInviteEnabled($config, $container); |
||
122 | $config = $this->configureGdprFormatterType($config, $container); |
||
123 | $config = $this->configureRoles($config, $container); |
||
124 | $config = $this->configureSelfSignup($config, $container); |
||
125 | $config = $this->configureEmailConfirmation($config, $container); |
||
126 | $config = $this->configureSignedInAfterSignup($config, $container); |
||
127 | $config = $this->configureFirewall($config, $container); |
||
128 | |||
129 | $this->configureTeams($config, $container); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @throws NonExistentClass |
||
134 | * @throws ParameterNotSetException |
||
135 | */ |
||
136 | private function configureTeams(array $config, ContainerBuilder $container): void |
||
137 | { |
||
138 | if (isset($config['user']['teams_enabled']) && $config['user']['teams_enabled']) { |
||
139 | $container->setParameter('parthenon_user_teams_enabled_flag', true); |
||
140 | $container->setParameter('parthenon_user_teams_enabled', $config['user']['teams_enabled']); |
||
141 | if (!isset($config['user']['team_class']) || empty($config['user']['team_class'])) { |
||
142 | throw new ParameterNotSetException('When the user module is enabled and teams are enabled the team_class must be defined'); |
||
143 | } |
||
144 | |||
145 | if (!class_exists($config['user']['team_class'])) { |
||
146 | throw new NonExistentClass(sprintf("The class '%s' does not exist.", $config['user']['team_class'])); |
||
147 | } |
||
148 | |||
149 | $teamDdfinition = $container->getDefinition(TeamInterface::class); |
||
150 | $teamDdfinition->setClass($config['user']['team_class']); |
||
151 | $container->setDefinition(TeamInterface::class, $teamDdfinition); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | private function configureGdprFormatterType(array $config, ContainerBuilder $container): array |
||
156 | { |
||
157 | if (isset($config['user']['gdpr']['export']['formatter_type']) && !empty($config['user']['gdpr']['export']['formatter_type'])) { |
||
158 | $container->setParameter('parthenon_user_gdpr_formatter_type', $config['user']['gdpr']['export']['formatter_type']); |
||
159 | } |
||
160 | |||
161 | return $config; |
||
162 | } |
||
163 | |||
164 | private function configureTeamsInviteEnabled(array $config, ContainerBuilder $container): array |
||
165 | { |
||
166 | if (isset($config['user']['teams_invites_enabled'])) { |
||
167 | $container->setParameter('parthenon_user_teams_invites_enabled', $config['user']['teams_invites_enabled']); |
||
168 | } |
||
169 | |||
170 | return $config; |
||
171 | } |
||
172 | |||
173 | private function configureUserInvitesEnabled(array $config, ContainerBuilder $container): array |
||
174 | { |
||
175 | if (isset($config['user']['user_invites_enabled'])) { |
||
176 | $container->setParameter('parthenon_user_users_invites_enabled', $config['user']['user_invites_enabled']); |
||
177 | } |
||
178 | |||
179 | return $config; |
||
180 | } |
||
181 | |||
182 | private function configureRoles(array $config, ContainerBuilder $containerBuilder): array |
||
183 | { |
||
184 | if (!isset($config['user']['roles'])) { |
||
185 | return $config; |
||
186 | } |
||
187 | |||
188 | $containerBuilder->setParameter('parthenon_user_roles_default_role', $config['user']['roles']['default_role'] ?? 'ROLE_USER'); |
||
189 | $containerBuilder->setParameter('parthenon_user_roles_user_assignable_roles', $config['user']['roles']['user_assignable'] ?? []); |
||
190 | $containerBuilder->setParameter('parthenon_user_roles_athena_assignable_roles', $config['user']['roles']['athena_assignable'] ?? []); |
||
191 | |||
192 | return $config; |
||
193 | } |
||
194 | |||
195 | private function configureLoginRoute(array $config, ContainerBuilder $container): array |
||
196 | { |
||
197 | if (isset($config['user']['login_route'])) { |
||
198 | $container->setParameter('parthenon_user_login_route', $config['user']['login_route']); |
||
199 | } |
||
200 | |||
201 | return $config; |
||
202 | } |
||
203 | |||
204 | private function configureSignupSuccessRoute(array $config, ContainerBuilder $container): array |
||
205 | { |
||
206 | if (isset($config['user']['signup_success_route'])) { |
||
207 | $container->setParameter('parthenon_user_signup_success_route', $config['user']['signup_success_route']); |
||
208 | } |
||
209 | |||
210 | return $config; |
||
211 | } |
||
212 | |||
213 | private function configureEmailConfirmation(array $config, ContainerBuilder $container): array |
||
214 | { |
||
215 | if (isset($config['user']['email_confirmation'])) { |
||
216 | $container->setParameter('parthenon_user_email_confirmation', $config['user']['email_confirmation']); |
||
217 | } |
||
218 | |||
219 | return $config; |
||
220 | } |
||
221 | |||
222 | private function configureFirewall(array $config, ContainerBuilder $container): array |
||
223 | { |
||
224 | if (isset($config['user']['firewall_name'])) { |
||
225 | $container->setParameter('parthenon_user_firewall_name', $config['user']['firewall_name']); |
||
226 | } |
||
227 | |||
228 | return $config; |
||
229 | } |
||
230 | |||
231 | private function configureSignedInAfterSignup(array $config, ContainerBuilder $container): array |
||
232 | { |
||
233 | if (isset($config['user']['signed_in_after_signup'])) { |
||
234 | $container->setParameter('parthenon_user_signed_in_after_signup', $config['user']['signed_in_after_signup']); |
||
235 | } |
||
236 | |||
237 | return $config; |
||
238 | } |
||
239 | |||
240 | private function configureSelfSignup(array $config, ContainerBuilder $container): array |
||
241 | { |
||
242 | if (isset($config['user']['self_signup_enabled'])) { |
||
243 | $container->setParameter('parthenon_user_self_signup_enabled', $config['user']['self_signup_enabled']); |
||
244 | } |
||
245 | |||
246 | return $config; |
||
247 | } |
||
248 | |||
249 | private function configureAutotagging(ContainerBuilder $container): void |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @throws \Exception |
||
259 | */ |
||
260 | private function configureDoctrine(float|array|bool|int|string|null $bundles, XmlFileLoader $loader): string|int|bool|array|float|null |
||
261 | { |
||
262 | if (isset($bundles['DoctrineBundle'])) { |
||
263 | $loader->load('services/orm/user.xml'); |
||
264 | } |
||
265 | |||
266 | return $bundles; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @throws \Exception |
||
271 | */ |
||
272 | private function configureMongoDb(float|int|bool|array|string|null $bundles, XmlFileLoader $loader): void |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @throws NonExistentClass |
||
281 | * @throws ParameterNotSetException |
||
282 | */ |
||
283 | private function configureUserClass(array $config, ContainerBuilder $container): array |
||
284 | { |
||
285 | if (!isset($config['user']['user_class']) || empty($config['user']['user_class'])) { |
||
286 | throw new ParameterNotSetException('When the user module is enabled the user_class must be defined'); |
||
298 | } |
||
299 | } |
||
300 |