Completed
Push — development ( b15f63...e85e87 )
by Claudio
02:46
created

AccountController::createAvatar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 2
Ratio 18.18 %

Importance

Changes 0
Metric Value
dl 2
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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\Mail;
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\Support\Facades\DB;
14
use Laravel\Lumen\Routing\Controller as BaseController;
15
16
/**
17
 * Class AccountController
18
 * @package App\Http\Controllers
19
 */
20
class AccountController extends BaseController
21
{
22
    /**
23
     * Check an User Name
24
     *
25
     * @param Request $request
26
     * @return JsonResponse
27
     */
28
    public function checkName(Request $request): JsonResponse
29
    {
30
        if (User::where('username', $request->json()->get('name'))->count() > 0)
31
            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...
32
33
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
34
    }
35
36
    /**
37
     * Select an User Name
38
     *
39
     * @param Request $request
40
     * @return JsonResponse
41
     */
42
    public function selectName(Request $request): JsonResponse
43
    {
44
        $request->user()->update(['username' => $request->json()->get('name')]);
45
46
        Session::set('ChocolateyWEB', $request->user());
47
48
        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...
49
    }
50
51
    /**
52
     * Save User Look
53
     *
54
     * @param Request $request
55
     * @return JsonResponse
56
     */
57
    public function saveLook(Request $request): JsonResponse
58
    {
59
        $request->user()->update([
60
            'look' => $request->json()->get('figure'),
61
            'gender' => $request->json()->get('gender')]);
62
63
        Session::set('ChocolateyWEB', $request->user());
64
65
        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...
66
    }
67
68
    /**
69
     * Select a Room
70
     *
71
     * @TODO: Generate the Room for the User
72
     * @TODO: Get Room Models.
73
     *
74
     * @param Request $request
75
     * @return JsonResponse
76
     */
77
    public function selectRoom(Request $request): JsonResponse
78
    {
79
        if (!in_array($request->json()->get('roomIndex'), [1, 2, 3]))
80
            return response('', 400);
81
82
        $request->user()->traits = ["USER"];
83
84
        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...
85
    }
86
87
    /**
88
     * Get User Non Read Messenger Discussions
89
     *
90
     * @TODO: Code Integration with HabboMessenger
91
     * @TODO: Create Messenger Model
92
     *
93
     * @return JsonResponse
94
     */
95
    public function getDiscussions(): JsonResponse
96
    {
97
        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...
98
    }
99
100
    /**
101
     * Get User Preferences
102
     *
103
     * @param Request $request
104
     * @return JsonResponse
105
     */
106
    public function getPreferences(Request $request): JsonResponse
107
    {
108
        $userPreferences = UserPreferences::find($request->user()->uniqueId);
109
110
        foreach ($userPreferences->getAttributes() as $attributeName => $attributeValue)
111
            $userPreferences->{$attributeName} = $attributeValue == 1;
112
113
        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...
114
    }
115
116
    /**
117
     * Save New User Preferences
118
     *
119
     * @param Request $request
120
     * @return JsonResponse
121
     */
122
    public function savePreferences(Request $request): JsonResponse
123
    {
124
        UserSettings::updateOrCreate([
125
            'user_id' => $request->user()->uniqueId,
126
            'block_following' => $request->json()->get('friendCanFollow') == false,
127
            'block_friendrequests' => $request->json()->get('friendRequestEnabled') == false
128
        ]);
129
130
        UserPreferences::find($request->user()->uniqueId)->update((array)$request->json()->all());
131
132
        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...
133
    }
134
135
    /**
136
     * Get All E-Mail Accounts
137
     *
138
     * @param Request $request
139
     * @return JsonResponse
140
     */
141
    public function getAvatars(Request $request): JsonResponse
142
    {
143
        $azureIdAccounts = ChocolateyId::where('mail', $request->user()->email)->first();
144
145
        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...
146
    }
147
148
    /**
149
     * Check if an Username is available
150
     * for a new Avatar Account
151
     *
152
     * @param Request $request
153
     * @return JsonResponse
154
     */
155
    public function checkNewName(Request $request): JsonResponse
156
    {
157 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...
158
            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...
159
160
        return response()->json(['isAvailable' => true]);
161
    }
162
163
    /**
164
     * Create a New User Avatar
165
     *
166
     * @param Request $request
167
     * @return JsonResponse
168
     */
169
    public function createAvatar(Request $request): JsonResponse
170
    {
171 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...
172
            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...
173
174
        $request->user()->name = $request->json()->get('name');
175
176
        $this->createUser($request, $request->user()->getAttributes());
177
178
        return response()->json('');
179
    }
180
181
    /**
182
     * Create a New User
183
     *
184
     * @param Request $request
185
     * @param array $userInfo
186
     * @param bool $newUser If is a New User
187
     * @return User
188
     */
189
    public function createUser(Request $request, array $userInfo, bool $newUser = false): User
190
    {
191
        $userName = $newUser ? uniqid(strstr($userInfo['email'], '@', true)) : $userInfo['username'];
192
        $userMail = $newUser ? $userInfo['email'] : $userInfo['mail'];
193
194
        $userData = new User;
195
        $userData->store($userName, $userInfo['password'], $userMail, $request->ip())->save();
196
        $userData->createData();
197
198
        Session::set('ChocolateyWEB', $userData);
199
200
        return $userData;
201
    }
202
203
    /**
204
     * Change Logged In User
205
     *
206
     * @param Request $request
207
     */
208
    public function selectAvatar(Request $request)
209
    {
210
        $userData = User::find($request->json()->get('uniqueId'));
211
212
        Session::set('ChocolateyWEB', $userData);
213
    }
214
215
    /**
216
     * Confirm E-Mail Activation
217
     *
218
     * @param Request $request
219
     * @return JsonResponse
220
     */
221
    public function confirmActivation(Request $request): JsonResponse
222
    {
223
        $mailRequest = Mail::where('token', $request->json()->get('token'))->where('used', '0')->first();
224
225
        if ($mailRequest == null)
226
            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...
227
228
        $mailRequest->update(['used' => '1']);
229
230
        DB::table('users')->where('mail', $mailRequest->mail)->update(['mail_verified' => 1]);
231
232
        if($request->user() !== null)
233
            $request->user()->mailVerified = true;
234
235
        return response()->json(['email' => $mailRequest->mail, 'emailVerified' => true, 'identityVerified' => true]);
236
    }
237
}
238