Test Setup Failed
Push — graphql_api ( 4ff753 )
by Herberto
07:48 queued 01:35
created

UserResolver::getUser()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Api\GraphQl\Node\User;
16
17
use Acme\App\Core\Port\Auth\Authentication\AuthenticationServiceInterface;
18
use Acme\App\Core\Port\Auth\Authentication\NoUserAuthenticatedException;
19
use Acme\App\Core\SharedKernel\Exception\AppRuntimeException;
20
use Acme\App\Presentation\Api\GraphQl\Node\User\Admin\AdminViewModel;
21
use Acme\App\Presentation\Api\GraphQl\Node\User\Editor\EditorViewModel;
22
use Acme\App\Presentation\Api\GraphQl\Node\User\Visitor\VisitorViewModel;
23
use function json_encode;
24
25
class UserResolver
26
{
27
    /**
28
     * @var AuthenticationServiceInterface
29
     */
30
    private $authenticationService;
31
32
    public function __construct(AuthenticationServiceInterface $authenticationService)
33
    {
34
        $this->authenticationService = $authenticationService;
35
    }
36
37
    public function getUser(): AbstractUserViewModel
38
    {
39
        try {
40
            $user = $this->authenticationService->getLoggedInUser();
41
        } catch (NoUserAuthenticatedException $e) {
42
            return VisitorViewModel::construct();
43
        }
44
45
        if ($user->isAdmin()) {
46
            return AdminViewModel::constructFromEntity($user);
47
        }
48
49
        if ($user->isEditor()) {
50
            return EditorViewModel::constructFromEntity($user);
51
        }
52
53
        throw new AppRuntimeException('Unknown user type, with roles: ' . json_encode($user->getRoles()));
54
    }
55
}
56