| Total Complexity | 49 | 
| Total Lines | 353 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like Activity 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 Activity, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 34 | class Activity implements IProvider { | 
            ||
| 35 | |||
| 36 | /** @var IFactory */  | 
            ||
| 37 | protected $languageFactory;  | 
            ||
| 38 | |||
| 39 | /** @var IL10N */  | 
            ||
| 40 | protected $l;  | 
            ||
| 41 | |||
| 42 | /** @var IURLGenerator */  | 
            ||
| 43 | protected $url;  | 
            ||
| 44 | |||
| 45 | /** @var IManager */  | 
            ||
| 46 | protected $activityManager;  | 
            ||
| 47 | |||
| 48 | /** @var IUserManager */  | 
            ||
| 49 | protected $userManager;  | 
            ||
| 50 | /** @var IContactsManager */  | 
            ||
| 51 | protected $contactsManager;  | 
            ||
| 52 | |||
| 53 | /** @var array */  | 
            ||
| 54 | protected $displayNames = [];  | 
            ||
| 55 | |||
| 56 | /** @var array */  | 
            ||
| 57 | protected $contactNames = [];  | 
            ||
| 58 | |||
| 59 | const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';  | 
            ||
| 60 | const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';  | 
            ||
| 61 | const SUBJECT_SHARED_EMAIL_PASSWORD_SEND = 'shared_with_email_password_send';  | 
            ||
| 62 | const SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF = 'shared_with_email_password_send_self';  | 
            ||
| 63 | const SUBJECT_UNSHARED_EMAIL_SELF = 'unshared_with_email_self';  | 
            ||
| 64 | const SUBJECT_UNSHARED_EMAIL_BY = 'unshared_with_email_by';  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * @param IFactory $languageFactory  | 
            ||
| 68 | * @param IURLGenerator $url  | 
            ||
| 69 | * @param IManager $activityManager  | 
            ||
| 70 | * @param IUserManager $userManager  | 
            ||
| 71 | * @param IContactsManager $contactsManager  | 
            ||
| 72 | */  | 
            ||
| 73 | 	public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) { | 
            ||
| 74 | $this->languageFactory = $languageFactory;  | 
            ||
| 75 | $this->url = $url;  | 
            ||
| 76 | $this->activityManager = $activityManager;  | 
            ||
| 77 | $this->userManager = $userManager;  | 
            ||
| 78 | $this->contactsManager = $contactsManager;  | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * @param string $language  | 
            ||
| 83 | * @param IEvent $event  | 
            ||
| 84 | * @param IEvent|null $previousEvent  | 
            ||
| 85 | * @return IEvent  | 
            ||
| 86 | * @throws \InvalidArgumentException  | 
            ||
| 87 | * @since 11.0.0  | 
            ||
| 88 | */  | 
            ||
| 89 | 	public function parse($language, IEvent $event, IEvent $previousEvent = null) { | 
            ||
| 90 | 		if ($event->getApp() !== 'sharebymail') { | 
            ||
| 91 | throw new \InvalidArgumentException();  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | 		$this->l = $this->languageFactory->get('sharebymail', $language); | 
            ||
| 95 | |||
| 96 | 		if ($this->activityManager->isFormattingFilteredObject()) { | 
            ||
| 97 | 			try { | 
            ||
| 98 | return $this->parseShortVersion($event);  | 
            ||
| 99 | 			} catch (\InvalidArgumentException $e) { | 
            ||
| 100 | // Ignore and simply use the long version...  | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | return $this->parseLongVersion($event);  | 
            ||
| 105 | }  | 
            ||
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * @param IEvent $event  | 
            ||
| 109 | * @return IEvent  | 
            ||
| 110 | * @throws \InvalidArgumentException  | 
            ||
| 111 | * @since 11.0.0  | 
            ||
| 112 | */  | 
            ||
| 113 | 	public function parseShortVersion(IEvent $event) { | 
            ||
| 114 | $parsedParameters = $this->getParsedParameters($event);  | 
            ||
| 115 | |||
| 116 | 		if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) { | 
            ||
| 117 | 			$event->setParsedSubject($this->l->t('Shared with %1$s', [ | 
            ||
| 118 | $parsedParameters['email']['name'],  | 
            ||
| 119 | ]))  | 
            ||
| 120 | 				->setRichSubject($this->l->t('Shared with {email}'), [ | 
            ||
| 121 | 'email' => $parsedParameters['email'],  | 
            ||
| 122 | ]);  | 
            ||
| 123 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 124 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 125 | 			} else { | 
            ||
| 126 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 127 | }  | 
            ||
| 128 | 		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) { | 
            ||
| 129 | 			$event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [ | 
            ||
| 130 | $parsedParameters['email']['name'],  | 
            ||
| 131 | $parsedParameters['actor']['name'],  | 
            ||
| 132 | ]))  | 
            ||
| 133 | 				->setRichSubject($this->l->t('Shared with {email} by {actor}'), [ | 
            ||
| 134 | 'email' => $parsedParameters['email'],  | 
            ||
| 135 | 'actor' => $parsedParameters['actor'],  | 
            ||
| 136 | ]);  | 
            ||
| 137 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 138 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 139 | 			} else { | 
            ||
| 140 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 141 | }  | 
            ||
| 142 | 		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) { | 
            ||
| 143 | 			$event->setParsedSubject($this->l->t('Unshared from %1$s', [ | 
            ||
| 144 | $parsedParameters['email']['name'],  | 
            ||
| 145 | ]))  | 
            ||
| 146 | 				->setRichSubject($this->l->t('Unshared from {email}'), [ | 
            ||
| 147 | 'email' => $parsedParameters['email'],  | 
            ||
| 148 | ]);  | 
            ||
| 149 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 150 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 151 | 			} else { | 
            ||
| 152 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 153 | }  | 
            ||
