1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Fisharebest\Webtrees\Contracts\UserInterface; 23use Fisharebest\Webtrees\Services\UserService; 24 25/** 26 * A tree can act as a user, for example to send email. 27 */ 28class TreeUser implements UserInterface 29{ 30 private Tree $tree; 31 32 /** 33 * TreeUser constructor. 34 * 35 * @param Tree $tree 36 */ 37 public function __construct(Tree $tree) 38 { 39 $this->tree = $tree; 40 } 41 42 /** 43 * The user‘s internal identifier. 44 * 45 * @return int 46 */ 47 public function id(): int 48 { 49 return 0; 50 } 51 52 /** 53 * The users email address. 54 * 55 * @return string 56 */ 57 public function email(): string 58 { 59 $user_service = app(UserService::class); 60 $contact_id = (int) $this->getPreference('CONTACT_USER_ID'); 61 62 if ($contact_id !== 0) { 63 $contact = $user_service->find($contact_id); 64 65 if ($contact instanceof User) { 66 return $contact->email(); 67 } 68 } 69 70 return Site::getPreference('SMTP_FROM_NAME'); 71 } 72 73 /** 74 * @param string $setting_name 75 * @param string $default 76 * 77 * @return string 78 */ 79 public function getPreference(string $setting_name, string $default = ''): string 80 { 81 return $default; 82 } 83 84 /** 85 * The user‘s real name. 86 * 87 * @return string 88 */ 89 public function realName(): string 90 { 91 return $this->tree->title(); 92 } 93 94 /** 95 * The user‘s login name. 96 * 97 * @return string 98 */ 99 public function userName(): string 100 { 101 return ''; 102 } 103 104 /** 105 * @param string $setting_name 106 * @param string $setting_value 107 * 108 * @return void 109 */ 110 public function setPreference(string $setting_name, string $setting_value): void 111 { 112 } 113} 114