Completed
Push — development ( ed511f...d577ea )
by Claudio
02:44
created

AccountController::checkName()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 1
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
     * Save User Look.
25
     *
26
     * @param Request $request
27
     *
28
     * @return JsonResponse
29
     */
30
    public function saveLook(Request $request): JsonResponse
31
    {
32
        UserFacade::updateSession(['look' => $request->json()->get('figure'), 'gender' => $request->json()->get('gender')]);
33
34
        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...
35
    }
36
37
    /**
38
     * Get User Non Read Messenger Discussions.
39
     *
40
     * @TODO: Code Integration with HabboMessenger
41
     * @TODO: Create Messenger Model
42
     *
43
     * @return JsonResponse
44
     */
45
    public function getDiscussions(): JsonResponse
46
    {
47
        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...
48
    }
49
50
    /**
51
     * Get User Preferences.
52
     *
53
     * @return JsonResponse
54
     */
55
    public function getPreferences(): JsonResponse
56
    {
57
        $userPreferences = UserPreferences::find(UserFacade::getUser()->uniqueId);
58
59
        foreach ($userPreferences->getAttributes() as $attributeName => $attributeValue) {
60
            $userPreferences->{$attributeName} = $attributeValue == 1;
61
        }
62
63
        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...
64
    }
65
66
    /**
67
     * Save New User Preferences.
68
     *
69
     * @param Request $request
70
     *
71
     * @return Response
72
     */
73
    public function savePreferences(Request $request): Response
74
    {
75
        UserSettings::updateOrCreate(['user_id' => UserFacade::getUser()->uniqueId], [
76
            'block_following'      => $request->json()->get('friendCanFollow') == false ? '1' : '0',
77
            'block_friendrequests' => $request->json()->get('friendRequestEnabled') == false ? '1' : '0',
78
        ]);
79
80
        foreach ((array) $request->json()->all() as $setting => $value) {
81
            UserPreferences::find(UserFacade::getUser()->uniqueId)->update([$setting => $value == true ? '1' : '0']);
82
        }
83
84
        return response(null);
85
    }
86
87
    /**
88
     * Get All E-Mail Accounts.
89
     *
90
     * @return JsonResponse
91
     */
92
    public function getAvatars(): JsonResponse
93
    {
94
        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...
95
    }
96
97
    /**
98
     * Check if an Username is available
99
     * for a new Avatar Account.
100
     *
101
     * @param Request $request
102
     *
103
     * @return JsonResponse
104
     */
105
    public function checkName(Request $request): JsonResponse
106
    {
107
        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...
108
            && UserFacade::filterName($request->input('name') && !UserFacade::getUser()->isStaff))]);
109
    }
110
111
    /**
112
     * Create a New User Avatar.
113
     *
114
     * @param Request $request
115
     *
116
     * @return JsonResponse
117
     */
118
    public function createAvatar(Request $request): JsonResponse
119
    {
120
        if (User::where('username', $request->json()->get('name'))->count() == 0 && UserFacade::filterName($request->json()->get('name')) && !UserFacade::getUser()->isStaff) {
121
            $this->createUser($request, ['username' => $request->json()->get('name'), 'email' => UserFacade::getUser()->email, 'password' => openssl_random_pseudo_bytes(20)]);
122
123
            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...
124
        }
125
126
        return response()->json('');
127
    }
128
129
    /**
130
     * Create a New User.
131
     *
132
     * @param Request $request
133
     * @param array   $userInfo
134
     * @param bool    $newUser  If is a New User
135
     *
136
     * @return User
137
     */
138
    public function createUser(Request $request, array $userInfo, bool $newUser = false): User
139
    {
140
        $userName = $newUser ? $this->uniqueName($userInfo['email']) : $userInfo['username'];
141
142
        $token = Mail::store($userInfo['email'], 'public/registration/activate');
143
144
        Mail::send(['email' => $userInfo['email'], 'name' => $userName, 'url' => "/activate/{$token}", 'subject' => 'Welcome to '.Config::get('chocolatey.hotelName')]);
145
146
        return UserFacade::setSession((new User())->store($userName, $userInfo['password'], $userInfo['email'], $request->ip(), $newUser));
147
    }
148
149
    /**
150
     * Create Random Unique Username.
151
     *
152
     * @WARNING: Doesn't create Like Habbo Way
153
     *
154
     * @param string $userMail
155
     *
156
     * @return string
157
     */
158
    protected function uniqueName(string $userMail): string
159
    {
160
        $partialName = explode(' ', (new Alliteration())->getName());
161
162
        return strtolower($partialName[0].strstr($userMail, '@', true).$partialName[1]);
163
    }
164
165
    /**
166
     * Change Logged In User.
167
     *
168
     * @param Request $request
169
     */
170
    public function selectAvatar(Request $request)
171
    {
172
        UserFacade::setSession(User::find($request->json()->get('uniqueId')));
173
    }
174
175
    /**
176
     * Confirm E-Mail Activation.
177
     *
178
     * @param Request $request
179
     *
180
     * @return JsonResponse
181
     */
182
    public function confirmActivation(Request $request): JsonResponse
183
    {
184 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...
185
            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...
186
        }
187
188
        if (strpos(Mail::getMail()->link, 'change-email') !== false):
189
            $email = str_replace('change-email/', '', Mail::getMail()->link);
190
191
        User::where('mail', Mail::getMail()->mail)->update(['mail' => $email]);
192
193
        ChocolateyId::where('mail', Mail::getMail()->mail)->update(['mail' => $email]);
194
        endif;
195
196
        User::where('mail', Mail::getMail()->mail)->update(['mail_verified' => '1']);
197
198
        return response()->json(['email' => Mail::getMail()->mail, 'emailVerified' => true, 'identityVerified' => true]);
199
    }
200
201
    /**
202
     * Send User Forgot E-Mail.
203
     *
204
     * @param Request $request
205
     *
206
     * @return JsonResponse
207
     */
208
    public function forgotPassword(Request $request): JsonResponse
209
    {
210
        if (($user = User::where('mail', $request->json()->get('email'))->first()) == null) {
211
            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...
212
        }
213
214
        $token = Mail::store($user->email, 'public/forgotPassword');
215
216
        Mail::send(['name' => $user->name, 'email' => $user->email, 'subject' => 'Password reset confirmation',
217
            'url'          => "/reset-password/{$token}",
218
        ], 'habbo-web-mail.password-reset');
219
220
        return response()->json(['email' => $user->email]);
221
    }
222
223
    /**
224
     * Send an Account Confirmation E-Mail.
225
     *
226
     * @param Request $request
227
     *
228
     * @return Response
229
     */
230
    public function verifyAccount(Request $request): Response
231
    {
232
        $token = Mail::store(UserFacade::getUser()->email, 'public/registration/activate');
233
234
        Mail::send(['name' => UserFacade::getUser()->name, 'email' => $request->user()->email,
235
            'url'          => "/activate/{$token}", 'subject' => 'Welcome to '.Config::get('chocolatey.hotelName'),
236
        ]);
237
238
        return response(null);
239
    }
240
}
241