UserService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B createOrUpdateUser() 0 28 2
1
<?php
2
3
namespace AppBundle\Service\Entity;
4
5
use AppBundle\Entity\Repository\UserRepository;
6
use AppBundle\Entity\User;
7
use AppBundle\Service\HashGenerator;
8
use AppBundle\Service\Vkontakte\AccessToken;
9
use AppBundle\Service\Vkontakte\VkontakteClient;
10
11
/**
12
 * @author Vehsamrak
13
 */
14
class UserService extends EntityService
15
{
16
17
    /** @var UserRepository */
18
    private $userRepository;
19
20
    /** @var VkontakteClient */
21
    private $vkontakteClient;
22
23 2
    public function __construct(
24
        VkontakteClient $vkontakteClient,
25
        UserRepository $userRepository
26
    )
27
    {
28 2
        $this->userRepository = $userRepository;
29 2
        $this->vkontakteClient = $vkontakteClient;
30 2
    }
31
32 2
    public function createOrUpdateUser(AccessToken $vkToken): User
33
    {
34 2
        $userVkId = $vkToken->getUserVkontakteId();
35 2
        $user = $this->userRepository->findUserByVkId($userVkId);
36 2
        $vkTokenHash = $vkToken->getVkontakteTokenHash();
37
38 2
        if ($user) {
39 1
            $user->updateToken();
40 1
            $user->setVkToken($vkTokenHash);
41
        } else {
42 1
            $id = HashGenerator::generate();
43 1
            $name = $this->vkontakteClient->getUserName($vkTokenHash);
44
45 1
            $user = new User(
46
                $id,
47
                $name,
48
                $userVkId,
49
                $vkTokenHash,
50 1
                $vkToken->userEmail
51
            );
52
53 1
            $this->userRepository->persist($user);
54
        }
55
56 2
        $this->userRepository->flush($user);
57
58 2
        return $user;
59
    }
60
}
61