xref: /webtrees/app/Statistics/Repository/ContactRepository.php (revision 1a2589c705c5f3df3b73b8df4b5a0f19e787de5b)
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\Statistics\Repository;
21
22use Fisharebest\Webtrees\Services\UserService;
23use Fisharebest\Webtrees\Statistics\Repository\Interfaces\ContactRepositoryInterface;
24use Fisharebest\Webtrees\Tree;
25use Fisharebest\Webtrees\User;
26use Psr\Http\Message\ServerRequestInterface;
27
28use function app;
29use function assert;
30
31/**
32 * A repository providing methods for contact related statistics.
33 */
34class ContactRepository implements ContactRepositoryInterface
35{
36    private Tree $tree;
37
38    private UserService $user_service;
39
40    /**
41     * @param Tree        $tree
42     * @param UserService $user_service
43     */
44    public function __construct(Tree $tree, UserService $user_service)
45    {
46        $this->tree         = $tree;
47        $this->user_service = $user_service;
48    }
49
50    /**
51     * @return string
52     */
53    public function contactWebmaster(): string
54    {
55        $user_id = (int) $this->tree->getPreference('WEBMASTER_USER_ID');
56        $user    = $this->user_service->find($user_id);
57
58        if ($user instanceof User) {
59            $request = app(ServerRequestInterface::class);
60            assert($request instanceof ServerRequestInterface);
61
62            return $this->user_service->contactLink($user, $request);
63        }
64
65        return '';
66    }
67
68    /**
69     * @return string
70     */
71    public function contactGedcom(): string
72    {
73        $user_id = (int) $this->tree->getPreference('CONTACT_USER_ID');
74        $user    = $this->user_service->find($user_id);
75
76        if ($user instanceof User) {
77            $request = app(ServerRequestInterface::class);
78            assert($request instanceof ServerRequestInterface);
79
80            return $this->user_service->contactLink($user, $request);
81        }
82
83        return '';
84    }
85}
86