Completed
Pull Request — master (#551)
by Maxence
02:27
created

MemberAdd::confirmMemberFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\FederatedItems;
33
34
35
use daita\MySmallPhpTools\Traits\TStringTools;
36
use Exception;
37
use OC\User\NoUserException;
38
use OCA\Circles\Db\MemberRequest;
39
use OCA\Circles\Exceptions\MemberLevelException;
40
use OCA\Circles\Exceptions\MembersLimitException;
41
use OCA\Circles\Exceptions\MemberTypeNotFoundException;
42
use OCA\Circles\Exceptions\TokenDoesNotExistException;
43
use OCA\Circles\Exceptions\UserTypeNotFoundException;
44
use OCA\Circles\IFederatedItem;
45
use OCA\Circles\IFederatedItemMemberCheckNotRequired;
46
use OCA\Circles\IFederatedItemMemberRequired;
47
use OCA\Circles\IFederatedUser;
48
use OCA\Circles\Model\DeprecatedCircle;
49
use OCA\Circles\Model\DeprecatedMember;
50
use OCA\Circles\Model\Federated\FederatedEvent;
51
use OCA\Circles\Model\Member;
52
use OCA\Circles\Model\SharesToken;
53
use OCA\Circles\Service\CircleService;
54
use OCA\Circles\Service\ConfigService;
55
use OCP\IUser;
56
use OCP\IUserManager;
57
use OCP\Mail\IEMailTemplate;
58
use OCP\Util;
59
60
61
/**
62
 * Class MemberAdd
63
 *
64
 * @package OCA\Circles\GlobalScale
65
 */
66
class MemberAdd implements
67
	IFederatedItem,
68
	IFederatedItemMemberRequired,
69
	IFederatedItemMemberCheckNotRequired {
70
71
72
	use TStringTools;
73
74
75
	/** @var IUserManager */
76
	private $userManager;
77
78
	/** @var MemberRequest */
79
	private $memberRequest;
80
81
	/** @var CircleService */
82
	private $circleService;
83
84
	/** @var ConfigService */
85
	private $configService;
86
87
88
	/**
89
	 * MemberAdd constructor.
90
	 *
91
	 * @param IUserManager $userManager
92
	 * @param MemberRequest $memberRequest
93
	 * @param CircleService $circleService
94
	 * @param ConfigService $configService
95
	 */
96
	public function __construct(
97
		IUserManager $userManager, MemberRequest $memberRequest, CircleService $circleService,
98
		ConfigService $configService
99
	) {
100
		$this->userManager = $userManager;
101
		$this->memberRequest = $memberRequest;
102
		$this->circleService = $circleService;
103
		$this->configService = $configService;
104
	}
105
106
107
	/**
108
	 * @param FederatedEvent $event
109
	 *
110
	 * @throws MemberLevelException
111
	 * @throws NoUserException
112
	 * @throws UserTypeNotFoundException
113
	 * @throws MembersLimitException
114
	 */
115
	public function verify(FederatedEvent $event): void {
116
		$member = $event->getMember();
117
		$circle = $event->getCircle();
118
		$initiator = $circle->getInitiator();
119
120
		if ($initiator->getLevel() < Member::LEVEL_MODERATOR) {
121
			throw new MemberLevelException('Insufficient rights to add members to this Circle');
122
		}
123
124
		$this->confirmMemberFormat($member);
125
		$member->setId($this->uuid(Member::ID_LENGTH));
126
		$member->setCircleId($circle->getId());
127
128
129
		// TODO: check Config on Circle to know if we set Level to 1 or just send an invitation
130
		$member->setLevel(Member::LEVEL_MEMBER);
131
		$member->setStatus(Member::STATUS_MEMBER);
132
133
		$event->setDataOutcome(['member' => $member]);
134
135
		// TODO: Managing cached name
136
		//		$member->setCachedName($eventMember->getCachedName());
137
// check Circle is 'verified'
138
		$this->circleService->confirmCircleNotFull($circle);
139
140
		$event->setReadingOutcome('Member %s have been added to Circle', ['userId' => $member->getUserId()]);
141
142
		return;
143
144
145
//		$member = $this->membersRequest->getFreshNewMember(
146
//			$circle->getUniqueId(), $ident, $eventMember->getType(), $eventMember->getInstance()
147
//		);
148
//		$member->hasToBeInviteAble();
149
//
150
//		$this->membersService->addMemberBasedOnItsType($circle, $member);
151
//
152
//		$password = '';
153
//		$sendPasswordByMail = false;
154
//		if ($this->configService->enforcePasswordProtection($circle)) {
155
//			if ($circle->getSetting('password_single_enabled') === 'true') {
156
//				$password = $circle->getPasswordSingle();
157
//			} else {
158
//				$sendPasswordByMail = true;
159
//				$password = $this->miscService->token(15);
160
//			}
161
//		}
162
//
163
//		$event->setData(
164
//			new SimpleDataStore(
165
//				[
166
//					'password'       => $password,
167
//					'passwordByMail' => $sendPasswordByMail
168
//				]
169
//			)
170
//		);
171
	}
172
173
174
	/**
175
	 * @param FederatedEvent $event
176
	 *
177
	 */
178
	public function manage(FederatedEvent $event): void {
179
		//$circle = $event->getCircle();
180
		$member = $event->getMember();
181
//		if ($member->getJoined() === '') {
182
//			$this->membersRequest->createMember($member);
183
//		} else {
184
//			$this->membersRequest->updateMemberLevel($member);
185
//		}
186
//
187
188
		$this->memberRequest->save($member);
189
190
//
191
//		//
192
//		// TODO: verifiez comment se passe le cached name sur un member_add
193
//		//
194
//		$cachedName = $member->getCachedName();
195
//		$password = $event->getData()
196
//						  ->g('password');
197
//
198
//		$shares = $this->generateUnknownSharesLinks($circle, $member, $password);
199
//		$result = [
200
//			'unknownShares' => $shares,
201
//			'cachedName'    => $cachedName
202
//		];
203
//
204
//		if ($member->getType() === DeprecatedMember::TYPE_CONTACT
205
//			&& $this->configService->isLocalInstance($member->getInstance())) {
206
//			$result['contact'] = $this->miscService->getInfosFromContact($member);
207
//		}
208
//
209
//		$event->setResult(new SimpleDataStore($result));
210
//		$this->eventsService->onMemberNew($circle, $member);
211
	}
212
213
214
	/**
215
	 * @param FederatedEvent[] $events
216
	 *
217
	 * @throws Exception
218
	 */
219
	public function result(array $events): void {
220
//		$password = $cachedName = '';
221
//		$circle = $member = null;
222
//		$links = [];
223
//		$recipients = [];
224
//		foreach ($events as $event) {
225
//			$data = $event->getData();
226
//			if ($data->gBool('passwordByMail') !== false) {
227
//				$password = $data->g('password');
228
//			}
229
//			$circle = $event->getDeprecatedCircle();
230
//			$member = $event->getMember();
231
//			$result = $event->getResult();
232
//			if ($result->g('cachedName') !== '') {
233
//				$cachedName = $result->g('cachedName');
234
//			}
235
//
236
//			$links = array_merge($links, $result->gArray('unknownShares'));
237
//			$contact = $result->gArray('contact');
238
//			if (!empty($contact)) {
239
//				$recipients = $contact['emails'];
240
//			}
241
//		}
242
//
243
//		if (empty($links) || $circle === null || $member === null) {
244
//			return;
245
//		}
246
//
247
//		if ($cachedName !== '') {
248
//			$member->setCachedName($cachedName);
249
//			$this->membersService->updateMember($member);
250
//		}
251
//
252
//		if ($member->getType() === DeprecatedMember::TYPE_MAIL
253
//			|| $member->getType() === DeprecatedMember::TYPE_CONTACT) {
254
//			if ($member->getType() === DeprecatedMember::TYPE_MAIL) {
255
//				$recipients = [$member->getUserId()];
256
//			}
257
//
258
//			foreach ($recipients as $recipient) {
259
//				$this->memberIsMailbox($circle, $recipient, $links, $password);
260
//			}
261
//		}
262
	}
263
264
265
	/**
266
	 * confirm the format of UserId, based on UserType.
267
	 *
268
	 * @param IFederatedUser $member
269
	 *
270
	 * @throws UserTypeNotFoundException
271
	 * @throws NoUserException
272
	 */
273
	private function confirmMemberFormat(IFederatedUser $member): void {
274
		switch ($member->getUserType()) {
275
			case Member::TYPE_USER:
276
				$this->confirmMemberTypeUser($member);
277
				break;
278
279
			// TODO #M003: confirm other UserType
280
			default:
281
				throw new UserTypeNotFoundException();
282
		}
283
	}
284
285
286
	/**
287
	 * @param IFederatedUser $member
288
	 *
289
	 * @throws NoUserException
290
	 */
291
	private function confirmMemberTypeUser(IFederatedUser $member): void {
292
		if ($this->configService->isLocalInstance($member->getInstance())) {
293
			$user = $this->userManager->get($member->getUserId());
294
			if ($user === null) {
295
				throw new NoUserException('user not found');
296
			}
297
298
			$member->setUserId($user->getUID());
299
300
			return;
301
		}
302
303
		// TODO #M002: request the remote instance and check that user exists
304
	}
305
306
//	/**
307
//	 * Verify if a local account is valid.
308
//	 *
309
//	 * @param $ident
310
//	 * @param $type
311
//	 *
312
//	 * @param string $instance
313
//	 *
314
//	 * @throws NoUserException
315
//	 */
316
//	private function verifyIdentLocalMember(&$ident, $type, string $instance = '') {
317
//		if ($type !== DeprecatedMember::TYPE_USER) {
318
//			return;
319
//		}
320
//
321
//		if ($instance === '') {
322
//			try {
323
//				$ident = $this->miscService->getRealUserId($ident);
324
//			} catch (NoUserException $e) {
325
//				throw new NoUserException($this->l10n->t("This user does not exist"));
326
//			}
327
//		}
328
//	}
329
//
330
//
331
//	/**
332
//	 * Verify if a mail have a valid format.
333
//	 *
334
//	 * @param string $ident
335
//	 * @param int $type
336
//	 *
337
//	 * @throws EmailAccountInvalidFormatException
338
//	 */
339
//	private function verifyIdentEmailAddress(string $ident, int $type) {
340
//		if ($type !== DeprecatedMember::TYPE_MAIL) {
341
//			return;
342
//		}
343
//
344
//		if ($this->configService->isAccountOnly()) {
345
//			throw new EmailAccountInvalidFormatException(
346
//				$this->l10n->t('You cannot add a mail address as member of your Circle')
347
//			);
348
//		}
349
//
350
//		if (!filter_var($ident, FILTER_VALIDATE_EMAIL)) {
351
//			throw new EmailAccountInvalidFormatException(
352
//				$this->l10n->t('Email format is not valid')
353
//			);
354
//		}
355
//	}
356
//
357
//
358
//	/**
359
//	 * Verify if a contact exist in current user address books.
360
//	 *
361
//	 * @param $ident
362
//	 * @param $type
363
//	 *
364
//	 * @throws NoUserException
365
//	 * @throws EmailAccountInvalidFormatException
366
//	 */
367
//	private function verifyIdentContact(&$ident, $type) {
368
//		if ($type !== DeprecatedMember::TYPE_CONTACT) {
369
//			return;
370
//		}
371
//
372
//		if ($this->configService->isAccountOnly()) {
373
//			throw new EmailAccountInvalidFormatException(
374
//				$this->l10n->t('You cannot add a contact as member of your Circle')
375
//			);
376
//		}
377
//
378
//		$tmpContact = $this->userId . ':' . $ident;
379
//		$result = MiscService::getContactData($tmpContact);
380
//		if (empty($result)) {
381
//			throw new NoUserException($this->l10n->t("This contact is not available"));
382
//		}
383
//
384
//		$ident = $tmpContact;
385
//	}
386
387
388
	/**
389
	 * @param DeprecatedCircle $circle
390
	 * @param string $recipient
391
	 * @param array $links
392
	 * @param string $password
393
	 */
394 View Code Duplication
	private function memberIsMailbox(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
395
		DeprecatedCircle $circle, string $recipient, array $links, string $password
396
	) {
397
		if ($circle->getViewer() === null) {
398
			$author = $circle->getOwner()
399
							 ->getUserId();
400
		} else {
401
			$author = $circle->getViewer()
402
							 ->getUserId();
403
		}
404
405
		try {
406
			$template = $this->generateMailExitingShares($author, $circle->getName());
407
			$this->fillMailExistingShares($template, $links);
408
			$this->sendMailExistingShares($template, $author, $recipient);
409
			$this->sendPasswordExistingShares($author, $recipient, $password);
410
		} catch (Exception $e) {
411
			$this->miscService->log('Failed to send mail about existing share ' . $e->getMessage());
0 ignored issues
show
Bug introduced by
The property miscService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
412
		}
413
	}
414
415
416
	/**
417
	 * @param DeprecatedCircle $circle
418
	 * @param DeprecatedMember $member
419
	 * @param string $password
420
	 *
421
	 * @return array
422
	 */
423 View Code Duplication
	private function generateUnknownSharesLinks(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
424
		DeprecatedCircle $circle, DeprecatedMember $member, string $password
0 ignored issues
show
Unused Code introduced by
The parameter $circle is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
425
	): array {
426
		$unknownShares = $this->getUnknownShares($member);
427
428
		$data = [];
429
		foreach ($unknownShares as $share) {
430
			try {
431
				$data[] = $this->getMailLinkFromShare($share, $member, $password);
432
			} catch (TokenDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
433
			}
434
		}
435
436
		return $data;
437
	}
438
439
440
	/**
441
	 * @param DeprecatedMember $member
442
	 *
443
	 * @return array
444
	 */
445 View Code Duplication
	private function getUnknownShares(DeprecatedMember $member): array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
446
		$allShares = $this->sharesRequest->getSharesForCircle($member->getCircleId());
0 ignored issues
show
Bug introduced by
The property sharesRequest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
447
		$knownShares = array_map(
448
			function(SharesToken $shareToken) {
449
				return $shareToken->getShareId();
450
			},
451
			$this->tokensRequest->getTokensFromMember($member)
0 ignored issues
show
Bug introduced by
The property tokensRequest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
452
		);
453
454
		$unknownShares = [];
455
		foreach ($allShares as $share) {
456
			if (!in_array($share['id'], $knownShares)) {
457
				$unknownShares[] = $share;
458
			}
459
		}
460
461
		return $unknownShares;
462
	}
463
464
465
	/**
466
	 * @param array $share
467
	 * @param DeprecatedMember $member
468
	 * @param string $password
469
	 *
470
	 * @return array
471
	 * @throws TokenDoesNotExistException
472
	 */
473 View Code Duplication
	private function getMailLinkFromShare(array $share, DeprecatedMember $member, string $password = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
474
		$sharesToken = $this->tokensRequest->generateTokenForMember($member, (int)$share['id'], $password);
475
		$link = $this->urlGenerator->linkToRouteAbsolute(
0 ignored issues
show
Bug introduced by
The property urlGenerator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
476
			'files_sharing.sharecontroller.showShare',
477
			['token' => $sharesToken->getToken()]
478
		);
479
		$author = $share['uid_initiator'];
480
		$filename = basename($share['file_target']);
481
482
		return [
483
			'author'   => $author,
484
			'link'     => $link,
485
			'filename' => $filename
486
		];
487
	}
488
489
490
	/**
491
	 * @param string $author
492
	 * @param string $circleName
493
	 *
494
	 * @return IEMailTemplate
495
	 */
496 View Code Duplication
	private function generateMailExitingShares(string $author, string $circleName): IEMailTemplate {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
497
		$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []);
0 ignored issues
show
Bug introduced by
The property mailer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
498
		$emailTemplate->addHeader();
499
500
		$text = $this->l10n->t('%s shared multiple files with \'%s\'.', [$author, $circleName]);
0 ignored issues
show
Bug introduced by
The property l10n does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
501
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
502
503
		return $emailTemplate;
504
	}
