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