Web::error()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.9332
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Source\Controllers;
4
5
use Source\Models\User;
6
7
class Web extends Controller
0 ignored issues
show
Bug introduced by
The type Source\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
{
9
    /** @var mixed */
10
    private $user;
11
12
    public function __construct($router)
13
    {
14
        parent::__construct($router);
15
16
        if (!isset($_SESSION['user'])) {
17
            return;
18
        }
19
20
        if (!empty($_SESSION['user'])) {
21
            $req = callAPI('/me/profile', 'POST', null, $_SESSION['user']);
22
23
            if (isset($req['curl_error'])) {
24
                return;
25
            }
26
27
            if ($req['code'] != 200) {
28
                if ((json_decode($req['result']))->message == 'please login first') {
29
                    $_SESSION['user'] = '';
30
                    return;
31
                }
32
33
                return;
34
            }
35
36
            $this->user = (json_decode($req['result']))->message;
37
        }
38
    }
39
40
    public function home(): void
41
    {
42
        $head = $this->seo->optimize(
43
            'RedStore | ' . site('name'),
44
            site('desc'),
45
            $this->router->route('web.home'),
46
            routeImage('Home')
47
        )->render();
48
49
        $req = callAPI('/products/1/4/rate/DESC', 'GET', [], $_SESSION['user'] ?? '');
50
        $req2 = callAPI('/products/1/4/id/DESC', 'GET', [], $_SESSION['user'] ?? '');
51
        $req3 = callAPI('/products/2/4/id/DESC', 'GET', [], $_SESSION['user'] ?? '');
52
53
        $featuredProducts = (json_decode($req['result']))->message ?? [];
54
        $latestProducts = (json_decode($req2['result']))->message ?? [];
55
        $latestProducts2 = (json_decode($req3['result']))->message ?? [];
56
57
        echo $this->view->render('theme/main/index', [
58
            'head' => $head,
59
            'featuredProducts' => $featuredProducts,
60
            'latestProducts' => $latestProducts,
61
            'latestProducts2' => $latestProducts2
62
        ]);
63
    }
64
65
    public function products($data): void
66
    {
67
        $page = 1;
68
        $sort = 'id';
69
70
        if (!empty($data)) {
71
            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
72
73
            if (isset($data['page'])) {
74
                if (filter_var($data['page'], FILTER_VALIDATE_INT)) {
75
                    $page = $data['page'];
76
                }
77
            }
78
79
            if (isset($data['sort'])) {
80
                switch ($data['sort']) {
81
                    case 'value':
82
                        $sort = 'value';
83
                        break;
84
                    case 'rate':
85
                        $sort = 'rate';
86
                        break;
87
                    case 'views':
88
                        $sort = 'rate';
89
                        break;
90
                    case 'sales':
91
                        $sort = 'id';
92
                        break;
93
                    default:
94
                        $sort = 'id';
95
                        break;
96
                }
97
            }
98
        }
99
100
        $head = $this->seo->optimize(
101
            'RedStore | ' . site('name'),
102
            site('desc'),
103
            $this->router->route('web.products'),
104
            routeImage('Products')
105
        )->render();
106
107
        $req = callAPI("/products/{$page}/12/{$sort}/DESC", 'GET', [], $_SESSION['user'] ?? '');
108
        if (isset($req['curl_error']) || $req['code'] != 200) {
109
            error_log(json_encode($req));
110
        }
111
112
        $products = (json_decode($req['result']))->message ?? [];
113
        $productsCount = (json_decode($req['result']))->count ?? 0;
114
115
        $lastpage = ceil($productsCount / 12);
116
        if ($lastpage == 0) {
117
            $lastpage = 1;
118
        }
119
120
        echo $this->view->render('theme/products/products', [
121
            'head' => $head,
122
            'user' => $this->user,
123
            'currentPage' => $page,
124
            'sort' => $sort,
125
            'lastPage' => $lastpage,
126
            'products' => $products,
127
            'productsCount' => $productsCount
128
        ]);
129
    }
130
131
    public function productsDetails($data): void
132
    {
133
        $head = $this->seo->optimize(
134
            'RedStore | ' . site('name'),
135
            site('desc'),
136
            $this->router->route('web.productsDetails'),
137
            routeImage('ProductsDetails')
138
        )->render();
139
140
        $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
141
142
        if (!isset($data['id']) || !filter_var($data['id'], FILTER_VALIDATE_INT)) {
143
            $this->router->redirect('web.home');
144
        }
145
146
        $req = callAPI("/product/{$data['id']}", 'GET', [], $_SESSION['user'] ?? '');
147
        if (isset($req['curl_error']) || $req['code'] != 200) {
148
            error_log(json_encode($req));
149
        }
150
151
        $product = (json_decode($req['result']))->message;
152
153
        $req = callAPI("/products/1/4/rate/DESC/product_type_id/{$product->category->id}/{$product->id}", 'GET', [], $_SESSION['user'] ?? '');
154
        if (isset($req['curl_error']) || $req['code'] != 200) {
155
            error_log(json_encode($req));
156
        }
157
158
        $relatedProducts = (json_decode($req['result']))->message ?? [];
159
160
        echo $this->view->render('theme/products/products-details', [
161
            'head' => $head,
162
            'user' => $this->user,
163
            'token' => $_SESSION['user'] ?? '',
164
            'product' => $product,
165
            'relatedProducts' => $relatedProducts
166
        ]);
167
    }
168
169
    public function productInsert(): void
170
    {
171
        if (empty($this->user) || !in_array($this->user->access_level_id->name, ['Administrador', 'Gerente', 'Vendedor'])) {
172
            $this->router->redirect('web.products');
173
        }
174
175
        $head = $this->seo->optimize(
176
            'RedStore | ' . site('name'),
177
            site('desc'),
178
            $this->router->route('web.productInsert'),
179
            routeImage('ProductInsert')
180
        )->render();
181
182
        $req = callAPI('/categories', 'GET');
183
184
        if (isset($req['curl_error']) || $req['code'] != 200) {
185
            error_log(json_encode($req));
186
            $this->router->redirect('web.products');
187
        }
188
189
        $categories = (json_decode($req['result']))->message;
190
191
        echo $this->view->render('theme/products/product-insert', [
192
            'head' => $head,
193
            'categories' => $categories,
194
            'mainURL' => BASE_API
195
        ]);
196
    }
197
198
    public function about(): void
199
    {
200
        $head = $this->seo->optimize(
201
            'RedStore | ' . site('name'),
202
            site('desc'),
203
            $this->router->route('web.about'),
204
            routeImage('About')
205
        )->render();
206
207
        echo $this->view->render('theme/main/about', [
208
            'head' => $head
209
        ]);
210
    }
211
212
    public function cart(): void
213
    {
214
        $head = $this->seo->optimize(
215
            'RedStore | ' . site('name'),
216
            site('desc'),
217
            $this->router->route('web.cart'),
218
            routeImage('Cart')
219
        )->render();
220
221
        $products = [
222
            // [
223
            //     'id' => 1,
224
            //     'quantity' => 1
225
            // ],
226
            // [
227
            //     'id' => 2,
228
            //     'quantity' => 1
229
            // ],
230
            // [
231
            //     'id' => 3,
232
            //     'quantity' => 1
233
            // ]
234
        ];
235
236
        // $rawProducts = $_COOKIE['rawproducts'];
237
        $rawProducts = [['id' => 1],[ 'id' => 2]];
238
239
        foreach ($rawProducts as $key => $value) {
240
            $req = callAPI("/product/{$value['id']}", 'GET', [], $_SESSION['user'] ?? '');
241
            if (isset($req['curl_error']) || $req['code'] != 200) {
242
                error_log(json_encode($req));
243
            }
244
    
245
            $products[] = (json_decode($req['result']))->message ?? [];
246
        }
247
248
        echo $this->view->render('theme/main/cart', [
249
            'head' => $head,
250
            'products' => $products
251
        ]);
252
    }
253
254
    public function error($data): void
255
    {
256
        $error = filter_var($data['errcode'], FILTER_VALIDATE_INT);
257
258
        $head = $this->seo->optimize(
259
            "Ooooppss {$error} |" . site('name'),
260
            site('desc'),
261
            $this->router->route('web.error', ['errcode' => $error]),
262
            routeImage($error)
263
        )->render();
264
265
        echo $this->view->render('theme/main/error', [
266
            'head' => $head,
267
            'error' => $error
268
        ]);
269
    }
270
}
271