Total Complexity | 45 |
Total Lines | 267 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Products often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Products, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Products extends GoomerApi |
||
15 | { |
||
16 | /** |
||
17 | * Products constructor. |
||
18 | * @throws \Exception |
||
19 | */ |
||
20 | public function __construct() |
||
21 | { |
||
22 | parent::__construct(); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * list all products |
||
27 | * @param array $data |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | public function index(array $data): void |
||
31 | { |
||
32 | if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT) ) { |
||
33 | $this->call( |
||
34 | 400, |
||
35 | "invalid_data", |
||
36 | "É preciso informar o ID do restaurante para verificar os produtos" |
||
37 | )->back(); |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | //get products |
||
42 | $products = (new Product())->find("restaurant_id=:restaurant_id","restaurant_id={$restaurant_id}"); |
||
43 | |||
44 | if (!$products->count()) { |
||
45 | $this->call( |
||
46 | 404, |
||
47 | "not_found", |
||
48 | "Nada encontrado para sua busca." |
||
49 | )->back(["results" => 0]); |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $page = (!empty($data["page"]) ? $data["page"] : 1); |
||
54 | $pager = new Pager(url("/{restaurant_id}/products")); |
||
55 | $pager->pager($products->count(), 10, $page); |
||
56 | |||
57 | $response["results"] = $products->count(); |
||
|
|||
58 | $response["page"] = $pager->page(); |
||
59 | $response["pages"] = $pager->pages(); |
||
60 | |||
61 | foreach ($products->limit($pager->limit())->offset($pager->offset())->order("name ASC")->fetch(true) as $product) { |
||
62 | $response["products"][] = $product->restaurant()->category()->data(); |
||
63 | } |
||
64 | |||
65 | $this->back($response); |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param array $data |
||
71 | * @throws \Exception |
||
72 | */ |
||
73 | public function create(array $data): void |
||
74 | { |
||
75 | $request = $this->requestLimit("productsCreate", 5, 60); |
||
76 | if (!$request) { |
||
77 | return; |
||
78 | } |
||
79 | |||
80 | if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT) ) { |
||
81 | $this->call( |
||
82 | 400, |
||
83 | "invalid_data", |
||
84 | "É preciso informar o ID do restaurante para criar os produtos" |
||
85 | )->back(); |
||
86 | return; |
||
87 | } |
||
88 | |||
89 | $restaurant = (new Restaurant())->findById($restaurant_id); |
||
90 | |||
91 | if (!$restaurant) { |
||
92 | $this->call( |
||
93 | 404, |
||
94 | "not_found", |
||
95 | "Você tentou cadastrar um produto em um restaurante que não existe" |
||
96 | )->back(); |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | if ( empty($data["category_id"]) || !$category_id = filter_var($data["category_id"], FILTER_VALIDATE_INT) || empty($data["name"]) || empty($data["price"]) ) { |
||
101 | $this->call( |
||
102 | 400, |
||
103 | "empty_data", |
||
104 | "Para criar informe o ID da categoria do produto, o nome do produto e o preço do produto" |
||
105 | )->back(); |
||
106 | return ; |
||
107 | } |
||
108 | |||
109 | $category = (new ProductCategory())->findById($category_id); |
||
110 | |||
111 | if (!$category) { |
||
112 | $this->call( |
||
113 | 404, |
||
114 | "not_found", |
||
115 | "Você tentou cadastrar um produto em uma categoria que não existe" |
||
116 | )->back(); |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | $product = new Product(); |
||
121 | $product->restaurant_id = $restaurant_id; |
||
122 | $product->category_id = $category_id; |
||
123 | $product->name = filter_var($data["name"], FILTER_SANITIZE_STRIPPED); |
||
124 | $product->price = filter_var($data["price"], FILTER_SANITIZE_STRIPPED); |
||
125 | $product->image = (!empty($data["image"])) ? filter_var($data["image"], FILTER_SANITIZE_STRIPPED) : null; |
||
126 | $product->description = (!empty($data["description"])) ? filter_var($data["description"], FILTER_SANITIZE_STRIPPED) : null; |
||
127 | $product->old_price = (!empty($data["old_price"])) ? filter_var($data["old_price"], FILTER_SANITIZE_STRIPPED) : 0.00; |
||
128 | $product->save(); |
||
129 | |||
130 | if($product->fail()){ |
||
131 | $this->call( |
||
132 | 400, |
||
133 | "empty_data", |
||
134 | $product->fail()->getMessage() |
||
135 | )->back(); |
||
136 | return; |
||
137 | } |
||
138 | |||
139 | $this->back(["product" => $product->restaurant()->category()->data()]); |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param array $data |
||
145 | */ |
||
146 | public function read(array $data): void |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param array $data |
||
186 | */ |
||
187 | public function update(array $data): void |
||
188 | { |
||
189 | if ( empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT)) { |
||
190 | $this->call( |
||
191 | 400, |
||
192 | "invalid_data", |
||
193 | "É preciso informar o ID do restaurante para atualizar o produto" |
||
194 | )->back(); |
||
195 | return; |
||
196 | } |
||
197 | |||
198 | if (empty($data["product_id"]) || !$product_id = filter_var($data["product_id"], FILTER_VALIDATE_INT) ) { |
||
199 | $this->call( |
||
200 | 400, |
||
201 | "invalid_data", |
||
202 | "É preciso informar o ID do produto que deseja atualizar" |
||
203 | )->back(); |
||
204 | return; |
||
205 | } |
||
206 | |||
207 | $product = (new Product())->find("restaurant_id = :restaurant_id AND id = :id", |
||
208 | "restaurant_id={$restaurant_id}&id=$product_id")->fetch(); |
||
209 | |||
210 | if (!$product) { |
||
211 | $this->call( |
||
212 | 404, |
||
213 | "not_found", |
||
214 | "Você tentou atualizar um produto de um restaurante que não existe" |
||
215 | )->back(); |
||
216 | return; |
||
217 | } |
||
218 | |||
219 | $product->name = (!empty($data["name"])) ? filter_var($data["name"], FILTER_SANITIZE_STRIPPED) : $product->name; |
||
220 | $product->price = (!empty($data["price"])) ? filter_var($data["price"], FILTER_SANITIZE_STRIPPED) : $product->price; |
||
221 | $product->image = (!empty($data["image"])) ? filter_var($data["image"], FILTER_SANITIZE_STRIPPED) : $product->image; |
||
222 | $product->description = (!empty($data["description"])) ? filter_var($data["description"], FILTER_SANITIZE_STRIPPED) : $product->description; |
||
223 | $product->old_price = (!empty($data["old_price"])) ? filter_var($data["old_price"], FILTER_SANITIZE_STRIPPED) : $product->old_price; |
||
224 | $product->save(); |
||
225 | |||
226 | if($product->fail()){ |
||
227 | $this->call( |
||
228 | 400, |
||
229 | "empty_data", |
||
230 | $product->fail()->getMessage() |
||
231 | )->back(); |
||
232 | return; |
||
233 | } |
||
234 | |||
235 | $this->back(["product" => $product->restaurant()->category()->data()]); |
||
236 | return; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param array $data |
||
241 | */ |
||
242 | public function delete(array $data): void |
||
281 | } |
||
282 | } |