APILoggedUserEmailController::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Http\Requests\LoggedUserEmailUpdate;
6
use Acacha\Users\Http\Requests\LoggedUserPasswordUpdate;
7
use Auth;
8
use Hash;
9
use Illuminate\Http\JsonResponse;
10
11
/**
12
* Class APILoggedUserEmailController
13
*
14
* @package Acacha\Users\Http\Controllers
15
*/
16 View Code Duplication
class APILoggedUserEmailController 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...
17
18
    /**
19
     * Update logged user password.
20
     *
21
     * @param LoggedUserEmailUpdate $request
22
     * @return JsonResponse|\App\User|null
23
     */
24
    public function update(LoggedUserEmailUpdate $request)
25
    {
26
        $user = Auth::user();
27
        if (! $this->checkCurrentPassword($request, $user)) {
28
            return new JsonResponse(
29
                [
30
                  'message'=> 'The given data was invalid.',
31
                  'errors'=> [
32
                    'current_password' => [ 'The password is not valid']
33
                  ]
34
                ],
35
                422
36
            );
37
        }
38
        $user->update(['email' => $request->email]);
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<Acacha\Users\Http...\LoggedUserEmailUpdate>. 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...
39
40
        return $user;
41
    }
42
43
    /**
44
     * Check current password
45
     *
46
     * @param $user
47
     * @return bool
48
     */
49
    protected function checkCurrentPassword(LoggedUserEmailUpdate $request, $user)
50
    {
51
        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...\LoggedUserEmailUpdate>. 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...
52
    }
53
}