Completed
Push — 2.0 ( b567d9...afe19a )
by Kirill
02:58
created

AuthQuery::resolve()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 2
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\GraphQL\Queries;
10
11
use App\GraphQL\Serializers\AuthUserSerializer;
12
use App\Services\TokenAuth;
13
use Illuminate\Contracts\Auth\Guard;
14
use Illuminate\Contracts\Auth\StatefulGuard;
15
use Illuminate\Support\Arr;
16
use GraphQL\Type\Definition\Type;
17
use App\GraphQL\Types\AuthUserType;
18
use GraphQL\Type\Definition\ObjectType;
19
use App\GraphQL\Serializers\UserSerializer;
20
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21
22
/**
23
 * Class AuthMutation
24
 * @package App\GraphQL\Mutations
25
 */
26
class AuthQuery extends AbstractQuery
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $attributes = [
32
        'name' => 'auth',
33
    ];
34
35
    /**
36
     * @var TokenAuth
37
     */
38
    private $tokenAuth;
39
40
    /**
41
     * @var StatefulGuard|Guard
42
     */
43
    private $guard;
44
45
    /**
46
     * AuthMutation constructor.
47
     * @param array $attributes
48
     * @param Guard $guard
49
     * @param TokenAuth $tokenAuth
50
     */
51
    public function __construct(array $attributes = [], Guard $guard, TokenAuth $tokenAuth)
52
    {
53
        parent::__construct($attributes);
54
55
        $this->tokenAuth = $tokenAuth;
56
        $this->guard = $guard;
57
    }
58
59
    /**
60
     * @return ObjectType
61
     */
62
    public function type(): ObjectType
63
    {
64
        return \GraphQL::type(AuthUserType::getName());
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function queryArguments(): array
71
    {
72
        return [
73
            'email'    => [
74
                'name' => 'email',
75
                'type' => Type::nonNull(Type::string()),
76
            ],
77
            'password' => [
78
                'name' => 'password',
79
                'type' => Type::nonNull(Type::string()),
80
            ],
81
            'remember' => [
82
                'name' => 'remember',
83
                'type' => Type::boolean(),
84
            ],
85
        ];
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function rules(): array
92
    {
93
        return [
94
            'email' => [
95
                'email',
96
                'exists:users,email',
97
            ],
98
        ];
99
    }
100
101
    /**
102
     * @param $root
103
     * @param array $args
104
     * @return array
105
     * @throws AccessDeniedHttpException
106
     */
107
    public function resolve($root, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $root is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        [$email, $password] = $this->getEmailAndPassword($args);
0 ignored issues
show
Bug introduced by
The variable $email does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $password does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
110
111
        $user = $this->tokenAuth->attemptFromEmailAndPassword($email, $password);
112
113
        if (! $user) {
114
            throw new AccessDeniedHttpException('User password are not correct');
115
        }
116
117
        if (isset($args['remember']) && $this->guard instanceof StatefulGuard) {
118
            $this->guard->login($user, (bool)$args['remember']);
119
        }
120
121
        return AuthUserSerializer::serialize($user);
122
    }
123
124
    /**
125
     * @param array $args
126
     * @return array
127
     */
128
    private function getEmailAndPassword(array $args = []): array
129
    {
130
        return [
131
            Arr::get($args, 'email', ''),
132
            Arr::get($args, 'password', ''),
133
        ];
134
    }
135
}