xref: /webtrees/app/Statistics/Repository/FavoritesRepository.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
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\Statistics\Repository;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
25use Fisharebest\Webtrees\Module\ModuleBlockInterface;
26use Fisharebest\Webtrees\Module\UserFavoritesModule;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Statistics\Repository\Interfaces\FavoritesRepositoryInterface;
29use Fisharebest\Webtrees\Tree;
30
31use function count;
32
33/**
34 * A repository providing methods for favorites related statistics.
35 */
36class FavoritesRepository implements FavoritesRepositoryInterface
37{
38    private Tree $tree;
39
40    private ModuleService $module_service;
41
42    /**
43     * @param Tree          $tree
44     * @param ModuleService $module_service
45     */
46    public function __construct(Tree $tree, ModuleService $module_service)
47    {
48        $this->tree           = $tree;
49        $this->module_service = $module_service;
50    }
51
52    /**
53     * @return string
54     */
55    public function gedcomFavorites(): string
56    {
57        $module = $this->module_service
58            ->findByInterface(FamilyTreeFavoritesModule::class)
59            ->first();
60
61        if ($module instanceof FamilyTreeFavoritesModule) {
62            return $module->getBlock($this->tree, 0, ModuleBlockInterface::CONTEXT_EMBED);
63        }
64
65        return '';
66    }
67
68    /**
69     * @return string
70     */
71    public function userFavorites(): string
72    {
73        $module = $this->module_service
74            ->findByInterface(UserFavoritesModule::class)
75            ->first();
76
77        if ($module instanceof UserFavoritesModule) {
78            return $module->getBlock($this->tree, 0, ModuleBlockInterface::CONTEXT_EMBED);
79        }
80
81        return '';
82    }
83
84    /**
85     * @return string
86     */
87    public function totalGedcomFavorites(): string
88    {
89        $count  = 0;
90        $module = $this->module_service
91            ->findByInterface(FamilyTreeFavoritesModule::class)
92            ->first();
93
94        if ($module instanceof FamilyTreeFavoritesModule) {
95            $count = count($module->getFavorites($this->tree));
96        }
97
98        return I18N::number($count);
99    }
100
101    /**
102     * @return string
103     */
104    public function totalUserFavorites(): string
105    {
106        $count  = 0;
107        $module = $this->module_service
108            ->findByInterface(UserFavoritesModule::class)
109            ->first();
110
111        if ($module instanceof UserFavoritesModule) {
112            $count = count($module->getFavorites($this->tree, Auth::user()));
113        }
114
115        return I18N::number($count);
116    }
117}
118