ProductFunctions   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 29
eloc 72
c 2
b 0
f 0
dl 0
loc 144
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validateOrderAndDirection() 0 29 5
A allProductsWithoutFilters() 0 12 3
A validateProductId() 0 23 4
A validateSearchFilters() 0 13 3
A validateSingleProductId() 0 15 3
A validatePagination() 0 8 3
B validateImageUpload() 0 29 8
1
<?php
2
3
namespace Source\Controllers;
4
5
use stdClass;
6
use Source\Core\Request;
7
use Source\Core\Response;
8
use Source\Models\Product;
9
use Source\Models\ProductImage;
10
use Source\Models\Category;
11
12
trait ProductFunctions
13
{
14
    private function validateSingleProductId($data)
15
    {
16
        if (empty($data['id'])) {
17
            $this->Message->message = 'ID do produto inválido';
0 ignored issues
show
Bug introduced by
The property Message does not exist on Source\Controllers\ProductFunctions. Did you mean message?
Loading history...
18
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
19
        }
20
21
        $id = filter_var($data['id'], FILTER_VALIDATE_INT);
22
23
        if (!$id) {
24
            $this->Message->message = 'parâmetro inválido';
25
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
26
        }
27
28
        return $id;
29
    }
30
31
    private function validateProductId($data)
32
    {
33
        if (!isset($data['id'])) {
34
            $this->Message->message = 'Id do produto inválido!';
0 ignored issues
show
Bug introduced by
The property Message does not exist on Source\Controllers\ProductFunctions. Did you mean message?
Loading history...
35
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
36
        }
37
38
        $id = filter_var($data["id"], FILTER_VALIDATE_INT);
39
40
        if (!$id) {
41
            $this->Message->message = 'Id do produto inválido!';
42
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
43
        }
44
45
        $Product = new Product();
46
        $Product = $Product->findById($id, 'id');
47
48
        if (is_null($Product)) {
49
            $this->Message->message = 'Produto não encontrado!';
50
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
51
        }
52
53
        return $Product;
54
    }
55
56
    private function allProductsWithoutFilters($data)
57
    {
58
        if (!empty($data)) {
59
            return;
60
        }
61
62
        if (is_null(($products = $Product->findAll()))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $Product seems to be never defined.
Loading history...
63
            (new Response())->setStatusCode(HTTP_NO_CONTENT)->send($this->Message);
0 ignored issues
show
Bug introduced by
The property Message does not exist on Source\Controllers\ProductFunctions. Did you mean message?
Loading history...
64
        }
65
66
        $this->Message->message = $products;
67
        (new Response())->setStatusCode(HTTP_OK)->send($this->Message);
68
    }
69
70
    private function validatePagination($data)
71
    {
72
        if (!isset($data['page']) || !isset($data['limit'])) {
73
            $this->Message->message = [
0 ignored issues
show
Bug introduced by
The property Message does not exist on Source\Controllers\ProductFunctions. Did you mean message?
Loading history...
74
                'message' => 'Missing page or limit argument'
75
            ];
76
77
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
78
        }
79
    }
80
81
    private function validateOrderAndDirection($data)
82
    {
83
        if (!isset($data['order']) || !isset($data['direction'])) {
84
            return;
85
        }
86
87
        $page = filter_var($data['page'], FILTER_VALIDATE_INT);
88
        $limit = filter_var($data['limit'], FILTER_VALIDATE_INT);
89
90
        $find = $this->validateSearchFilters($data);
91
92
        $find = $find->order($data['order'] . ' ' . $data['direction']);
93
        $find = $find->limit($limit);
94
        $find = $find->offset(($page * $limit) - $limit);
95
        $products = $find->fetch(true);
96
97
        if (empty($products)) {
98
            (new Response())->setStatusCode(HTTP_NO_CONTENT)->send($this->Message);
0 ignored issues
show
Bug introduced by
The property Message does not exist on Source\Controllers\ProductFunctions. Did you mean message?
Loading history...
99
        }
100
101
        foreach ($products as $key => $value) {
102
            $products[$key] = $value->data();
103
104
            $products[$key]->category = (new Category())->findById((int) $products[$key]->product_type_id)->data();
105
            $products[$key]->ProductImage = (new ProductImage())->findAllByProductId((int) $products[$key]->id);
106
        }
107
108
        $this->Message->message = $products;
109
        (new Response())->setStatusCode(HTTP_OK)->send($this->Message);
110
    }
111
112
    private function validateSearchFilters($data)
113
    {
114
        $Product = new Product();
115
116
        if (empty($data['filterColumn'])) {
117
            return $Product->find();
118
        }
119
120
        if (empty($data['selfId'])) {
121
            return $Product->find("{$data['filterColumn']} = {$data['filterValue']}");
122
        }
123
124
        return $Product->find("{$data['filterColumn']} = {$data['filterValue']} AND id != {$data['selfId']}");
125
    }
126
127
    private function validateImageUpload($data)
128
    {
129
        if (empty($data['image'])) {
130
            $this->Message->message = 'imagem nao encontrada';
0 ignored issues
show
Bug introduced by
The property Message does not exist on Source\Controllers\ProductFunctions. Did you mean message?
Loading history...
131
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
132
        }
133
134
        $base64Image = $data['image'];
135
136
        try {
137
            $base64 = getimagesizefromstring(base64_decode(explode(',', $base64Image)[1]));
138
        } catch (\Exception $e) {
139
            $this->Message->message = 'imagem invalida';
140
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
141
        }
142
143
        if (!$base64) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $base64 of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
            $this->Message->message = 'imagem invalida';
145
            (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
146
        }
147
148
        if (!empty($base64[0]) && !empty($base64[0]) && !empty($base64['mime'])) {
149
            if (!in_array($base64['mime'], $this->validExtensions)) {
150
                $this->Message->message = 'tipo de imagem invalida';
151
                (new Response())->setStatusCode(HTTP_BAD_REQUEST)->send($this->Message);
152
            }
153
        }
154
155
        return $base64Image;
156
    }
157
}
158