Completed
Pull Request — master (#551)
by Maxence
01:54
created

MemberAdd::getUnknownShares()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
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\RemoteEvents;
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\MemberTypeNotFoundException;
41
use OCA\Circles\Exceptions\RemoteEventException;
42
use OCA\Circles\Exceptions\TokenDoesNotExistException;
43
use OCA\Circles\Exceptions\UserTypeNotFoundException;
44
use OCA\Circles\IMember;
45
use OCA\Circles\IRemoteEvent;
46
use OCA\Circles\Model\DeprecatedCircle;
47
use OCA\Circles\Model\DeprecatedMember;
48
use OCA\Circles\Model\Member;
49
use OCA\Circles\Model\Remote\RemoteEvent;
50
use OCA\Circles\Model\SharesToken;
51
use OCA\Circles\Service\CircleService;
52
use OCA\Circles\Service\ConfigService;
53
use OCP\IUser;
54
use OCP\IUserManager;
55
use OCP\Mail\IEMailTemplate;
56
use OCP\Util;
57
58
59
/**
60
 * Class MemberAdd
61
 *
62
 * @package OCA\Circles\GlobalScale
63
 */
64
class MemberAdd implements IRemoteEvent {
65
66
67
	use TStringTools;
68
69
70
	/** @var IUserManager */
71
	private $userManager;
72
73
	/** @var MemberRequest */
74
	private $memberRequest;
75
76
	/** @var CircleService */
77
	private $circleService;
78
79
	/** @var ConfigService */
80
	private $configService;
81
82
83
	/**
84
	 * MemberAdd constructor.
85
	 *
86
	 * @param IUserManager $userManager
87
	 * @param MemberRequest $memberRequest
88
	 * @param CircleService $circleService
89
	 * @param ConfigService $configService
90
	 */
91
	public function __construct(
92
		IUserManager $userManager, MemberRequest $memberRequest, CircleService $circleService,
93
		ConfigService $configService
94
	) {
95
		$this->userManager = $userManager;
96
		$this->memberRequest = $memberRequest;
97
		$this->circleService = $circleService;
98
		$this->configService = $configService;
99
	}
100
101
102
	/**
103
	 * @param RemoteEvent $event
104
	 *
105
	 * @throws MemberLevelException
106
	 * @throws NoUserException
107
	 * @throws RemoteEventException
108
	 * @throws UserTypeNotFoundException
109
	 * @throws \OCA\Circles\Exceptions\MembersLimitException
110
	 */
111
	public function verify(RemoteEvent $event): void {
112
		if (!$event->hasMember()) {
113
			throw new RemoteEventException('event have no member linked');
114
		}
115
116
		$member = $event->getMember();
117
		$circle = $event->getCircle();
118
		$viewer = $circle->getViewer();
119
120
		if ($viewer->getLevel() < Member::LEVEL_MODERATOR) {
121
			throw new MemberLevelException('Viewer have no rights to add members to this Circles');
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
		echo json_encode($member) . "\n";
134
135
		// TODO: Managing cached name
136
		//		$member->setCachedName($eventMember->getCachedName());
137
138
// check Circle is 'verified'
139
		$this->circleService->confirmCircleNotFull($circle);
140
141
		return;
142
143
144
//		$member = $this->membersRequest->getFreshNewMember(
145
//			$circle->getUniqueId(), $ident, $eventMember->getType(), $eventMember->getInstance()
146
//		);
147
//		$member->hasToBeInviteAble();
148
//
149
//		$this->membersService->addMemberBasedOnItsType($circle, $member);
150
//
151
//		$password = '';
152
//		$sendPasswordByMail = false;
153
//		if ($this->configService->enforcePasswordProtection($circle)) {
154
//			if ($circle->getSetting('password_single_enabled') === 'true') {
155
//				$password = $circle->getPasswordSingle();
156
//			} else {
157
//				$sendPasswordByMail = true;
158
//				$password = $this->miscService->token(15);
159
//			}
160
//		}
161
//
162
//		$event->setData(
163
//			new SimpleDataStore(
164
//				[
165
//					'password'       => $password,
166
//					'passwordByMail' => $sendPasswordByMail
167
//				]
168
//			)
169
//		);
170
	}
171
172
173
	/**
174
	 * @param RemoteEvent $event
175
	 *
176
	 */
177
	public function manage(RemoteEvent $event): void {
178
		//$circle = $event->getCircle();
179
		$member = $event->getMember();
180
//		if ($member->getJoined() === '') {
181
//			$this->membersRequest->createMember($member);
182
//		} else {
183
//			$this->membersRequest->updateMemberLevel($member);
184
//		}
185
//
186
187
		$this->memberRequest->save($member);
188
189
//
190
//		//
191
//		// TODO: verifiez comment se passe le cached name sur un member_add
192
//		//
193
//		$cachedName = $member->getCachedName();
194
//		$password = $event->getData()
195
//						  ->g('password');
196
//
197
//		$shares = $this->generateUnknownSharesLinks($circle, $member, $password);
198
//		$result = [
199
//			'unknownShares' => $shares,
200
//			'cachedName'    => $cachedName
201
//		];
202
//
203
//		if ($member->getType() === DeprecatedMember::TYPE_CONTACT
204
//			&& $this->configService->isLocalInstance($member->getInstance())) {
205
//			$result['contact'] = $this->miscService->getInfosFromContact($member);
206
//		}
207
//
208
//		$event->setResult(new SimpleDataStore($result));
209
//		$this->eventsService->onMemberNew($circle, $member);
210
	}
211
212
213
	/**
214
	 * @param RemoteEvent[] $events
215
	 *
216
	 * @throws Exception
217
	 */
218
	public function result(array $events): void {
219
//		$password = $cachedName = '';
220
//		$circle = $member = null;
221
//		$links = [];
222
//		$recipients = [];
223
//		foreach ($events as $event) {
224
//			$data = $event->getData();
225
//			if ($data->gBool('passwordByMail') !== false) {
226
//				$password = $data->g('password');
227
//			}
228
//			$circle = $event->getDeprecatedCircle();
229
//			$member = $event->getMember();
230
//			$result = $event->getResult();
231
//			if ($result->g('cachedName') !== '') {
232
//				$cachedName = $result->g('cachedName');
233
//			}
234
//
235
//			$links = array_merge($links, $result->gArray('unknownShares'));
236
//			$contact = $result->gArray('contact');
237
//			if (!empty($contact)) {
238
//				$recipients = $contact['emails'];
239
//			}
240
//		}
241
//
242
//		if (empty($links) || $circle === null || $member === null) {
243
//			return;
244
//		}
245
//
246
//		if ($cachedName !== '') {
247
//			$member->setCachedName($cachedName);
248
//			$this->membersService->updateMember($member);
249
//		}
250
//
251
//		if ($member->getType() === DeprecatedMember::TYPE_MAIL
252
//			|| $member->getType() === DeprecatedMember::TYPE_CONTACT) {
253
//			if ($member->getType() === DeprecatedMember::TYPE_MAIL) {
254
//				$recipients = [$member->getUserId()];
255
//			}
256
//
257
//			foreach ($recipients as $recipient) {
258
//				$this->memberIsMailbox($circle, $recipient, $links, $password);
259
//			}
260
//		}
261
	}
262
263
264
	/**
265
	 * confirm the format of UserId, based on UserType.
266
	 *
267
	 * @param IMember $member
268
	 *
269
	 * @throws UserTypeNotFoundException
270
	 * @throws NoUserException
271
	 */
272
	private function confirmMemberFormat(IMember $member): void {
273
		switch ($member->getUserType()) {
274
			case Member::TYPE_USER:
275
				$this->confirmMemberTypeUser($member);
276
				break;
277
278
			// TODO #M003: confirm other UserType
279
			default:
280
				throw new UserTypeNotFoundException();
281
		}
282
	}
283
284
285
	/**
286
	 * @param IMember $member
287
	 *
288
	 * @throws NoUserException
289
	 */
290
	private function confirmMemberTypeUser(IMember $member): void {
291
		if ($this->configService->isLocalInstance($member->getInstance())) {
292
			$user = $this->userManager->get($member->getUserId());
293
			if ($user === null) {
294
				throw new NoUserException('user not found');
295
			}
296
297
			$member->setUserId($user->getUID());
298
299
			return;
300
		}
301
302
		// TODO #M002: request the remote instance and check that user exists
303
	}
304
305
//	/**
306
//	 * Verify if a local account is valid.
307
//	 *
308
//	 * @param $ident
309
//	 * @param $type
310
//	 *
311
//	 * @param string $instance
312
//	 *
313
//	 * @throws NoUserException
314
//	 */
315
//	private function verifyIdentLocalMember(&$ident, $type, string $instance = '') {
316
//		if ($type !== DeprecatedMember::TYPE_USER) {
317
//			return;
318
//		}
319
//
320
//		if ($instance === '') {
321
//			try {
322
//				$ident = $this->miscService->getRealUserId($ident);
323
//			} catch (NoUserException $e) {
324
//				throw new NoUserException($this->l10n->t("This user does not exist"));
325
//			}
326
//		}
327
//	}
328
//
329
//
330
//	/**
331
//	 * Verify if a mail have a valid format.
332
//	 *
333
//	 * @param string $ident
334
//	 * @param int $type
335
//	 *
336
//	 * @throws EmailAccountInvalidFormatException
337
//	 */
338
//	private function verifyIdentEmailAddress(string $ident, int $type) {
339
//		if ($type !== DeprecatedMember::TYPE_MAIL) {
340
//			return;
341
//		}
342
//
343
//		if ($this->configService->isAccountOnly()) {
344
//			throw new EmailAccountInvalidFormatException(
345
//				$this->l10n->t('You cannot add a mail address as member of your Circle')
346
//			);
347
//		}
348
//
349
//		if (!filter_var($ident, FILTER_VALIDATE_EMAIL)) {
350
//			throw new EmailAccountInvalidFormatException(
351
//				$this->l10n->t('Email format is not valid')
352
//			);
353
//		}
354
//	}
355
//
356
//
357
//	/**
358
//	 * Verify if a contact exist in current user address books.
359
//	 *
360
//	 * @param $ident
361
//	 * @param $type
362
//	 *
363
//	 * @throws NoUserException
364
//	 * @throws EmailAccountInvalidFormatException
365
//	 */
366
//	private function verifyIdentContact(&$ident, $type) {
367
//		if ($type !== DeprecatedMember::TYPE_CONTACT) {
368
//			return;
369
//		}
370
//
371
//		if ($this->configService->isAccountOnly()) {
372
//			throw new EmailAccountInvalidFormatException(
373
//				$this->l10n->t('You cannot add a contact as member of your Circle')
374
//			);
375
//		}
376
//
377
//		$tmpContact = $this->userId . ':' . $ident;
378
//		$result = MiscService::getContactData($tmpContact);
379
//		if (empty($result)) {
380
//			throw new NoUserException($this->l10n->t("This contact is not available"));
381
//		}
382
//
383
//		$ident = $tmpContact;
384
//	}
385
386
387
	/**
388
	 * @param DeprecatedCircle $circle
389
	 * @param string $recipient
390
	 * @param array $links
391
	 * @param string $password
392
	 */
393 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...
394
		DeprecatedCircle $circle, string $recipient, array $links, string $password
395
	) {
396
		if ($circle->getViewer() === null) {
397
			$author = $circle->getOwner()
398
							 ->getUserId();
399
		} else {
400
			$author = $circle->getViewer()
401
							 ->getUserId();
402
		}
403
404
		try {
405
			$template = $this->generateMailExitingShares($author, $circle->getName());
406
			$this->fillMailExistingShares($template, $links);
407
			$this->sendMailExistingShares($template, $author, $recipient);
408
			$this->sendPasswordExistingShares($author, $recipient, $password);
409
		} catch (Exception $e) {
410
			$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...
411
		}
412
	}
413
414
415
	/**
416
	 * @param DeprecatedCircle $circle
417
	 * @param DeprecatedMember $member
418
	 * @param string $password
419
	 *
420
	 * @return array
421
	 */
422 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...
423
		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...
424
	): array {
425
		$unknownShares = $this->getUnknownShares($member);
426
427
		$data = [];
428
		foreach ($unknownShares as $share) {
429
			try {
430
				$data[] = $this->getMailLinkFromShare($share, $member, $password);
431
			} catch (TokenDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
432
			}
433
		}
434
435
		return $data;
436
	}
437
438
439
	/**
440
	 * @param DeprecatedMember $member
441
	 *
442
	 * @return array
443
	 */
444 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...
445
		$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...
446
		$knownShares = array_map(
447
			function(SharesToken $shareToken) {
448
				return $shareToken->getShareId();
449
			},
450
			$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...
451
		);
452
453
		$unknownShares = [];
454
		foreach ($allShares as $share) {
455
			if (!in_array($share['id'], $knownShares)) {
456
				$unknownShares[] = $share;
457
			}
458
		}
459
460
		return $unknownShares;
461
	}
