Completed
Push — master ( e14874...af74be )
by Sergi Tur
09:41
created

checkCurrentPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 4
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Http\Requests\LoggedUserPasswordUpdate;
6
use Auth;
7
use Hash;
8
use Illuminate\Http\JsonResponse;
9
10
/**
11
* Class APILoggedUserPasswordController
12
*
13
* @package Acacha\Users\Http\Controllers
14
*/
15 View Code Duplication
class APILoggedUserPasswordController extends Controller {
0 ignored issues
show
Duplication introduced by
This class 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...
16
17
    /**
18
     * Update logged user password.
19
     *
20
     * @param LoggedUserPasswordUpdate $request
21
     * @return JsonResponse|\App\User|null
22
     */
23
    public function update(LoggedUserPasswordUpdate $request)
24
    {
25
        $user = Auth::user();
26
        if (! $this->checkCurrentPassword($request, $user)) {
27
            return new JsonResponse(
28
                [
29
                  'message'=> 'The given data was invalid.',
30
                  'errors'=> [
31
                    'current_password' => [ 'The password is not valid']
32
                  ]
33
                ],
34
                422
35
            );
36
        }
37
        $user->update(['password' => bcrypt($request->password)]);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<Acacha\Users\Http...ggedUserPasswordUpdate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
38
39
        return $user;
40
    }
41
42
    /**
43
     * Check current password
44
     *
45
     * @param $user
46
     * @return bool
47
     */
48
    protected function checkCurrentPassword(LoggedUserPasswordUpdate $request, $user)
49
    {
50
        return Hash::check($request->current_password, $user->password);
0 ignored issues
show
Documentation introduced by
The property current_password does not exist on object<Acacha\Users\Http...ggedUserPasswordUpdate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
    }
52
}