Completed
Push — development ( 9515cf...3cb16f )
by Claudio
02:12
created

AccountController::forgotPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Session;
6
use App\Models\ChocolateyId;
7
use App\Models\User;
8
use App\Models\UserPreferences;
9
use App\Models\UserSettings;
10
use Illuminate\Http\JsonResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\DB;
13
use Laravel\Lumen\Routing\Controller as BaseController;
14
15
/**
16
 * Class AccountController
17
 * @package App\Http\Controllers
18
 */
19
class AccountController extends BaseController
20
{
21
    /**
22
     * Check an User Name
23
     *
24
     * @param Request $request
25
     * @return JsonResponse
26
     */
27
    public function checkName(Request $request): JsonResponse
28
    {
29
        if (User::where('username', $request->json()->get('name'))->count() > 0)
30
            return response()->json(['code' => 'NAME_IN_USE', 'validationResult' => null, 'suggestions' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
31
32
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
33
    }
34
35
    /**
36
     * Select an User Name
37
     *
38
     * @param Request $request
39
     * @return JsonResponse
40
     */
41
    public function selectName(Request $request): JsonResponse
42
    {
43
        $request->user()->update(['username' => $request->json()->get('name')]);
44
45
        Session::set('ChocolateyWEB', $request->user());
46
47
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
    }
49
50
    /**
51
     * Save User Look
52
     *
53
     * @param Request $request
54
     * @return JsonResponse
55
     */
56
    public function saveLook(Request $request): JsonResponse
57
    {
58
        $request->user()->update([
59
            'look' => $request->json()->get('figure'),
60
            'gender' => $request->json()->get('gender')]);
61
62
        Session::set('ChocolateyWEB', $request->user());
63
64
        return response()->json($request->user());
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
    }
66
67
    /**
68
     * Select a Room
69
     *
70
     * @TODO: Generate the Room for the User
71
     * @TODO: Get Room Models.
72
     *
73
     * @param Request $request
74
     * @return JsonResponse
75
     */
76
    public function selectRoom(Request $request): JsonResponse
77
    {
78
        if (!in_array($request->json()->get('roomIndex'), [1, 2, 3]))
79
            return response('', 400);
80
81
        $request->user()->traits = ["USER"];
82
83
        return response()->json('');
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
    }
85
86
    /**
87
     * Get User Non Read Messenger Discussions
88
     *
89
     * @TODO: Code Integration with HabboMessenger
90
     * @TODO: Create Messenger Model
91
     *
92
     * @return JsonResponse
93
     */
94
    public function getDiscussions(): JsonResponse
95
    {
96
        return response()->json([]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
97
    }
98
99
    /**
100
     * Get User Preferences
101
     *
102
     * @param Request $request
103
     * @return JsonResponse
104
     */
105
    public function getPreferences(Request $request): JsonResponse
106
    {
107
        $userPreferences = UserPreferences::find($request->user()->uniqueId);
108
109
        foreach ($userPreferences->getAttributes() as $attributeName => $attributeValue)
110
            $userPreferences->{$attributeName} = $attributeValue == 1;
111
112
        return response()->json($userPreferences);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
113
    }
114
115
    /**
116
     * Save New User Preferences
117
     *
118
     * @param Request $request
119
     * @return JsonResponse
120
     */
121
    public function savePreferences(Request $request): JsonResponse
122
    {
123
        UserSettings::updateOrCreate([
124
            'user_id' => $request->user()->uniqueId,
125
            'block_following' => $request->json()->get('friendCanFollow') == false,
126
            'block_friendrequests' => $request->json()->get('friendRequestEnabled') == false
127
        ]);
128
129
        UserPreferences::find($request->user()->uniqueId)->update((array)$request->json()->all());
130
131
        return response()->json('');
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
132
    }
133
134
    /**
135
     * Get All E-Mail Accounts
136
     *
137
     * @param Request $request
138
     * @return JsonResponse
139
     */
140
    public function getAvatars(Request $request): JsonResponse
141
    {
142
        $azureIdAccounts = ChocolateyId::where('mail', $request->user()->email)->first();
143
144
        return response()->json($azureIdAccounts->relatedAccounts);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
145
    }
146
147
    /**
148
     * Check if an Username is available
149
     * for a new Avatar Account
150
     *
151
     * @param Request $request
152
     * @return JsonResponse
153
     */
154
    public function checkNewName(Request $request): JsonResponse
155
    {
156 View Code Duplication
        if (User::where('username', $request->input('name'))->count() > 0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
            return response()->json(['isAvailable' => false]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
159
        return response()->json(['isAvailable' => true]);
160
    }
161
162
    /**
163
     * Create a New User Avatar
164
     *
165
     * @param Request $request
166
     * @return JsonResponse
167
     */
168
    public function createAvatar(Request $request): JsonResponse
169
    {
170 View Code Duplication
        if (User::where('username', $request->json()->get('name'))->count() > 0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
            return response()->json(['isAvailable' => false]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
172
173
        $request->user()->name = $request->json()->get('name');
174
175
        $this->createUser($request, $request->user()->getAttributes());
176
177
        return response()->json('');
178
    }
179
180
    /**
181
     * Create a New User
182
     *
183
     * @param Request $request
184
     * @param array $userInfo
185
     * @param bool $newUser If is a New User
186
     * @return User
187
     */
188
    public function createUser(Request $request, array $userInfo, bool $newUser = false): User
189
    {
190
        $userName = $newUser ? uniqid(strstr($userInfo['email'], '@', true)) : $userInfo['username'];
191
        $userMail = $newUser ? $userInfo['email'] : $userInfo['mail'];
192
193
        $mailController = new MailController;
194
195
        $mailController->send([
196
            'mail' => $userMail,
197
            'name' => $userName,
198
            'url' => "/activate/{$mailController
199
            ->prepare($userMail, 'public/registration/activate')}"
200
        ]);
201
202
        $userData = new User;
203
        $userData->store($userName, $userInfo['password'], $userMail, $request->ip())->save();
204
        $userData->createData();
205
206
        Session::set('ChocolateyWEB', $userData);
207
208
        return $userData;
209
    }
210
211
    /**
212
     * Change Logged In User
213
     *
214
     * @param Request $request
215
     */
216
    public function selectAvatar(Request $request)
217
    {
218
        $userData = User::find($request->json()->get('uniqueId'));
219
220
        Session::set('ChocolateyWEB', $userData);
221
    }
222
223
    /**
224
     * Confirm E-Mail Activation
225
     *
226
     * @param Request $request
227
     * @return JsonResponse
228
     */
229
    public function confirmActivation(Request $request): JsonResponse
230
    {
231
        if (($mailRequest = (new MailController)->getMail($request->json()->get('token'))) == null)
232
            return response()->json(['error' => 'activation.invalid_token'], 400);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
233
234
        if (strpos($mailRequest->link, 'change-email') !== false):
235
            $mail = str_replace('change-email/', '', $mailRequest->link);
236
237
            DB::table('users')->where('mail', $mailRequest->mail)->update(['mail' => $mail]);
238
            DB::table('chocolatey_users_id')->where('mail', $mailRequest->mail)->update(['mail' => $mail]);
239
        endif;
240
241
        DB::table('users')->where('mail', $mailRequest->mail)->update(['mail_verified' => 1]);
242
243
        if ($request->user() !== null)
244
            $request->user()->emailVerified = true;
245
246
        return response()->json(['email' => $mailRequest->mail, 'emailVerified' => true, 'identityVerified' => true]);
247
    }
248
249
    /**
250
     * Send User Forgot E-Mail
251
     *
252
     * @param Request $request
253
     * @return JsonResponse
254
     */
255
    public function forgotPassword(Request $request): JsonResponse
256
    {
257
        if (($user = User::where('mail', $request->json()->get('email'))->first()) == null)
258
            return response()->json(['email' => $request->json()->get('email')]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
259
260
        $mailController = new MailController;
261
262
        $mailController->send([
263
            'name' => $user->name,
264
            'mail' => $user->email,
265
            'url' => "/reset-password/{$mailController->prepare($user->email, 'public/forgotPassword')}"
266
        ], 'habbo-web-mail.password-reset');
267
268
        return response()->json(['email' => $user->email]);
269
    }
270
}
271