462
463
464
	/**
465
	 * @param array $share
466
	 * @param DeprecatedMember $member
467
	 * @param string $password
468
	 *
469
	 * @return array
470
	 * @throws TokenDoesNotExistException
471
	 */
472 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...
473
		$sharesToken = $this->tokensRequest->generateTokenForMember($member, (int)$share['id'], $password);
474
		$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...
475
			'files_sharing.sharecontroller.showShare',
476
			['token' => $sharesToken->getToken()]
477
		);
478
		$author = $share['uid_initiator'];
479
		$filename = basename($share['file_target']);
480
481
		return [
482
			'author'   => $author,
483
			'link'     => $link,
484
			'filename' => $filename
485
		];
486
	}
487
488
489
	/**
490
	 * @param string $author
491
	 * @param string $circleName
492
	 *
493
	 * @return IEMailTemplate
494
	 */
495 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...
496
		$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...
497
		$emailTemplate->addHeader();
498
499
		$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...
500
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
501
502
		return $emailTemplate;
503
	}
504
505
	/**
506
	 * @param IEMailTemplate $emailTemplate
507
	 * @param array $links
508
	 */
509 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...
510
		foreach ($links as $item) {
511
			$emailTemplate->addBodyButton(
512
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
513
			);
514
		}
515
	}
