Completed
Push — 2.0 ( be6014...b83469 )
by Kirill
03:33 queued 27s
created

AuthorisationQuery::rules()   A

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\Models\User;
12
use App\Services\TokenAuth;
13
use Illuminate\Support\Arr;
14
use App\GraphQL\Types\UserType;
15
use GraphQL\Type\Definition\Type;
16
use GraphQL\Type\Definition\ObjectType;
17
use App\GraphQL\Serializers\UserSerializer;
18
use Illuminate\Contracts\Auth\Authenticatable;
19
use Illuminate\Contracts\Validation\Validator;
20
21
/**
22
 * Class AuthorisationQuery
23
 * @package App\GraphQL\Mutations
24
 */
25
class AuthorisationQuery extends AbstractQuery
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $attributes = [
31
        'name' => 'authorization',
32
    ];
33
34
    /**
35
     * @var TokenAuth
36
     */
37
    private $tokenAuth;
38
39
    /**
40
     * AuthorisationQuery constructor.
41
     * @param array $attributes
42
     * @param TokenAuth $tokenAuth
43
     */
44
    public function __construct(array $attributes = [], TokenAuth $tokenAuth)
45
    {
46
        parent::__construct($attributes);
47
48
        $this->tokenAuth = $tokenAuth;
49
    }
50
51
    /**
52
     * @return ObjectType
53
     */
54
    public function type(): ObjectType
55
    {
56
        return \GraphQL::type(UserType::getName());
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function queryArguments(): array
63
    {
64
        return [
65
            'email'    => [
66
                'name' => 'email',
67
                'type' => Type::string(),
68
            ],
69
            'password' => [
70
                'name' => 'password',
71
                'type' => Type::string(),
72
            ],
73
        ];
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function rules(): array
80
    {
81
        return [
82
            'email'    => [
83
                'email',
84
                'exists:users,email',
85
            ]
86
        ];
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function messages(): array
93
    {
94
        return [
95
            'email.required'    => 'Укажите email',
96
            'password.required' => 'Укажите пароль',
97
        ];
98
    }
99
100
    /**
101
     * @param Validator $validator
102
     * @param array $args
103
     * @return \Generator|null
104
     */
105
    public function validate(Validator $validator, array $args = []): ?\Generator
106
    {
107
        [$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...
108
109
        if ($email && $password) {
110
            $user = $this->tokenAuth->attemptFromEmailAndPassword($email, $password);
111
112
            if ($user === null) {
113
                yield 'password' => 'Invalid user password';
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param array $args
120
     * @return array
121
     */
122
    private function getEmailAndPassword(array $args = []): array
123
    {
124
        return [
125
            Arr::get($args, 'email'),
126
            Arr::get($args, 'password')
127
        ];
128
    }
129
130
    /**
131
     * @param $root
132
     * @param $args
133
     * @return array
134
     * @throws \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException
135
     */
136
    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...
137
    {
138
        $user = $this->getUser($args);
139
        $user->token = $this->tokenAuth->fromUser($user);
140
141
        return UserSerializer::serialize($user);
142
    }
143
144
    /**
145
     * @param array $args
146
     * @return User|Authenticatable
147
     */
148
    private function getUser(array $args): User
149
    {
150
        [$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...
151
152
        if ($email && $password) {
153
            return $this->tokenAuth->attemptFromEmailAndPassword($email, $password);
154
        }
155
156
        return $this->tokenAuth->guest();
157
    }
158
159
}