Passed
Push — 2.0 ( d495c9...3d053b )
by Kirill
03:10
created

AuthMutation::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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\Mutations;
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
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21
22
/**
23
 * Class AuthMutation
24
 * @package App\GraphQL\Mutations
25
 */
26
class AuthMutation extends AbstractMutation
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
     * AuthorisationQuery constructor.
42
     * @param array $attributes
43
     * @param TokenAuth $tokenAuth
44
     */
45
    public function __construct(array $attributes = [], TokenAuth $tokenAuth)
46
    {
47
        parent::__construct($attributes);
48
49
        $this->tokenAuth = $tokenAuth;
50
    }
51
52
    /**
53
     * @return ObjectType
54
     */
55
    public function type(): ObjectType
56
    {
57
        return \GraphQL::type(UserType::getName());
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function args(): array
64
    {
65
        return [
66
            'email'    => [
67
                'name' => 'email',
68
                'type' => Type::nonNull(Type::string()),
69
            ],
70
            'password' => [
71
                'name' => 'password',
72
                'type' => Type::nonNull(Type::string()),
73
            ],
74
            'remember' => [
75
                'name' => 'remember',
76
                'type' => Type::boolean(),
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)
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...
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 are not correct');
108
        }
109
110
        $user->token = $this->tokenAuth->fromUser($user);
0 ignored issues
show
Bug introduced by
Accessing token on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
111
112
        return UserSerializer::serialize($user);
113
    }
114
115
    /**
116
     * @param array $args
117
     * @return array
118
     */
119
    private function getEmailAndPassword(array $args = []): array
120
    {
121
        return [
122
            Arr::get($args, 'email', ''),
123
            Arr::get($args, 'password', ''),
124
        ];
125
    }
126
127
128
}