Completed
Push — master ( fb5ed1...817ef8 )
by Daniel Rodrigues
05:00
created

UserRepository::update()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 26

Duplication

Lines 10
Ratio 24.39 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 41
rs 8.5806
cc 4
eloc 26
nc 4
nop 2
1
<?php
2
3
namespace API\Repositories;
4
5
use API\Repositories\Contracts\UserRepositoryInterface;
6
use Illuminate\Support\Facades\Hash;
7
use Validator;
8
use Illuminate\Validation\Rule;
9
10
final class UserRepository extends BaseRepository implements UserRepositoryInterface
11
{
12 View Code Duplication
    public function show($id)
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...
13
    {
14
        $user = $this->user->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
15
16
        if (count($user) > 0) {
17
            return response()->json(['status' => 'success', 'data' => ['user' => $user]], 200);
18
        }
19
        return response()->json(['status' => 'error', 'message' => 'no data'], 404);
20
    }
21
22
    public function store($request)
23
    {
24
        $data = $request->all();
25
        $data['password'] = Hash::make($data['password']);
26
27
        $validator = Validator::make($data, [
28
            'first_name' => 'required',
29
            'last_name' => 'required',
30
            'email' => 'required|unique:user',
31
            'password' => 'required',
32
        ]);
33
34 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
35
            return response()->json([
36
                'status' => 'fail',
37
                'data' => [
38
                    'first_name' => 'required',
39
                    'last_name' => 'required',
40
                    'email' => 'required|unique',
41
                    'password' => 'required',
42
                ]], 400);
43
        }
44
45
        $create = $this->user->create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on API\User. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
47
        if (count($create) > 0) {
48
            return response()->json(['status' => 'success'], 201);
49
        }
50
        return response()->json(['status' => 'error'], 500);
51
    }
52
53
    public function update($request, $id)
54
    {
55
        $user = $this->user->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
57
        if (count($user) > 0) {
58
59
            $data = $request->all();
60
61
            $validator = Validator::make($data, [
62
                'first_name' => 'sometimes|required',
63
                'last_name' => 'sometimes|required',
64
                'email' => [
65
                    'sometimes',
66
                    'required',
67
                    Rule::unique('user')->ignore($id, 'id_user'),
68
                ],
69
                'password' => 'sometimes|required',
70
            ]);
71
72 View Code Duplication
            if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
                return response()->json([
74
                    'status' => 'fail',
75
                    'data' => [
76
                        'first_name' => 'required',
77
                        'last_name' => 'required',
78
                        'email' => 'required|unique_key',
79
                        'password' => 'required',
80
                    ]], 400);
81
            }
82
83
            if (isset($data['password'])) {
84
                $data['password'] = Hash::make($data['password']);
85
            }
86
87
            $user->fill($data)->save();
88
89
            return response()->json(['status' => 'success'], 200);
90
        }
91
92
        return response()->json(['status' => 'error', 'message' => 'no data'], 404);
93
    }
94
95 View Code Duplication
    public function delete($id)
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...
96
    {
97
        $user = $this->user->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
98
99
        if (count($user) > 0) {
100
            $user->delete();
101
            return response()->json(['status' => 'success', 'data' => null], 200);
102
        }
103
        return response()->json(['status' => 'error'], 404);
104
    }
105
}