xref: /webtrees/app/Statistics/Repository/FavoritesRepository.php (revision 11eb858145c6c7c490e5c0cd7b0bd51e519264f0)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Repository;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Module;
23use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
24use Fisharebest\Webtrees\Module\UserFavoritesModule;
25use Fisharebest\Webtrees\Statistics\Repository\Interfaces\FavoritesRepositoryInterface;
26use Fisharebest\Webtrees\Tree;
27
28/**
29 * A repository providing methods for favorites related statistics.
30 */
31class FavoritesRepository implements FavoritesRepositoryInterface
32{
33    /**
34     * @var Tree
35     */
36    private $tree;
37
38    /**
39     * Constructor.
40     *
41     * @param Tree $tree
42     */
43    public function __construct(Tree $tree)
44    {
45        $this->tree = $tree;
46    }
47
48    /**
49     * @inheritDoc
50     */
51    public function gedcomFavorites(): string
52    {
53        $module = Module::findByClass(FamilyTreeFavoritesModule::class);
54
55        if ($module instanceof FamilyTreeFavoritesModule) {
56            return $module->getBlock($this->tree, 0);
57        }
58
59        return '';
60    }
61
62    /**
63     * @inheritDoc
64     */
65    public function userFavorites(): string
66    {
67        $module = Module::findByClass(UserFavoritesModule::class);
68
69        if ($module instanceof UserFavoritesModule) {
70            return $module->getBlock($this->tree, 0);
71        }
72
73        return '';
74    }
75
76    /**
77     * @inheritDoc
78     */
79    public function totalGedcomFavorites(): string
80    {
81        $count  = 0;
82        $module = Module::findByClass(FamilyTreeFavoritesModule::class);
83
84        if ($module instanceof FamilyTreeFavoritesModule) {
85            $count = \count($module->getFavorites($this->tree));
86        }
87
88        return I18N::number($count);
89    }
90
91    /**
92     * @inheritDoc
93     */
94    public function totalUserFavorites(): string
95    {
96        $count  = 0;
97        $module = Module::findByClass(UserFavoritesModule::class);
98
99        if ($module instanceof UserFavoritesModule) {
100            $count = \count($module->getFavorites($this->tree, Auth::user()));
101        }
102
103        return I18N::number($count);
104    }
105}
106