Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.62% covered (danger)
21.62%
8 / 37
25.00% covered (danger)
25.00%
3 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
AccountCapabilities
21.62% covered (danger)
21.62%
8 / 37
25.00% covered (danger)
25.00%
3 / 12
463.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAuth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCommentSetting
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 getListSetting
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 getSavedSearchSetting
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getTagSetting
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getListTagSetting
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 isScheduledSearchEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSmsSetting
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 isAccountAvailable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isRatingRemovalAllowed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 libraryCardsEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Class to determine which account capabilities are available, based on
5 * configuration and other factors.
6 *
7 * PHP version 8
8 *
9 * Copyright (C) Villanova University 2015.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Config
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org Main Site
29 */
30
31namespace VuFind\Config;
32
33use Laminas\Config\Config;
34use VuFind\Auth\Manager as AuthManager;
35
36use function in_array;
37
38/**
39 * Class to determine which account capabilities are available, based on
40 * configuration and other factors.
41 *
42 * @category VuFind
43 * @package  Config
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org Main Site
47 */
48class AccountCapabilities
49{
50    /**
51     * Function to fetch auth manager
52     *
53     * @var callable
54     */
55    protected $authCallback;
56
57    /**
58     * Constructor
59     *
60     * @param Config   $config  Top-level configuration
61     * @param callable $getAuth Function to fetch auth manager
62     */
63    public function __construct(protected Config $config, callable $getAuth)
64    {
65        $this->authCallback = $getAuth;
66    }
67
68    /**
69     * Get authentication manager
70     *
71     * @return AuthManager
72     */
73    protected function getAuth(): AuthManager
74    {
75        return ($this->authCallback)();
76    }
77
78    /**
79     * Get comment setting.
80     *
81     * @return string
82     */
83    public function getCommentSetting()
84    {
85        if (!$this->isAccountAvailable()) {
86            return 'disabled';
87        }
88        return isset($this->config->Social->comments)
89            && $this->config->Social->comments === 'disabled'
90            ? 'disabled' : 'enabled';
91    }
92
93    /**
94     * Get list setting.
95     *
96     * @return string
97     */
98    public function getListSetting()
99    {
100        if (!$this->isAccountAvailable()) {
101            return 'disabled';
102        }
103        $setting = isset($this->config->Social->lists)
104            ? trim(strtolower($this->config->Social->lists)) : 'enabled';
105        if (!$setting) {
106            $setting = 'disabled';
107        }
108        $legal = ['enabled', 'disabled', 'public_only', 'private_only'];
109        if (!in_array($setting, $legal)) {
110            $setting = 'enabled';
111        }
112        return $setting;
113    }
114
115    /**
116     * Get saved search setting.
117     *
118     * @return string
119     */
120    public function getSavedSearchSetting()
121    {
122        if (!$this->isAccountAvailable()) {
123            return 'disabled';
124        }
125        return isset($this->config->Site->allowSavedSearches)
126            && !$this->config->Site->allowSavedSearches
127            ? 'disabled' : 'enabled';
128    }
129
130    /**
131     * Get tag setting.
132     *
133     * @return string
134     */
135    public function getTagSetting()
136    {
137        if (!$this->isAccountAvailable()) {
138            return 'disabled';
139        }
140        return isset($this->config->Social->tags)
141            && $this->config->Social->tags === 'disabled'
142            ? 'disabled' : 'enabled';
143    }
144
145    /**
146     * Get list tag setting.
147     *
148     * @return string
149     */
150    public function getListTagSetting()
151    {
152        if (!$this->isAccountAvailable()) {
153            return 'disabled';
154        }
155        return $this->config->Social->listTags ?? 'disabled';
156    }
157
158    /**
159     * Is scheduled search enabled?
160     *
161     * @return bool
162     */
163    public function isScheduledSearchEnabled(): bool
164    {
165        return $this->config->Account->schedule_searches ?? false;
166    }
167
168    /**
169     * Get SMS setting ('enabled' or 'disabled').
170     *
171     * @return string
172     */
173    public function getSmsSetting()
174    {
175        return isset($this->config->Mail->sms)
176            && $this->config->Mail->sms === 'disabled'
177            ? 'disabled' : 'enabled';
178    }
179
180    /**
181     * Is a user account capable of saving data currently available?
182     *
183     * @return bool
184     */
185    protected function isAccountAvailable()
186    {
187        // We can't use account features if login is broken or privacy is on:
188        $auth = $this->getAuth();
189        return $auth->loginEnabled() && !$auth->inPrivacyMode();
190    }
191
192    /**
193     * Check if record ratings can be removed
194     *
195     * @return bool
196     */
197    public function isRatingRemovalAllowed(): bool
198    {
199        return (bool)($this->config->Social->remove_rating ?? true);
200    }
201
202    /**
203     * Are library cards enabled and supported?
204     *
205     * @return bool
206     */
207    public function libraryCardsEnabled(): bool
208    {
209        return ($this->config->Catalog->library_cards ?? false) && !$this->getAuth()->inPrivacyMode();
210    }
211}