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