xref: /webtrees/app/Statistics/Repository/FavoritesRepository.php (revision df27497a949ee94233b78915e33f253447a14e62)
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
68        if ($module instanceof FamilyTreeFavoritesModule) {
69            return $module->getBlock($this->tree, 0, ModuleBlockInterface::CONTEXT_EMBED);
70        }
71
72        return '';
73    }
74
75    /**
76     * @inheritDoc
77     */
78    public function userFavorites(): string
79    {
80        $module = $this->module_service
81            ->findByInterface(UserFavoritesModule::class);
82
83        if ($module instanceof UserFavoritesModule) {
84            return $module->getBlock($this->tree, 0, ModuleBlockInterface::CONTEXT_EMBED);
85        }
86
87        return '';
88    }
89
90    /**
91     * @inheritDoc
92     */
93    public function totalGedcomFavorites(): string
94    {
95        $count  = 0;
96        $module = $this->module_service
97            ->findByInterface(FamilyTreeFavoritesModule::class);
98
99        if ($module instanceof FamilyTreeFavoritesModule) {
100            $count = count($module->getFavorites($this->tree));
101        }
102
103        return I18N::number($count);
104    }
105
106    /**
107     * @inheritDoc
108     */
109    public function totalUserFavorites(): string
110    {
111        $count  = 0;
112        $module = $this->module_service
113            ->findByInterface(UserFavoritesModule::class);
114
115        if ($module instanceof UserFavoritesModule) {
116            $count = count($module->getFavorites($this->tree, Auth::user()));
117        }
118
119        return I18N::number($count);
120    }
121}
122