505
506
	/**
507
	 * @param IEMailTemplate $emailTemplate
508
	 * @param array $links
509
	 */
510 View Code Duplication
	private function fillMailExistingShares(IEMailTemplate $emailTemplate, array $links) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
511
		foreach ($links as $item) {
512
			$emailTemplate->addBodyButton(
513
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
514
			);
515
		}
516
	}
517
518
519
	/**
520
	 * @param IEMailTemplate $emailTemplate
521
	 * @param string $author
522
	 * @param string $recipient
523
	 *
524
	 * @throws Exception
525
	 */
526 View Code Duplication
	private function sendMailExistingShares(IEMailTemplate $emailTemplate, string $author, string $recipient
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
527
	) {
528
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
529
530
		$instanceName = $this->defaults->getName();
0 ignored issues
show
Bug introduced by
The property defaults does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
531
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
532
533
		$message = $this->mailer->createMessage();
534
535
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
536
		$message->setSubject($subject);
537
		$message->setPlainBody($emailTemplate->renderText());
538
		$message->setHtmlBody($emailTemplate->renderHtml());
539
		$message->setTo([$recipient]);
540
541
		$this->mailer->send($message);
542
	}
543
544
545
	/**
546
	 * @param string $author
547
	 * @param string $email
548
	 * @param string $password
549
	 *
550
	 * @throws Exception
551
	 */
