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