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