| 154 | 		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) { | 
            ||
| 155 | 			$event->setParsedSubject($this->l->t('Unshared from %1$s by %2$s', [ | 
            ||
| 156 | $parsedParameters['email']['name'],  | 
            ||
| 157 | $parsedParameters['actor']['name'],  | 
            ||
| 158 | ]))  | 
            ||
| 159 | 				->setRichSubject($this->l->t('Unshared from {email} by {actor}'), [ | 
            ||
| 160 | 'email' => $parsedParameters['email'],  | 
            ||
| 161 | 'actor' => $parsedParameters['actor'],  | 
            ||
| 162 | ]);  | 
            ||
| 163 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 164 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 165 | 			} else { | 
            ||
| 166 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 167 | }  | 
            ||
| 168 | 		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) { | 
            ||
| 169 | 			$event->setParsedSubject($this->l->t('Password for mail share sent to %1$s', [ | 
            ||
| 170 | $parsedParameters['email']['name']  | 
            ||
| 171 | ]))  | 
            ||
| 172 | 				->setRichSubject($this->l->t('Password for mail share sent to {email}'), [ | 
            ||
| 173 | 'email' => $parsedParameters['email']  | 
            ||
| 174 | ]);  | 
            ||
| 175 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 176 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 177 | 			} else { | 
            ||
| 178 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 179 | }  | 
            ||
| 180 | 		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) { | 
            ||
| 181 | 			$event->setParsedSubject($this->l->t('Password for mail share sent to you')) | 
            ||
| 182 | 				->setRichSubject($this->l->t('Password for mail share sent to you')); | 
            ||
| 183 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 184 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 185 | 			} else { | 
            ||
| 186 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 187 | }  | 
            ||
| 188 | 		} else { | 
            ||
| 189 | throw new \InvalidArgumentException();  | 
            ||
| 190 | }  | 
            ||
| 191 | |||
| 192 | return $event;  | 
            ||
| 193 | }  | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * @param IEvent $event  | 
            ||
| 197 | * @return IEvent  | 
            ||
| 198 | * @throws \InvalidArgumentException  | 
            ||
| 199 | * @since 11.0.0  | 
            ||
| 200 | */  | 
            ||
