Completed
Push — development ( f57bd7...a7d33c )
by Claudio
04:40
created

AccountController::selectRoom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Mail;
6
use App\Facades\User as UserFacade;
7
use App\Models\ChocolateyId;
8
use App\Models\User;
9
use App\Models\UserPreferences;
10
use App\Models\UserSettings;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\Request;
13
use Illuminate\Http\Response;
14
use Illuminate\Support\Facades\Config;
15
use Laravel\Lumen\Routing\Controller as BaseController;
16
use Nubs\RandomNameGenerator\Alliteration;
17
18
/**
19
 * Class AccountController.
20
 */
21
class AccountController extends BaseController
22
{
23
    /**
24
     * Check an User Name.
25
     *
26
     * @param Request $request
27
     *
28
     * @return JsonResponse
29
     */
30
    public function checkName(Request $request): JsonResponse
31
    {
32 View Code Duplication
        if (User::where('username', $request->json()->get('name'))->count() > 0 && $request->json()->get('name') != $request->user()->name) {
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...
33
            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...
34
        }
35
36 View Code Duplication
        if (strlen($request->json()->get('name')) > 50 || !$this->filterName($request->json()->get('name'))) {
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...
37
            return response()->json(['code' => 'INVALID_NAME', 'validationResult' => ['resultType' => 'VALIDATION_ERROR_ILLEGAL_WORDS'], 'suggestions' => []]);
38
        }
39
40
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
41
    }
42
43
    /**
44
     * Filter an Username from the Invalid Names Base.
45
     *
46
     * @param string $userName
47
     *
48
     * @return bool
49
     */
50
    protected function filterName(string $userName): bool
51
    {
52
        return count(array_filter(Config::get('chocolatey.invalid'), function ($username) use ($userName) {
53
                return stripos($userName, $username) !== false;
54
            })) == 0;
55
    }
56
57
    /**
58
     * Select an User Name.
59
     *
60
     * @param Request $request
61
     *
62
     * @return JsonResponse
63
     */
64
    public function selectName(Request $request): JsonResponse
65
    {
66
        UserFacade::updateUser(['username' => $request->json()->get('name')]);
67
68
        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...
69
    }
70
71
    /**
72
     * Save User Look.
73
     *
74
     * @param Request $request
75
     *
76
     * @return JsonResponse
77
     */
78
    public function saveLook(Request $request): JsonResponse
79
    {
80
        UserFacade::updateSession(['look' => $request->json()->get('figure'), 'gender' => $request->json()->get('gender')]);
81
82
        return response()->json(UserFacade::getUser());
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...
83
    }
84
85
    /**
86
     * Get User Non Read Messenger Discussions.
87
     *
88
     * @TODO: Code Integration with HabboMessenger
89
     * @TODO: Create Messenger Model
90
     *
91
     * @return JsonResponse
92
     */
93
    public function getDiscussions(): JsonResponse
94
    {
95
        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...
96
    }
97
98
    /**
99
     * Get User Preferences.
100
     *
101
     * @return JsonResponse
102
     */
103
    public function getPreferences(): JsonResponse
104
    {
105
        $userPreferences = UserPreferences::find(UserFacade::getUser()->uniqueId);
106
107
        foreach ($userPreferences->getAttributes() as $attributeName => $attributeValue) {
108
            $userPreferences->{$attributeName} = $attributeValue == 1;
109
        }
110
111
        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...
112
    }
113
114
    /**
115
     * Save New User Preferences.
116
     *
117
     * @param Request $request
118
     *
119
     * @return Response
120
     */
121
    public function savePreferences(Request $request): Response
122
    {
123
        UserSettings::updateOrCreate(['user_id' => UserFacade::getUser()->uniqueId], [
124
            'block_following' => $request->json()->get('friendCanFollow') == false ? '1' : '0',
125
            'block_friendrequests' => $request->json()->get('friendRequestEnabled') == false ? '1' : '0',
126
        ]);
127
128
        foreach ((array)$request->json()->all() as $setting => $value) {
129
            UserPreferences::find(UserFacade::getUser()->uniqueId)->update([$setting => $value == true ? '1' : '0']);
130
        }
131
132
        return response(null);
133
    }
134
135
    /**
136
     * Get All E-Mail Accounts.
137
     *
138
     * @return JsonResponse
139
     */
140
    public function getAvatars(): JsonResponse
141
    {
142
        return response()->json(ChocolateyId::where('mail', UserFacade::getUser()->email)->first()->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...
143
    }
144
145
    /**
146
     * Check if an Username is available
147
     * for a new Avatar Account.
148
     *
149
     * @param Request $request
150
     *
151
     * @return JsonResponse
152
     */
153
    public function checkNewName(Request $request): JsonResponse
154
    {
155
        return response()->json(['isAvailable' => (User::where('username', $request->input('name'))->count() == 0
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...
156
            && UserFacade::filterName($request->input('name') && !UserFacade::getUser()->isStaff))]);
157
    }
158
159
    /**
160
     * Create a New User Avatar.
161
     *
162
     * @param Request $request
163
     *
164
     * @return JsonResponse
165
     */
166
    public function createAvatar(Request $request): JsonResponse
167
    {
168
        if (User::where('username', $request->json()->get('name'))->count() == 0 && UserFacade::filterName($request->json()->get('name')) && !UserFacade::getUser()->isStaff) {
169
            $this->createUser($request, ['username' => $request->json()->get('name'), 'email' => UserFacade::getUser()->email, 'password' => openssl_random_pseudo_bytes(20)]);
170
171
            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...
172
        }
173
174
        return response()->json('');
175
    }
176
177
    /**
178
     * Create a New User.
179
     *
180
     * @param Request $request
181
     * @param array $userInfo
182
     * @param bool $newUser If is a New User
183
     *
184
     * @return User
185
     */
186
    public function createUser(Request $request, array $userInfo, bool $newUser = false): User
187
    {
188
        $userName = $newUser ? $this->uniqueName($userInfo['email']) : $userInfo['username'];
189
190
        $token = Mail::store($userInfo['email'], 'public/registration/activate');
191
192
        Mail::send(['email' => $userInfo['email'], 'name' => $userName, 'url' => "/activate/{$token}", 'subject' => 'Welcome to ' . Config::get('chocolatey.hotelName')]);
193
194
        return UserFacade::setSession((new User())->store($userName, $userInfo['password'], $userInfo['email'], $request->ip(), $newUser));
195
    }
196
197
    /**
198
     * Create Random Unique Username.
199
     *
200
     * @WARNING: Doesn't create Like Habbo Way
201
     *
202
     * @param string $userMail
203
     *
204
     * @return string
205
     */
206
    protected function uniqueName(string $userMail): string
207
    {
208
        $partialName = explode(' ', (new Alliteration())->getName());
209
210
        return strtolower($partialName[0] . strstr($userMail, '@', true) . $partialName[1]);
211
    }
212
213
    /**
214
     * Change Logged In User.
215
     *
216
     * @param Request $request
217
     */
218
    public function selectAvatar(Request $request)
219
    {
220
        UserFacade::setSession(User::find($request->json()->get('uniqueId')));
221
    }
222
223
    /**
224
     * Confirm E-Mail Activation.
225
     *
226
     * @param Request $request
227
     *
228
     * @return JsonResponse
229
     */
230
    public function confirmActivation(Request $request): JsonResponse
231
    {
232 View Code Duplication
        if (Mail::getByToken($request->json()->get('token')) == null) {
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...
233
            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...
234
        }
235
236
        if (strpos(Mail::getMail()->link, 'change-email') !== false):
237
            $email = str_replace('change-email/', '', Mail::getMail()->link);
238
239
            User::where('mail', Mail::getMail()->mail)->update(['mail' => $email]);
240
241
            ChocolateyId::where('mail', Mail::getMail()->mail)->update(['mail' => $email]);
242
        endif;
243
244
        User::where('mail', Mail::getMail()->mail)->update(['mail_verified' => '1']);
245
246
        return response()->json(['email' => Mail::getMail()->mail, 'emailVerified' => true, 'identityVerified' => true]);
247
    }
248
249
    /**
250
     * Send User Forgot E-Mail.
251
     *
252
     * @param Request $request
253
     *
254
     * @return JsonResponse
255
     */
256
    public function forgotPassword(Request $request): JsonResponse
257
    {
258
        if (($user = User::where('mail', $request->json()->get('email'))->first()) == null) {
259
            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...
260
        }
261
262
        $token = Mail::store($user->email, 'public/forgotPassword');
263
264
        Mail::send(['name' => $user->name, 'email' => $user->email, 'subject' => 'Password reset confirmation',
265
            'url' => "/reset-password/{$token}",
266
        ], 'habbo-web-mail.password-reset');
267
268
        return response()->json(['email' => $user->email]);
269
    }
270
271
    /**
272
     * Send an Account Confirmation E-Mail.
273
     *
274
     * @param Request $request
275
     *
276
     * @return Response
277
     */
278
    public function verifyAccount(Request $request): Response
279
    {
280
        $token = Mail::store(UserFacade::getUser()->email, 'public/registration/activate');
281
282
        Mail::send(['name' => UserFacade::getUser()->name, 'email' => $request->user()->email,
283
            'url' => "/activate/{$token}", 'subject' => 'Welcome to ' . Config::get('chocolatey.hotelName'),
284
        ]);
285
286
        return response(null);
287
    }
288
}
289