Completed
Push — development ( e85e87...4a3a46 )
by Claudio
02:21
created

MailController::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Mail as MailModel;
6
use App\Models\User;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Config;
10
use Illuminate\Support\Facades\Mail;
11
use Laravel\Lumen\Http\ResponseFactory;
12
use Laravel\Lumen\Routing\Controller as BaseController;
13
14
/**
15
 * Class MailController
16
 * @package App\Http\Controllers
17
 */
18
class MailController extends BaseController
19
{
20
    /**
21
     * Send an Email
22
     *
23
     * @param array $configuration
24
     */
25
    public function send(array $configuration)
26
    {
27
        Mail::send('habbo-web-mail.confirm-mail', $configuration, function ($message) use ($configuration) {
28
            $message->from(Config::get('chocolatey.contact'), Config::get('chocolatey.name'));
29
            $message->to($configuration['mail']);
30
        });
31
    }
32
33
    /**
34
     * Prepare the E-Mail
35
     *
36
     * @param string $email
37
     * @param string $url
38
     * @return string
39
     */
40
    public function prepare(string $email, string $url): string
41
    {
42
        (new MailModel)->store($token = uniqid('HabboMail', true), $url, $email)->save();
43
44
        return $token;
45
    }
46
47
    /**
48
     * Resend E-mail Verification
49
     *
50
     * @param Request $request
51
     * @return JsonResponse
52
     */
53
    public function verify(Request $request): JsonResponse
54
    {
55
        $this->send([
56
            'name' => $request->user()->name,
57
            'mail' => $request->user()->email,
58
            'url' => "/activate/{$this->prepare($request->user()->email, 'public/registration/activate')}"
59
        ]);
60
61
        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...
62
    }
63
64
    /**
65
     * Send User Forgot E-Mail
66
     *
67
     * @param Request $request
68
     * @return JsonResponse
69
     */
70
    public function forgotPassword(Request $request): JsonResponse
71
    {
72
        if (($user = User::where('mail', $request->json()->get('email'))->first()) == null)
73
            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...
74
75
        $this->send([
76
            'name' => $user->name,
77
            'mail' => $user->email,
78
            'url' => "/activate/{$this->prepare($user->email, 'public/forgotPassword')}"
79
        ]);
80
81
        return response()->json('');
82
    }
83
}
84