| 201 | 	public function parseLongVersion(IEvent $event) { | 
            ||
| 202 | $parsedParameters = $this->getParsedParameters($event);  | 
            ||
| 203 | |||
| 204 | 		if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) { | 
            ||
| 205 | 			$event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [ | 
            ||
| 206 | $parsedParameters['file']['path'],  | 
            ||
| 207 | $parsedParameters['email']['name'],  | 
            ||
| 208 | ]))  | 
            ||
| 209 | 				->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters); | 
            ||
| 210 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 211 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 212 | 			} else { | 
            ||
| 213 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 214 | }  | 
            ||
| 215 | 		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) { | 
            ||
| 216 | 			$event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [ | 
            ||
| 217 | $parsedParameters['file']['path'],  | 
            ||
| 218 | $parsedParameters['email']['name'],  | 
            ||
| 219 | $parsedParameters['actor']['name'],  | 
            ||
| 220 | ]))  | 
            ||
| 221 | 				->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters); | 
            ||
| 222 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 223 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 224 | 			} else { | 
            ||
| 225 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 226 | }  | 
            ||
| 227 | 		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) { | 
            ||
| 228 | 			$event->setParsedSubject($this->l->t('You unshared %1$s from %2$s by mail', [ | 
            ||
| 229 | $parsedParameters['file']['path'],  | 
            ||
| 230 | $parsedParameters['email']['name'],  | 
            ||
| 231 | ]))  | 
            ||
| 232 | 				->setRichSubject($this->l->t('You unshared {file} from {email} by mail'), $parsedParameters); | 
            ||
| 233 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 234 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 235 | 			} else { | 
            ||
| 236 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 237 | }  | 
            ||
| 238 | 		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) { | 
            ||
| 239 | 			$event->setParsedSubject($this->l->t('%3$s unshared %1$s from %2$s by mail', [ | 
            ||
| 240 | $parsedParameters['file']['path'],  | 
            ||
| 241 | $parsedParameters['email']['name'],  | 
            ||
| 242 | $parsedParameters['actor']['name'],  | 
            ||
| 243 | ]))  | 
            ||
| 244 | 				->setRichSubject($this->l->t('{actor} unshared {file} from {email} by mail'), $parsedParameters); | 
            ||
| 245 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 246 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 247 | 			} else { | 
            ||
| 248 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 249 | }  | 
            ||
| 250 | 		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) { | 
            ||
| 251 | 			$event->setParsedSubject($this->l->t('Password to access %1$s was sent to %2s', [ | 
            ||
| 252 | $parsedParameters['file']['path'],  | 
            ||
| 253 | $parsedParameters['email']['name']  | 
            ||
| 254 | ]))  | 
            ||
| 255 | 				->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters); | 
            ||
| 256 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 257 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 258 | 			} else { | 
            ||
| 259 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 260 | }  | 
            ||
| 261 | 		} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) { | 
            ||
| 262 | $event->setParsedSubject(  | 
            ||
| 263 | 				$this->l->t('Password to access %1$s was sent to you', | 
            ||
| 264 | [$parsedParameters['file']['path']]))  | 
            ||
| 265 | 				->setRichSubject($this->l->t('Password to access {file} was sent to you'), $parsedParameters); | 
            ||
| 266 | 			if ($this->activityManager->getRequirePNG()) { | 
            ||
| 267 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); | 
            ||
| 268 | 			} else { | 
            ||
| 269 | 				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); | 
            ||
| 270 | }  | 
            ||
| 271 | |||
| 272 | 		} else { | 
            ||
| 273 | throw new \InvalidArgumentException();  | 
            ||
| 274 | }  | 
            ||
| 275 | |||
| 276 | return $event;  | 
            ||
| 277 | }  | 
            ||
