PasswordController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace BrainExe\Core\Authentication\Controller;
4
5
use BrainExe\Core\Annotations\Controller;
6
use BrainExe\Core\Annotations\Route;
7
use BrainExe\Core\Application\UserException;
8
use BrainExe\Core\Authentication\PasswordHasher;
9
use BrainExe\Core\Authentication\UserProvider;
10
use BrainExe\Core\Authentication\UserVO;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * @Controller
15
 */
16
class PasswordController
17
{
18
19
    /**
20
     * @var UserProvider
21
     */
22
    private $user;
23
    /**
24
     * @var PasswordHasher
25
     */
26
    private $passwordHasher;
27
28
    /**
29
     * @param UserProvider $userProvider
30
     * @param PasswordHasher $passwordHasher
31
     */
32 2
    public function __construct(
33
        UserProvider $userProvider,
34
        PasswordHasher $passwordHasher
35
    ) {
36 2
        $this->user           = $userProvider;
37 2
        $this->passwordHasher = $passwordHasher;
38 2
    }
39
40
    /**
41
     * @param Request $request
42
     * @return bool
43
     * @throws UserException
44
     * @Route("/user/change_password/", name="user.change_password", methods="POST")
45
     */
46 2
    public function changePassword(Request $request) : bool
47
    {
48 2
        $oldPassword = $request->request->get('oldPassword');
49 2
        $newPassword = $request->request->get('newPassword');
50
51
        /** @var UserVO $user */
52 2
        $user = $request->attributes->get('user');
53 2
        if (!$this->passwordHasher->verifyHash($oldPassword, $user->getPassword())) {
54 1
            throw new UserException('Invalid Password given');
55
        }
56
57 1
        $this->user->changePassword($user, $newPassword);
58
59 1
        return true;
60
    }
61
}
62