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

APILoggedUserEmailController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 38
loc 38
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 18 18 2
A checkCurrentPassword() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}