| 278 | |||
| 279 | 	protected function getParsedParameters(IEvent $event) { | 
            ||
| 280 | $subject = $event->getSubject();  | 
            ||
| 281 | $parameters = $event->getSubjectParameters();  | 
            ||
| 282 | |||
| 283 | 		switch ($subject) { | 
            ||
| 284 | case self::SUBJECT_SHARED_EMAIL_SELF:  | 
            ||
| 285 | case self::SUBJECT_UNSHARED_EMAIL_SELF:  | 
            ||
| 286 | return [  | 
            ||
| 287 | 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),  | 
            ||
| 288 | 'email' => $this->generateEmailParameter($parameters[1]),  | 
            ||
| 289 | ];  | 
            ||
| 290 | case self::SUBJECT_SHARED_EMAIL_BY:  | 
            ||
| 291 | case self::SUBJECT_UNSHARED_EMAIL_BY:  | 
            ||
| 292 | return [  | 
            ||
| 293 | 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),  | 
            ||
| 294 | 'email' => $this->generateEmailParameter($parameters[1]),  | 
            ||
| 295 | 'actor' => $this->generateUserParameter($parameters[2]),  | 
            ||
| 296 | ];  | 
            ||
| 297 | case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:  | 
            ||
| 298 | return [  | 
            ||
| 299 | 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),  | 
            ||
| 300 | 'email' => $this->generateEmailParameter($parameters[1]),  | 
            ||
| 301 | ];  | 
            ||
| 302 | case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:  | 
            ||
| 303 | return [  | 
            ||
| 304 | 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),  | 
            ||
| 305 | ];  | 
            ||
| 306 | }  | 
            ||
| 307 | throw new \InvalidArgumentException();  | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * @param int $id  | 
            ||
| 312 | * @param string $path  | 
            ||
| 313 | * @return array  | 
            ||
| 314 | */  | 
            ||
| 315 | 	protected function generateFileParameter($id, $path) { | 
            ||
| 316 | return [  | 
            ||
| 317 | 'type' => 'file',  | 
            ||
| 318 | 'id' => $id,  | 
            ||
| 319 | 'name' => basename($path),  | 
            ||
| 320 | 'path' => trim($path, '/'),  | 
            ||
| 321 | 			'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), | 
            ||
| 322 | ];  | 
            ||
| 323 | }  | 
            ||
| 324 | |||
| 325 | /**  | 
            ||
| 326 | * @param string $email  | 
            ||
| 327 | * @return array  | 
            ||
| 328 | */  | 
            ||
| 329 | 	protected function generateEmailParameter($email) { | 
            ||
| 330 | 		if (!isset($this->contactNames[$email])) { | 
            ||
| 331 | $this->contactNames[$email] = $this->getContactName($email);  | 
            ||
| 332 | }  | 
            ||
| 333 | |||
| 334 | return [  | 
            ||
| 335 | 'type' => 'email',  | 
            ||
| 336 | 'id' => $email,  | 
            ||
| 337 | 'name' => $this->contactNames[$email],  | 
            ||
| 338 | ];  | 
            ||
| 339 | }  | 
            ||
| 340 | |||
| 341 | /**  | 
            ||
| 342 | * @param string $uid  | 
            ||
| 343 | * @return array  | 
            ||
| 344 | */  | 
            ||
| 345 | 	protected function generateUserParameter($uid) { | 
            ||
| 346 | 		if (!isset($this->displayNames[$uid])) { | 
            ||
| 347 | $this->displayNames[$uid] = $this->getDisplayName($uid);  | 
            ||
| 348 | }  | 
            ||
| 349 | |||
| 350 | return [  | 
            ||
| 351 | 'type' => 'user',  | 
            ||
| 352 | 'id' => $uid,  | 
            ||
| 353 | 'name' => $this->displayNames[$uid],  | 
            ||
| 354 | ];  | 
            ||
| 355 | }  | 
            ||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * @param string $email  | 
            ||
| 359 | * @return string  | 
            ||
| 360 | */  | 
            ||
| 361 | 	protected function getContactName($email) { | 
            ||
| 375 | }  | 
            ||
| 376 | |||
| 377 | /**  | 
            ||
| 378 | * @param string $uid  | 
            ||
| 379 | * @return string  | 
            ||
| 380 | */  | 
            ||
| 381 | 	protected function getDisplayName($uid) { | 
            ||
| 387 | }  | 
            ||
| 388 | }  | 
            ||
| 389 | }  | 
            ||
| 390 |