AuthQuery::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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\Services\TokenAuth;
12
use Illuminate\Support\Arr;
13
use GraphQL\Type\Definition\Type;
14
use App\GraphQL\Types\AuthUserType;
15
use Illuminate\Contracts\Auth\Guard;
16
use GraphQL\Type\Definition\ObjectType;
17
use Illuminate\Contracts\Auth\StatefulGuard;
18
use App\GraphQL\Serializers\AuthUserSerializer;
19
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
20
21
/**
22
 * Class AuthMutation.
23
 */
24
class AuthQuery extends AbstractQuery
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $attributes = [
30
        'name' => 'auth',
31
    ];
32
33
    /**
34
     * @var TokenAuth
35
     */
36
    private $tokenAuth;
37
38
    /**
39
     * @var StatefulGuard|Guard
40
     */
41
    private $guard;
42
43
    /**
44
     * AuthMutation constructor.
45
     * @param Guard $guard
46
     * @param TokenAuth $tokenAuth
47
     */
48
    public function __construct(Guard $guard, TokenAuth $tokenAuth)
49
    {
50
        parent::__construct([]);
51
52
        $this->tokenAuth = $tokenAuth;
53
        $this->guard = $guard;
54
    }
55
56
    /**
57
     * @return ObjectType
58
     */
59
    public function type(): ObjectType
60
    {
61
        return \GraphQL::type('AuthUser');
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function queryArguments(): array
68
    {
69
        return [
70
            'email'    => [
71
                'name' => 'email',
72
                'type' => Type::nonNull(Type::string()),
73
            ],
74
            'password' => [
75
                'name' => 'password',
76
                'type' => Type::nonNull(Type::string()),
77
            ],
78
        ];
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function rules(): array
85
    {
86
        return [
87
            'email' => [
88
                'email',
89
                'exists:users,email',
90
            ],
91
        ];
92
    }
93
94
    /**
95
     * @param $root
96
     * @param array $args
97
     * @return array
98
     * @throws AccessDeniedHttpException
99
     */
100
    public function resolve($root, $args)
101
    {
102
        [$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...
103
104
        $user = $this->tokenAuth->attemptFromEmailAndPassword($email, $password);
105
106
        if (! $user) {
107
            throw new AccessDeniedHttpException('User password is not correct');
108
        }
109
110
        return AuthUserSerializer::serialize($user);
111
    }
112
113
    /**
114
     * @param array $args
115
     * @return array
116
     */
117
    private function getEmailAndPassword(array $args = []): array
118
    {
119
        return [
120
            Arr::get($args, 'email', ''),
121
            Arr::get($args, 'password', ''),
122
        ];
123
    }
124
}
125