Completed
Push — master ( 712d21...572f95 )
by Valentyn
05:22
created

AuthService::isPasswordValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Service;
5
6
use App\Users\Entity\ApiToken;
7
use App\Users\Entity\User;
8
use App\Users\Repository\UserRepository;
9
use App\Users\Request\AuthUserRequest;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
12
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
class AuthService
17
{
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $entityManager;
22
23
    /**
24
     * @var UserRepository
25
     */
26
    private $userRepository;
27
28
    /**
29
     * @var TranslatorInterface
30
     */
31
    private $translator;
32
33
    /**
34
     * @var UserPasswordEncoderInterface
35
     */
36
    private $passwordEncoder;
37
38 7
    public function __construct(
39
        EntityManagerInterface $entityManager,
40
        UserRepository $userRepository,
41
        TranslatorInterface $translator,
42
        UserPasswordEncoderInterface $passwordEncoder
43
    )
44
    {
45 7
        $this->entityManager = $entityManager;
46 7
        $this->userRepository = $userRepository;
47 7
        $this->translator = $translator;
48 7
        $this->passwordEncoder = $passwordEncoder;
49 7
    }
50
51 7
    public function getTokenByRequest(AuthUserRequest $request): ApiToken
52
    {
53 7
        $credentials = $request->get('credentials');
54
55 7
        return $this->getTokenByCredentials($credentials['username'], $credentials['password']);
56
    }
57
58 7
    public function getTokenByCredentials(string $username, string $password): ApiToken
59
    {
60 7
        $user = $this->findUserByCredentials($username, $password);
61 3
        $apiToken = $this->createApiTokenForUser($user);
62
63 3
        return $apiToken;
64
    }
65
66 3
    private function createApiTokenForUser(User $user): ApiToken
67
    {
68 3
        $apiToken = new ApiToken($user);
69
70 3
        $this->entityManager->persist($apiToken);
71 3
        $this->entityManager->flush();
72
73 3
        return $apiToken;
74
    }
75
76 7
    private function findUserByCredentials(string $username, string $password): User
77
    {
78 7
        $user = $this->userRepository->loadUserByUsername($username);
79
80 7
        if ($user === null) {
81 2
            throw new BadCredentialsException(
82 2
                $this->translator->trans('user_with_this_username_not_exist', [
83 2
                    'username' => $username,
84 2
                ], 'users')
85
            );
86
        }
87
88 5
        if ($this->passwordEncoder->isPasswordValid($user, $password) === false) {
89 2
            throw new BadCredentialsException(
90 2
                $this->translator->trans('wrong_password', [], 'users')
91
            );
92
        }
93
94 3
        return $user;
95
    }
96
}