Completed
Push — master ( 6ae6f0...a2576c )
by Matze
04:01
created

UserController::getAvatars()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BrainExe\Core\Authentication\Controller;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Core\Annotations\Controller;
7
use BrainExe\Core\Annotations\Guest;
8
use BrainExe\Core\Annotations\Route;
9
use BrainExe\Core\Application\UserException;
10
use BrainExe\Core\Authentication\DatabaseUserProvider;
11
use BrainExe\Core\Authentication\UserVO;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * @Controller
16
 */
17
class UserController
18
{
19
    /**
20
     * @var DatabaseUserProvider
21
     */
22
    private $userProvider;
23
24
    /**
25
     * @Inject("@DatabaseUserProvider")
26
     * @param DatabaseUserProvider $userProvider
27 2
     */
28
    public function __construct(DatabaseUserProvider $userProvider)
29 2
    {
30 2
        $this->userProvider = $userProvider;
31
    }
32
33
    /**
34
     * @param Request $request
35
     * @return UserVO
36
     * @Route("/user/", name="authenticate.current_user", methods="GET")
37
     * @Guest
38 1
     */
39
    public function getCurrentUser(Request $request)
40 1
    {
41
        return $request->attributes->get('user');
42
    }
43
44
    /**
45
     * @return string[]
46
     * @Route("/user/avatar/", name="authenticate.avatars", methods="GET")
47
     * @Guest
48
     */
49 1
    public function getAvatars()
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
     * @Guest
61
     */
62
    public function setAvatars(Request $request, $avatar)
63
    {
64
        if (!in_array($avatar, UserVO::AVATARS)) {
65
            throw new UserException(sprintf(_('Invalid avatar: %s'), $avatar));
66
        }
67
        /** @var UserVO $user */
68
        $user = $request->attributes->get('user');
69
        $user->avatar = $avatar;
70
        $this->userProvider->setUserProperty($user, UserVO::PROPERTY_AVATAR);
71
72
        return $user;
73
    }
74
75
    /**
76
     * Receives a list all all registered users. indexed by user-id
77
     *
78
     * @return string[]
79
     * @Route("/user/list/", name="authenticate.list_user", methods="GET")
80
     */
81
    public function getList()
82
    {
83
        return array_flip($this->userProvider->getAllUserNames());
84
    }
85
}
86