Passed
Push — devel-3.0 ( af8b21...5a06ca )
by Rubén
03:22
created

AccountPresetService::checkPasswordPreset()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 4
nop 1
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Services\Account;
26
27
use SP\Config\ConfigData;
28
use SP\Core\Exceptions\ValidationException;
29
use SP\DataModel\ItemPreset\Password;
30
use SP\Mvc\Controller\Validators\PasswordValidator;
31
use SP\Services\ItemPreset\ItemPresetInterface;
32
use SP\Services\ItemPreset\ItemPresetService;
33
34
35
/**
36
 * Class AccountPreset
37
 *
38
 * @package SP\Services\Account
39
 */
40
class AccountPresetService
41
{
42
    /**
43
     * @var ItemPresetService
44
     */
45
    private $itemPresetService;
46
    /**
47
     * @var ConfigData
48
     */
49
    private $configData;
50
51
    /**
52
     * AccountPreset constructor.
53
     *
54
     * @param ItemPresetService $itemPresetService
55
     * @param ConfigData        $configData
56
     */
57
    public function __construct(ItemPresetService $itemPresetService, ConfigData $configData)
58
    {
59
        $this->itemPresetService = $itemPresetService;
60
        $this->configData = $configData;
61
    }
62
63
    /**
64
     * @param AccountRequest $accountRequest
65
     *
66
     * @throws ValidationException
67
     * @throws \SP\Core\Exceptions\ConstraintException
68
     * @throws \SP\Core\Exceptions\NoSuchPropertyException
69
     * @throws \SP\Core\Exceptions\QueryException
70
     */
71
    public function checkPasswordPreset(AccountRequest $accountRequest)
72
    {
73
        $itemPreset = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PASSWORD);
74
75
        if ($itemPreset !== null && $itemPreset->getFixed() === 1) {
76
            $passwordPreset = $itemPreset->hydrate(Password::class);
77
78
            PasswordValidator::factory($passwordPreset)->validate($accountRequest->pass);
0 ignored issues
show
Bug introduced by
It seems like $passwordPreset can also be of type null and string; however, parameter $password of SP\Mvc\Controller\Valida...ordValidator::factory() does only seem to accept SP\DataModel\ItemPreset\Password, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            PasswordValidator::factory(/** @scrutinizer ignore-type */ $passwordPreset)->validate($accountRequest->pass);
Loading history...
79
80
            if ($this->configData->isAccountExpireEnabled()) {
81
                $expireTimePreset = $passwordPreset->getExpireTime();
82
83
                if ($expireTimePreset > 0
84
                    && ($accountRequest->passDateChange === 0
85
                        || $accountRequest->passDateChange < time() + $expireTimePreset)
86
                ) {
87
                    $accountRequest->passDateChange = time() + $expireTimePreset;
88
                }
89
            }
90
        }
91
    }
92
}