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

WorkoutPlanRepository::update()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 19

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 31
loc 31
rs 8.8571
cc 3
eloc 19
nc 3
nop 2
1
<?php
2
3
namespace API\Repositories;
4
5
use API\Repositories\Contracts\WorkoutPlanRepositoryInterface;
6
use Illuminate\Support\Facades\DB;
7
use Validator;
8
9
final class WorkoutPlanRepository extends BaseRepository implements WorkoutPlanRepositoryInterface
10
{
11
    public function workoutPlanByUser($id)
12
    {
13
        $workoutPlanByUser = DB::table('workout_plan AS wkP')
14
            ->join('workout_type AS wkT', 'wkT.id_workout_type', '=', 'wkP.fk_workout_type')
15
            ->join('user AS u', 'wkP.fk_user', '=', 'u.id_user')
16
            ->where('wkP.fk_user', $id)
17
            ->select('u.id_user', 'u.first_name as user_name', 'u.email AS user_email',
18
                'wkT.name AS workout_type', 'wkT.description AS workout_type_description',
19
                'wkP.id_workout_plan AS workout_plan_id',
20
                'wkP.date AS workout_plan_date')
21
            ->get();
22
23
        if (count($workoutPlanByUser) > 0) {
24
            return response()->json(['status' => 'success', 'data' => ['workoutPlanByUser' => $workoutPlanByUser]], 200);
25
        }
26
        return response()->json(['status' => 'error', 'message' => 'no data'], 404);
27
    }
28
29 View Code Duplication
    public function store($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...
30
    {
31
        $data = $request->all();
32
33
        $validator = Validator::make($data, [
34
            'fk_workout_type' => 'required',
35
            'fk_user' => 'required',
36
            'date' => 'required',
37
        ]);
38
39
        if ($validator->fails()) {
40
            return response()->json([
41
                'status' => 'fail',
42
                'data' => [
43
                    'fk_workout_type' => 'required',
44
                    'fk_user' => 'required',
45
                    'date' => 'required',
46
                ]], 400);
47
        }
48
49
        $workoutPlan = $this->workoutPlan->create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on API\WorkoutPlan. 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...
50
51
        if (count($workoutPlan) > 0) {
52
            return response()->json(['status' => 'success'], 201);
53
        }
54
        return response()->json(['status' => 'error'], 500);
55
    }
56
57 View Code Duplication
    public function update($request, $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...
58
    {
59
        $workoutPlan = $this->workoutPlan->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\WorkoutPlan>? 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...
60
61
        if (count($workoutPlan) > 0) {
62
63
            $data = $request->all();
64
65
            $validator = Validator::make($data, [
66
                'fk_workout_type' => 'sometimes|required',
67
                'fk_user' => 'sometimes|required',
68
                'date' => 'sometimes|required',
69
            ]);
70
71
            if ($validator->fails()) {
72
                return response()->json([
73
                    'status' => 'fail',
74
                    'data' => [
75
                        'fk_workout_type' => 'required',
76
                        'fk_user' => 'required',
77
                        'date' => 'required',
78
                    ]], 400);
79
            }
80
81
            $workoutPlan->fill($data)->save();
82
83
            return response()->json(['status' => 'success'], 200);
84
        }
85
86
        return response()->json(['status' => 'error'], 500);
87
    }
88
89 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...
90
    {
91
        $workoutPlan = $this->workoutPlan->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\WorkoutPlan>? 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...
92
93
        if (count($workoutPlan) > 0) {
94
            $workoutPlan->delete();
95
            return response()->json(['status' => 'success', 'data' => null], 200);
96
        }
97
98
        return response()->json(['status' => 'error'], 500);
99
    }
100
}