Completed
Push — master ( 6fa307...fa7e27 )
by Corentin
04:31
created

CategoryController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 5
Ratio 22.73 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 5
loc 22
rs 9.2
cc 3
eloc 14
nc 3
nop 1
1
<?php namespace App\Http\Controllers;
2
3
use App\Exceptions\RepositoryException;
4
use App\Models\Categories;
5
use App\Repositories\CategoryRepository;
6
use DB;
7
use Illuminate\Http\Request;
8
use Response;
9
use Validator;
10
11
class CategoryController extends Controller
12
{
13
    protected $categoryRepository;
14
15
    public function __construct(CategoryRepository $categoryRepository)
16
    {
17
        $this->categoryRepository = $categoryRepository;
18
    }
19
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return Response
24
     */
25
    public function index()
26
    {
27
        return response()->json($this->categoryRepository->allAPI());
28
    }
29
30
    /**
31
     * Store a newly created resource in storage.
32
     *
33
     * @return Response
34
     */
35
    public function store(Request $request)
36
    {
37
        $validator = Validator::make($request->all(), [
38
            'title' => 'required|name|between:1,50',
39
        ]);
40
41 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...
42
            return response()
43
                ->json(['code' => 1, 'error' => 'Title or description is invalid'])
44
                ->setStatusCode(400);
45
        }
46
47
        try {
48
            $categoryID = $this->categoryRepository->store($request->all());
49
        } catch (RepositoryException $e) {
50
            return response()
51
                ->json(['code' => 2, 'error' => $e->getMessage()])
52
                ->setStatusCode(400);
53
        }
54
55
        return response()->json(['id' => $categoryID]);
56
    }
57
58
    /**
59
     * Display the specified resource.
60
     *
61
     * @param  int $id
62
     * @return Response
63
     */
64
    public function show($id)
65
    {
66 View Code Duplication
        if (!$this->isID($id)) {
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...
67
            return response()
68
                ->json(['code'=>1, 'error'=>'Parameter must be an integer'])
69
                ->setStatusCode(400);
70
        }
71
72
        $category = Categories::fromGroup()->find($id);
0 ignored issues
show
Bug introduced by
The method fromGroup() does not exist on App\Models\Categories. Did you maybe mean scopeFromGroup()?

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...
73
74
        if ($category == null) {
75
            return response()->json([]);
76
        }
77
78
        return response()->json($category);
79
    }
80
81
    /**
82
     * Update the specified resource in storage.
83
     *
84
     * @param  int $id
85
     * @return Response
86
     */
87
    public function update($id, Request $request)
88
    {
89
        $validator = Validator::make($request->all(), [
90
            'title' => 'required|name|between:1,50'
91
        ]);
92
93 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...
94
            return response()
95
                ->json(['code' => 3, 'error' => 'Validation failed'])
96
                ->setStatusCode(400);
97
        }
98
99 View Code Duplication
        if (!$this->isID($id)) {
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...
100
            return response()
101
                ->json(['code' => 1, 'error' => 'Parameter must be numeric'])
102
                ->setStatusCode(400);
103
        }
104
105
        if ($this->categoryRepository->contains($request->input('title'))) {
106
            return response()
107
                ->json(['code' => 2, 'error' => 'Category name already exists'])
108
                ->setStatusCode(400);
109
        }
110
111
        $category = Categories::fromGroup()->find($id);
0 ignored issues
show
Bug introduced by
The method fromGroup() does not exist on App\Models\Categories. Did you maybe mean scopeFromGroup()?

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...
112
113 View Code Duplication
        if ($category == null) {
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...
114
            return response()
115
                ->json(['code' => 2, 'error' => 'Category not found'])
116
                ->setStatusCode(400);
117
        }
118
119
        $category->category_title = $request->input('title');
120
        $category->description = $request->input('description');
121
122 View Code Duplication
        if (!$category->save()) {
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...
123
            return response()
124
                ->json(['code' => 2, 'error' => 'Database error'])
125
                ->setStatusCode(400);
126
        }
127
128
        return response()->json([]);
129
    }
130
131
132
    /**
133
     * Remove the specified resource from storage.
134
     *
135
     * @param  int $id
136
     * @return Response
137
     */
138
    public function destroy($id)
139
    {
140 View Code Duplication
        if (!$this->isID($id)) {
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...
141
            return response()
142
                ->json(['code' => 1, 'error' => 'Parameter must be numeric'])
143
                ->setStatusCode(400);
144
        }
145
146
        $category = Categories::fromGroup()->find($id);
0 ignored issues
show
Bug introduced by
The method fromGroup() does not exist on App\Models\Categories. Did you maybe mean scopeFromGroup()?

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...
147
148 View Code Duplication
        if ($category == null) {
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...
149
            return response()
150
                ->json(['code' => 2, 'error' => 'Category not found'])
151
                ->setStatusCode(400);
152
        }
153
154
        $products = Categories::find($id)->products()->get();
155
156 View Code Duplication
        if (count($products) != 0) {
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...
157
            return response()
158
                ->json(['code' => 3, 'error' => 'Category not empty'])
159
                ->setStatusCode(400);
160
        }
161
162
        try {
163
            $category->delete();
164
        } catch (\Exception $e) {
165
            return response()
166
                ->json(['code' => 4, 'error' => 'Database error'])
167
                ->setStatusCode(400);
168
        }
169
170
        return response()->json([]);
171
    }
172
173
    private function isID($id)
174
    {
175
        return (bool)preg_match('/^[0-9]{1,10}$/', $id);
176
    }
177
}
178