516
517
518
	/**
519
	 * @param IEMailTemplate $emailTemplate
520
	 * @param string $author
521
	 * @param string $recipient
522
	 *
523
	 * @throws Exception
524
	 */
525 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...
526
	) {
527
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
528
529
		$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...
530
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
531
532
		$message = $this->mailer->createMessage();
533
534
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
535
		$message->setSubject($subject);
536
		$message->setPlainBody($emailTemplate->renderText());
537
		$message->setHtmlBody($emailTemplate->renderHtml());
538
		$message->setTo([$recipient]);
539
540
		$this->mailer->send($message);
541
	}
542
543
544
	/**
545
	 * @param string $author
546
	 * @param string $email
547
	 * @param string $password
548
	 *
549
	 * @throws Exception
550
	 */
551
	protected function sendPasswordExistingShares(string $author, string $email, string $password) {
552
		if ($password === '') {
553
			return;
554
		}
555
556
		$message = $this->mailer->createMessage();
557
558
		$authorUser = $this->userManager->get($author);
559
		$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...
560
		$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...
561
562
		$this->miscService->log("Sending password mail about existing files to '" . $email . "'", 0);
563
564
		$plainBodyPart = $this->l10n->t(
565
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
566
			[$authorName]
567
		);
568
		$htmlBodyPart = $this->l10n->t(
569
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
570
			[$authorName]
571
		);
572
573
		$emailTemplate = $this->mailer->createEMailTemplate(
574
			'sharebymail.RecipientPasswordNotification', [
575
														   'password' => $password,
576
														   'author'   => $author
577
													   ]
578
		);
579
580
		$emailTemplate->setSubject(
581
			$this->l10n->t(
582
				'Password to access files shared to you by %1$s', [$authorName]
583
			)
584
		);
585
		$emailTemplate->addHeader();
586
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
587
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
588
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
589
		$emailTemplate->addBodyText($password);
590
591
		// The "From" contains the sharers name
592
		$instanceName = $this->defaults->getName();
593
		$senderName = $this->l10n->t(
594
			'%1$s via %2$s',
595
			[
596
				$authorName,
597
				$instanceName
598
			]
599
		);
600
601
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
602
		if ($authorEmail !== null) {
603
			$message->setReplyTo([$authorEmail => $authorName]);
604
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
605
		} else {
606
			$emailTemplate->addFooter();
607
		}
608
609
		$message->setTo([$email]);
610
		$message->useTemplate($emailTemplate);
611
		$this->mailer->send($message);
612
	}
613
614
}
615