xref: /webtrees/app/GuestUser.php (revision 1821c9e596679be8717c51394b448df204b1f577)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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;
23
24use function is_string;
25
26/**
27 * A site visitor.
28 */
29class GuestUser implements UserInterface
30{
31    private string $email;
32
33    private string $real_name;
34
35    /**
36     * GuestUser constructor.
37     *
38     * @param string $email
39     * @param string $real_name
40     */
41    public function __construct(string $email = 'GUEST_USER', string $real_name = 'GUEST_USER')
42    {
43        $this->email = $email;
44        $this->real_name = $real_name;
45    }
46
47    /**
48     * The user‘s internal identifier.
49     *
50     * @return int
51     */
52    public function id(): int
53    {
54        return 0;
55    }
56
57    /**
58     * The users email address.
59     *
60     * @return string
61     */
62    public function email(): string
63    {
64        return $this->email;
65    }
66
67    /**
68     * The user‘s real name.
69     *
70     * @return string
71     */
72    public function realName(): string
73    {
74        return $this->real_name;
75    }
76
77    /**
78     * The user‘s login name.
79     *
80     * @return string
81     */
82    public function userName(): string
83    {
84        return '';
85    }
86
87    /**
88     * @param string $setting_name
89     * @param string $default
90     *
91     * @return string
92     */
93    public function getPreference(string $setting_name, string $default = ''): string
94    {
95        $preference = Session::get('_GUEST_' . $setting_name);
96
97        return is_string($preference) ? $preference : $default;
98    }
99
100    /**
101     * @param string $setting_name
102     * @param string $setting_value
103     *
104     * @return void
105     */
106    public function setPreference(string $setting_name, string $setting_value): void
107    {
108        Session::put('_GUEST_' . $setting_name, $setting_value);
109    }
110}
111