SettingsService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 19
c 1
b 0
f 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPasswordExpiryTimestamp() 0 13 4
A getSettings() 0 14 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "fe_change_pwd" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Derhansen\FeChangePwd\Service;
13
14
use TYPO3\CMS\Core\Context\Context;
15
use TYPO3\CMS\Core\Context\TypoScriptAspect;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * Class SettingsService
20
 */
21
class SettingsService
22
{
23
    /**
24
     * @var mixed
25
     */
26
    protected $settings;
27
28
    /**
29
     * Returns the settings
30
     *
31
     * @return array
32
     */
33
    public function getSettings(): array
34
    {
35
        if ($this->settings === null) {
36
            // Ensure, TSFE setup is loaded for cached pages
37
            if ($GLOBALS['TSFE']->tmpl === null || $GLOBALS['TSFE']->tmpl && empty($GLOBALS['TSFE']->tmpl->setup)) {
38
                GeneralUtility::makeInstance(Context::class)
39
                    ->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, true));
40
                $GLOBALS['TSFE']->getConfigArray();
41
            }
42
43
            $settings = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_fechangepwd.']['settings.'] ?? [];
44
            $this->settings = GeneralUtility::removeDotsFromTS($settings);
45
        }
46
        return $this->settings;
47
    }
48
49
    /**
50
     * Returns the password expiry timestamp depending on the configured setting switch. If password expiry is not
51
     * enabled, 0 is returned. If no password validity in days is configured, 90 days is taken as fallback
52
     *
53
     * @param \DateTime|null $currentDate
54
     * @return int
55
     */
56
    public function getPasswordExpiryTimestamp(?\DateTime $currentDate = null): int
57
    {
58
        if (!$currentDate) {
59
            $currentDate = new \DateTime();
60
        }
61
        $result = 0;
62
        $settings = $this->getSettings();
63
        if (isset($settings['passwordExpiration']['enabled']) && (bool)$settings['passwordExpiration']['enabled']) {
64
            $validityInDays = $settings['passwordExpiration']['validityInDays'] ?? 90;
65
            $currentDate->modify('+' . $validityInDays . 'days');
66
            $result = $currentDate->getTimestamp();
67
        }
68
        return $result;
69
    }
70
}
71