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

AdminViewModel::constructFromEntity()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 1
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\Admin;
16
17
use Acme\App\Core\Component\User\Domain\User\User;
18
use Acme\App\Core\SharedKernel\Exception\AppRuntimeException;
19
use Acme\App\Presentation\Api\GraphQl\Node\User\AbstractUserViewModel;
20
use function json_encode;
21
22
final class AdminViewModel extends AbstractUserViewModel
23
{
24
    private const TYPE = 'Admin';
25
26
    /**
27
     * @var string
28
     */
29
    private $adminViewModelClass = __CLASS__;
30
31
    public static function constructFromEntity(User $user): self
32
    {
33
        if (!$user->isAdmin()) {
34
            throw new AppRuntimeException(
35
                'User must be ' . self::TYPE . ', but it is: ' . json_encode($user->getRoles())
36
            );
37
        }
38
39
        return new static(
40
            $user->getId()->toScalar(),
41
            $user->getFullName(),
42
            $user->getUsername(),
43
            $user->getEmail(),
44
            $user->getMobile(),
45
            $user->getPassword(),
46
            self::TYPE
47
        );
48
    }
49
50
    public function getAdminViewModelClass(): string
51
    {
52
        return $this->adminViewModelClass;
53
    }
54
}
55