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

MailController::forgotPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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