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