Completed
Push — master ( 4c7a20...bb56b8 )
by Sergi Tur
01:43
created

APIForgotPasswordController::sendResetLinkEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 15
loc 15
rs 9.4285
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Acacha\Users\Http\Controllers\Controller.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Password;
10
11
/**
12
 * Class APIForgotPasswordController.
13
 *
14
 * @package Acacha\Users\Http\Controllers
15
 */
16
class APIForgotPasswordController extends Controller
17
{
18
    /**
19
     * Send a reset link to the given user.
20
     *
21
     * @param Request $request
22
     * @return JsonResponse
23
     */
24 View Code Duplication
    public function sendResetLinkEmail(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
25
    {
26
        $this->validate($request, ['email' => 'required|email']);
27
28
        $response = Password::broker()->sendResetLink(
29
            $request->only('email')
30
        );
31
32
        if (Password::RESET_LINK_SENT) {
33
            return new JsonResponse(['status' => trans($response) ], 200);
34
        }
35
36
        return new JsonResponse(['email' => trans($response) ], 422);
37
38
    }
39
40
    /**
41
     * Send a reset link to the given users.
42
     *
43
     * @param Request $request
44
     * @return JsonResponse
45
     */
46 View Code Duplication
    public function massiveSendResetLinkEmail(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
47
    {
48
        $this->validate($request, ['ids' => 'required']);
49
50
        $errors = [];
51
        foreach ($request->input('ids') as $id) {
0 ignored issues
show
Bug introduced by
The expression $request->input('ids') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
            $user = User::find($id);
53
            $response = Password::broker()->sendResetLink([ 'email' => $user->email ]);
54
            if (! Password::RESET_LINK_SENT) {
55
                dd('ERROR!');
56
                $errors[] = $response;
57
            }
58
        }
59
60
        if ( count($errors) > 0 ) return new JsonResponse(['status' => 'Error', 'errors' => $errors ], 422);
61
62
        return new JsonResponse(['status' => 'Done' ], 200);
63
    }
64
}
65