552
	protected function sendPasswordExistingShares(string $author, string $email, string $password) {
553
		if ($password === '') {
554
			return;
555
		}
556
557
		$message = $this->mailer->createMessage();
558
559
		$authorUser = $this->userManager->get($author);
560
		$authorName = ($authorUser instanceof IUser) ? $authorUser->getDisplayName() : $author;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
561
		$authorEmail = ($authorUser instanceof IUser) ? $authorUser->getEMailAddress() : null;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
562
563
		$this->miscService->log("Sending password mail about existing files to '" . $email . "'", 0);
564
565
		$plainBodyPart = $this->l10n->t(
566
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
567
			[$authorName]
568
		);
569
		$htmlBodyPart = $this->l10n->t(
570
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
571
			[$authorName]
572
		);
573
574
		$emailTemplate = $this->mailer->createEMailTemplate(
575
			'sharebymail.RecipientPasswordNotification', [
576
														   'password' => $password,
577
														   'author'   => $author
578
													   ]
579
		);
580
581
		$emailTemplate->setSubject(
582
			$this->l10n->t(
583
				'Password to access files shared to you by %1$s', [$authorName]
584
			)
585
		);
586
		$emailTemplate->addHeader();
587
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
588
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
589
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
590
		$emailTemplate->addBodyText($password);
591
592
		// The "From" contains the sharers name
593
		$instanceName = $this->defaults->getName();
594
		$senderName = $this->l10n->t(
595
			'%1$s via %2$s',
596
			[
597
				$authorName,
598
				$instanceName
599
			]
600
		);
601
602
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
603
		if ($authorEmail !== null) {
604
			$message->setReplyTo([$authorEmail => $authorName]);
605
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
606
		} else {
607
			$emailTemplate->addFooter();
608
		}
609
610
		$message->setTo([$email]);
611
		$message->useTemplate($emailTemplate);
612
		$this->mailer->send($message);
613
	}
614
615
}
616