xref: /webtrees/app/Statistics/Repository/FavoritesRepository.php (revision e0dcab18f07e7cdd57669bc5b941048aac1ce91c)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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 function count;
21use Fisharebest\Webtrees\Auth;
22use Fisharebest\Webtrees\I18N;
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
38    private $tree;
39    /**
40     * @var ModuleService
41     */
42    private $module_service;
43
44    /**
45     * Constructor.
46     *
47     * @param Tree          $tree
48     * @param ModuleService $module_service
49     */
50    public function __construct(Tree $tree, ModuleService $module_service)
51    {
52        $this->tree           = $tree;
53        $this->module_service = $module_service;
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function gedcomFavorites(): string
60    {
61        $module = $this->module_service
62            ->findByInterface(FamilyTreeFavoritesModule::class);
63
64        if ($module instanceof FamilyTreeFavoritesModule) {
65            return $module->getBlock($this->tree, 0);
66        }
67
68        return '';
69    }
70
71    /**
72     * @inheritDoc
73     */
74    public function userFavorites(): string
75    {
76        $module = $this->module_service
77            ->findByInterface(UserFavoritesModule::class);
78
79        if ($module instanceof UserFavoritesModule) {
80            return $module->getBlock($this->tree, 0);
81        }
82
83        return '';
84    }
85
86    /**
87     * @inheritDoc
88     */
89    public function totalGedcomFavorites(): string
90    {
91        $count  = 0;
92        $module = $this->module_service
93            ->findByInterface(FamilyTreeFavoritesModule::class);
94
95        if ($module instanceof FamilyTreeFavoritesModule) {
96            $count = count($module->getFavorites($this->tree));
97        }
98
99        return I18N::number($count);
100    }
101
102    /**
103     * @inheritDoc
104     */
105    public function totalUserFavorites(): string
106    {
107        $count  = 0;
108        $module = $this->module_service
109            ->findByInterface(UserFavoritesModule::class);
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