Completed
Push — master ( 716e26...a46f5a )
by Daniel Rodrigues
02:30
created

WorkoutPlanRepository::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 23
loc 23
rs 9.0856
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace API\Repositories;
4
5
use API\Repositories\Contracts\WorkoutPlanRepositoryInterface;
6
use Illuminate\Support\Facades\Validator;
7
8
final class WorkoutPlanRepository extends BaseRepository implements WorkoutPlanRepositoryInterface
9
{
10 View Code Duplication
    public function workoutPlanByUser($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...
11
    {
12
        $workoutPlansByUser = $this->workoutPlan
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<API\Models\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...
13
            ->where('user_id', $id)
14
            ->with('workoutType')
15
            ->get();
16
17
        if ($workoutPlansByUser) {
18
            return response()->json(['status' => 'success', 'data' => ['workoutPlanByUser' => $workoutPlansByUser]], 200);
19
        }
20
        return response()->json(['status' => 'error', 'message' => 'no data'], 404);
21
    }
22
23 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...
24
    {
25
        $data = $request->all();
26
27
        $validator = Validator::make($data, [
28
            'user_id' => 'required',
29
        ]);
30
31
        if ($validator->fails()) {
32
            return response()->json([
33
                'status' => 'fail',
34
                'data' => [
35
                    'user_id' => 'required',
36
                ]], 422);
37
        }
38
39
        $createWorkoutPlan = $this->workoutPlan->create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on API\Models\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...
40
41
        if ($createWorkoutPlan) {
42
            return response()->json(['status' => 'success'], 201);
43
        }
44
        return response()->json(['status' => 'error'], 500);
45
    }
46
47 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...
48
    {
49
        $workoutPlan = $this->workoutPlan->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\Models\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...
50
51
        if ($workoutPlan) {
52
53
            $data = $request->all();
54
55
            $validator = Validator::make($data, [
56
                'user_id' => 'sometimes|required',
57
            ]);
58
59
            if ($validator->fails()) {
60
                return response()->json([
61
                    'status' => 'fail',
62
                    'data' => [
63
                        'user_id' => 'required',
64
                    ]], 422);
65
            }
66
67
            $workoutPlan->fill($data)->save();
68
69
            return response()->json(['status' => 'success'], 200);
70
        }
71
72
        return response()->json(['status' => 'error'], 500);
73
    }
74
75 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...
76
    {
77
        $workoutPlan = $this->workoutPlan->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<API\Models\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...
78
79
        if ($workoutPlan) {
80
            $workoutPlan->delete();
81
            return response()->json(['status' => 'success', 'data' => null], 200);
82
        }
83
84
        return response()->json(['status' => 'error'], 500);
85
    }
86
}