Total Complexity | 60 |
Total Lines | 420 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProfileManager 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 ProfileManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class ProfileManager { |
||
53 | /** @var IAccountManager */ |
||
54 | private $accountManager; |
||
55 | |||
56 | /** @var IAppManager */ |
||
57 | private $appManager; |
||
58 | |||
59 | /** @var IConfig */ |
||
60 | private $config; |
||
61 | |||
62 | /** @var ProfileConfigMapper */ |
||
63 | private $configMapper; |
||
64 | |||
65 | /** @var ContainerInterface */ |
||
66 | private $container; |
||
67 | |||
68 | /** @var KnownUserService */ |
||
69 | private $knownUserService; |
||
70 | |||
71 | /** @var IFactory */ |
||
72 | private $l10nFactory; |
||
73 | |||
74 | /** @var LoggerInterface */ |
||
75 | private $logger; |
||
76 | |||
77 | /** @var Coordinator */ |
||
78 | private $coordinator; |
||
79 | |||
80 | /** @var ILinkAction[] */ |
||
81 | private $actions = []; |
||
82 | |||
83 | /** @var null|ILinkAction[] */ |
||
84 | private $sortedActions = null; |
||
85 | /** @var CappedMemoryCache<ProfileConfig> */ |
||
86 | private CappedMemoryCache $configCache; |
||
87 | |||
88 | private const CORE_APP_ID = 'core'; |
||
89 | |||
90 | /** |
||
91 | * Array of account property actions |
||
92 | */ |
||
93 | private const ACCOUNT_PROPERTY_ACTIONS = [ |
||
94 | EmailAction::class, |
||
95 | PhoneAction::class, |
||
96 | WebsiteAction::class, |
||
97 | TwitterAction::class, |
||
98 | FediverseAction::class, |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * Array of account properties displayed on the profile |
||
103 | */ |
||
104 | private const PROFILE_PROPERTIES = [ |
||
105 | IAccountManager::PROPERTY_ADDRESS, |
||
106 | IAccountManager::PROPERTY_AVATAR, |
||
107 | IAccountManager::PROPERTY_BIOGRAPHY, |
||
108 | IAccountManager::PROPERTY_DISPLAYNAME, |
||
109 | IAccountManager::PROPERTY_HEADLINE, |
||
110 | IAccountManager::PROPERTY_ORGANISATION, |
||
111 | IAccountManager::PROPERTY_ROLE, |
||
112 | ]; |
||
113 | |||
114 | public function __construct( |
||
115 | IAccountManager $accountManager, |
||
116 | IAppManager $appManager, |
||
117 | IConfig $config, |
||
118 | ProfileConfigMapper $configMapper, |
||
119 | ContainerInterface $container, |
||
120 | KnownUserService $knownUserService, |
||
121 | IFactory $l10nFactory, |
||
122 | LoggerInterface $logger, |
||
123 | Coordinator $coordinator |
||
124 | ) { |
||
125 | $this->accountManager = $accountManager; |
||
126 | $this->appManager = $appManager; |
||
127 | $this->config = $config; |
||
128 | $this->configMapper = $configMapper; |
||
129 | $this->container = $container; |
||
130 | $this->knownUserService = $knownUserService; |
||
131 | $this->l10nFactory = $l10nFactory; |
||
132 | $this->logger = $logger; |
||
133 | $this->coordinator = $coordinator; |
||
134 | $this->configCache = new CappedMemoryCache(); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * If no user is passed as an argument return whether profile is enabled globally in `config.php` |
||
139 | */ |
||
140 | public function isProfileEnabled(?IUser $user = null): ?bool { |
||
141 | $profileEnabledGlobally = $this->config->getSystemValueBool('profile.enabled', true); |
||
142 | |||
143 | if (empty($user) || !$profileEnabledGlobally) { |
||
144 | return $profileEnabledGlobally; |
||
145 | } |
||
146 | |||
147 | $account = $this->accountManager->getAccount($user); |
||
148 | return filter_var( |
||
149 | $account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue(), |
||
150 | FILTER_VALIDATE_BOOLEAN, |
||
151 | FILTER_NULL_ON_FAILURE, |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Register an action for the user |
||
157 | */ |
||
158 | private function registerAction(ILinkAction $action, IUser $targetUser, ?IUser $visitingUser): void { |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Return an array of registered profile actions for the user |
||
193 | * |
||
194 | * @return ILinkAction[] |
||
195 | */ |
||
196 | private function getActions(IUser $targetUser, ?IUser $visitingUser): array { |
||
197 | // If actions are already registered and sorted, return them |
||
198 | if ($this->sortedActions !== null) { |
||
199 | return $this->sortedActions; |
||
200 | } |
||
201 | |||
202 | foreach (self::ACCOUNT_PROPERTY_ACTIONS as $actionClass) { |
||
203 | /** @var ILinkAction $action */ |
||
204 | $action = $this->container->get($actionClass); |
||
205 | $this->registerAction($action, $targetUser, $visitingUser); |
||
206 | } |
||
207 | |||
208 | $context = $this->coordinator->getRegistrationContext(); |
||
209 | |||
210 | if ($context !== null) { |
||
211 | foreach ($context->getProfileLinkActions() as $registration) { |
||
212 | /** @var ILinkAction $action */ |
||
213 | $action = $this->container->get($registration->getService()); |
||
214 | $this->registerAction($action, $targetUser, $visitingUser); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | $actionsClone = $this->actions; |
||
219 | // Sort associative array into indexed array in ascending order of priority |
||
220 | usort($actionsClone, function (ILinkAction $a, ILinkAction $b) { |
||
221 | return $a->getPriority() === $b->getPriority() ? 0 : ($a->getPriority() < $b->getPriority() ? -1 : 1); |
||
222 | }); |
||
223 | |||
224 | $this->sortedActions = $actionsClone; |
||
225 | return $this->sortedActions; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Return whether the profile parameter of the target user |
||
230 | * is visible to the visiting user |
||
231 | */ |
||
232 | private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $visitingUser): bool { |
||
233 | try { |
||
234 | $account = $this->accountManager->getAccount($targetUser); |
||
235 | $scope = $account->getProperty($paramId)->getScope(); |
||
236 | } catch (PropertyDoesNotExistException $e) { |
||
237 | // Allow the exception as not all profile parameters are account properties |
||
238 | } |
||
239 | |||
240 | $visibility = $this->getProfileConfig($targetUser, $visitingUser)[$paramId]['visibility']; |
||
241 | // Handle profile visibility and account property scope |
||
242 | switch ($visibility) { |
||
243 | case ProfileConfig::VISIBILITY_HIDE: |
||
244 | return false; |
||
245 | case ProfileConfig::VISIBILITY_SHOW_USERS_ONLY: |
||
246 | if (!empty($scope)) { |
||
247 | switch ($scope) { |
||
248 | case IAccountManager::SCOPE_PRIVATE: |
||
249 | return $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()); |
||
250 | case IAccountManager::SCOPE_LOCAL: |
||
251 | case IAccountManager::SCOPE_FEDERATED: |
||
252 | case IAccountManager::SCOPE_PUBLISHED: |
||
253 | return $visitingUser !== null; |
||
254 | default: |
||
255 | return false; |
||
256 | } |
||
257 | } |
||
258 | return $visitingUser !== null; |
||
259 | case ProfileConfig::VISIBILITY_SHOW: |
||
260 | if (!empty($scope)) { |
||
261 | switch ($scope) { |
||
262 | case IAccountManager::SCOPE_PRIVATE: |
||
263 | return $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()); |
||
264 | case IAccountManager::SCOPE_LOCAL: |
||
265 | case IAccountManager::SCOPE_FEDERATED: |
||
266 | case IAccountManager::SCOPE_PUBLISHED: |
||
267 | return true; |
||
268 | default: |
||
269 | return false; |
||
270 | } |
||
271 | } |
||
272 | return true; |
||
273 | default: |
||
274 | return false; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Return the profile parameters of the target user that are visible to the visiting user |
||
280 | * in an associative array |
||
281 | */ |
||
282 | public function getProfileParams(IUser $targetUser, ?IUser $visitingUser): array { |
||
283 | $account = $this->accountManager->getAccount($targetUser); |
||
284 | |||
285 | // Initialize associative array of profile parameters |
||
286 | $profileParameters = [ |
||
287 | 'userId' => $account->getUser()->getUID(), |
||
288 | ]; |
||
289 | |||
290 | // Add account properties |
||
291 | foreach (self::PROFILE_PROPERTIES as $property) { |
||
292 | switch ($property) { |
||
293 | case IAccountManager::PROPERTY_ADDRESS: |
||
294 | case IAccountManager::PROPERTY_BIOGRAPHY: |
||
295 | case IAccountManager::PROPERTY_DISPLAYNAME: |
||
296 | case IAccountManager::PROPERTY_HEADLINE: |
||
297 | case IAccountManager::PROPERTY_ORGANISATION: |
||
298 | case IAccountManager::PROPERTY_ROLE: |
||
299 | $profileParameters[$property] = |
||
300 | $this->isParameterVisible($property, $targetUser, $visitingUser) |
||
301 | // Explicitly set to null when value is empty string |
||
302 | ? ($account->getProperty($property)->getValue() ?: null) |
||
303 | : null; |
||
304 | break; |
||
305 | case IAccountManager::PROPERTY_AVATAR: |
||
306 | // Add avatar visibility |
||
307 | $profileParameters['isUserAvatarVisible'] = $this->isParameterVisible($property, $targetUser, $visitingUser); |
||
308 | break; |
||
309 | } |
||
310 | } |
||
311 | |||
312 | // Add actions |
||
313 | $profileParameters['actions'] = array_map( |
||
314 | function (ILinkAction $action) { |
||
315 | return [ |
||
316 | 'id' => $action->getId(), |
||
317 | 'icon' => $action->getIcon(), |
||
318 | 'title' => $action->getTitle(), |
||
319 | 'target' => $action->getTarget(), |
||
320 | ]; |
||
321 | }, |
||
322 | // This is needed to reindex the array after filtering |
||
323 | array_values( |
||
324 | array_filter( |
||
325 | $this->getActions($targetUser, $visitingUser), |
||
326 | function (ILinkAction $action) use ($targetUser, $visitingUser) { |
||
327 | return $this->isParameterVisible($action->getId(), $targetUser, $visitingUser); |
||
328 | } |
||
329 | ), |
||
330 | ) |
||
331 | ); |
||
332 | |||
333 | return $profileParameters; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Return the filtered profile config containing only |
||
338 | * the properties to be stored on the database |
||
339 | */ |
||
340 | private function filterNotStoredProfileConfig(array $profileConfig): array { |
||
341 | $dbParamConfigProperties = [ |
||
342 | 'visibility', |
||
343 | ]; |
||
344 | |||
345 | foreach ($profileConfig as $paramId => $paramConfig) { |
||
346 | $profileConfig[$paramId] = array_intersect_key($paramConfig, array_flip($dbParamConfigProperties)); |
||
347 | } |
||
348 | |||
349 | return $profileConfig; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Return the default profile config |
||
354 | */ |
||
355 | private function getDefaultProfileConfig(IUser $targetUser, ?IUser $visitingUser): array { |
||
356 | // Construct the default config for actions |
||
357 | $actionsConfig = []; |
||
358 | foreach ($this->getActions($targetUser, $visitingUser) as $action) { |
||
359 | $actionsConfig[$action->getId()] = ['visibility' => ProfileConfig::DEFAULT_VISIBILITY]; |
||
360 | } |
||
361 | |||
362 | // Construct the default config for account properties |
||
363 | $propertiesConfig = []; |
||
364 | foreach (ProfileConfig::DEFAULT_PROPERTY_VISIBILITY as $property => $visibility) { |
||
365 | $propertiesConfig[$property] = ['visibility' => $visibility]; |
||
366 | } |
||
367 | |||
368 | return array_merge($actionsConfig, $propertiesConfig); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Return the profile config of the target user, |
||
373 | * if a config does not already exist a default config is created and returned |
||
374 | */ |
||
375 | public function getProfileConfig(IUser $targetUser, ?IUser $visitingUser): array { |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Return the profile config of the target user with additional medatata, |
||
403 | * if a config does not already exist a default config is created and returned |
||
404 | */ |
||
405 | public function getProfileConfigWithMetadata(IUser $targetUser, ?IUser $visitingUser): array { |
||
472 | } |
||
473 | } |
||
474 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.