UserController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 86
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCurrentUser() 0 4 1
A getAvatars() 0 4 1
A setAvatars() 0 13 2
A setEmail() 0 9 1
A getList() 0 4 1
1
<?php
2
3
namespace BrainExe\Core\Authentication\Controller;
4
5
use BrainExe\Core\Annotations\Controller;
6
use BrainExe\Core\Annotations\Guest;
7
use BrainExe\Core\Annotations\Route;
8
use BrainExe\Core\Application\UserException;
9
use BrainExe\Core\Authentication\UserProvider;
10
use BrainExe\Core\Authentication\UserVO;
11
use BrainExe\Core\Translation\TranslationTrait;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * @Controller
16
 */
17
class UserController
18
{
19
    use TranslationTrait;
20
21
    /**
22
     * @var UserProvider
23
     */
24
    private $userProvider;
25
26
    /**
27
     * @param UserProvider $userProvider
28
     */
29 6
    public function __construct(UserProvider $userProvider)
30
    {
31 6
        $this->userProvider = $userProvider;
32 6
    }
33
34
    /**
35
     * @param Request $request
36
     * @return UserVO
37
     * @Route("/user/", name="authenticate.current_user", methods="GET")
38
     * @Guest
39
     */
40 1
    public function getCurrentUser(Request $request) : UserVO
41
    {
42 1
        return $request->attributes->get('user');
43
    }
44
45
    /**
46
     * @return string[]
47
     * @Route("/user/avatar/", name="authenticate.avatars", methods="GET", options={"cache":10800})
48
     */
49 1
    public function getAvatars() : array
50
    {
51 1
        return UserVO::AVATARS;
52
    }
53
54
    /**
55
     * @param Request $request
56
     * @param string $avatar
57
     * @return UserVO
58
     * @throws UserException
59
     * @Route("/user/avatar/{avatar}/", name="authenticate.setAvatar", methods="POST")
60
     */
61 2
    public function setAvatars(Request $request, $avatar) : UserVO
62
    {
63 2
        if (!in_array($avatar, UserVO::AVATARS, true)) {
64 1
            throw new UserException($this->translate('Invalid avatar: %s', $avatar));
65
        }
66
67
        /** @var UserVO $user */
68 1
        $user = $request->attributes->get('user');
69 1
        $user->avatar = $avatar;
70 1
        $this->userProvider->setUserProperty($user, UserVO::PROPERTY_AVATAR);
71
72 1
        return $user;
73
    }
74
75
    /**
76
     * @param Request $request
77
     * @return UserVO
78
     * @throws UserException
79
     * @Route("/user/change_email/", name="authenticate.setEmail", methods="POST")
80
     * @Guest
81
     */
82 1
    public function setEmail(Request $request) : UserVO
83
    {
84
        /** @var UserVO $user */
85 1
        $user = $request->attributes->get('user');
86 1
        $user->email = $request->request->get('email');
87 1
        $this->userProvider->setUserProperty($user, UserVO::PROPERTY_EMAIL);
88
89 1
        return $user;
90
    }
91
92
    /**
93
     * Receives a list of all registered user names. indexed by user-id
94
     *
95
     * @return string[]
96
     * @Route("/user/list/", name="authenticate.list_user", methods="GET", options={"cache":30})
97
     */
98 1
    public function getList() : array
99
    {
100 1
        return array_flip($this->userProvider->getAllUserNames());
101
    }
102
}
103