Passed
Push — master ( 7ddec5...3947e9 )
by Murilo
01:44
created

Web::home()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 22
rs 9.7
cc 1
nc 1
nop 0
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': $sort = 'value'; break;
82
                    case 'rate': $sort = 'rate'; break;
83
                    case 'views': $sort = 'rate'; break;
84
                    case 'sales': $sort = 'id'; break;
85
                    default: $sort = 'id'; break;
86
                }
87
            }
88
        }
89
90
        $head = $this->seo->optimize(
91
            'RedStore | ' . site('name'),
92
            site('desc'),
93
            $this->router->route('web.products'),
94
            routeImage('Products')
95
        )->render();
96
97
        $req = callAPI("/products/{$page}/12/{$sort}/DESC", 'GET', [], $_SESSION['user'] ?? '');
98
        if (isset($req['curl_error']) || $req['code'] != 200) { error_log(json_encode($req)); }
99
100
        $products = (json_decode($req['result']))->message ?? [];
101
        $productsCount = (json_decode($req['result']))->count ?? 0;
102
103
        $lastpage = ceil($productsCount / 12);
104
        if ($lastpage == 0) { $lastpage = 1; }
105
106
        echo $this->view->render('theme/products/products', [
107
            'head' => $head,
108
            'user' => $this->user,
109
            'currentPage' => $page,
110
            'sort' => $sort,
111
            'lastPage' => $lastpage,
112
            'products' => $products,
113
            'productsCount' => $productsCount
114
        ]);
115
    }
116
117
    public function productsDetails($data): void
118
    {
119
        $head = $this->seo->optimize(
120
            'RedStore | ' . site('name'),
121
            site('desc'),
122
            $this->router->route('web.productsDetails'),
123
            routeImage('ProductsDetails')
124
        )->render();
125
126
        $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
127
128
        if (!isset($data['id']) || !filter_var($data['id'], FILTER_VALIDATE_INT)) {
129
            $this->router->redirect('web.home');
130
        }
131
132
        $req = callAPI("/product/{$data['id']}", 'GET', [], $_SESSION['user'] ?? '');
133
        if (isset($req['curl_error']) || $req['code'] != 200) { error_log(json_encode($req)); }
134
135
        $product = (json_decode($req['result']))->message;
136
137
        $req = callAPI("/products/1/4/rate/DESC/product_type_id/{$product->category->id}/{$product->id}", 'GET', [], $_SESSION['user'] ?? '');
138
        if (isset($req['curl_error']) || $req['code'] != 200) { error_log(json_encode($req)); }
139
140
        $relatedProducts = (json_decode($req['result']))->message ?? [];
141
142
        echo $this->view->render('theme/products/products-details', [
143
            'head' => $head,
144
            'user' => $this->user,
145
            'token' => $_SESSION['user'] ?? '',
146
            'product' => $product,
147
            'relatedProducts' => $relatedProducts
148
        ]);
149
    }
150
151
    public function productInsert(): void
152
    {
153
        if (empty($this->user) || !in_array($this->user->access_level_id->name, [ 'Administrador', 'Gerente', 'Vendedor' ])) {
154
            $this->router->redirect('web.products');
155
        }
156
157
        $head = $this->seo->optimize(
158
            'RedStore | ' . site('name'),
159
            site('desc'),
160
            $this->router->route('web.productInsert'),
161
            routeImage('ProductInsert')
162
        )->render();
163
164
        $req = callAPI('/categories', 'GET');
165
166
        if (isset($req['curl_error']) || $req['code'] != 200) {
167
            error_log(json_encode($req));
168
            $this->router->redirect('web.products');
169
        }
170
171
        $categories = (json_decode($req['result']))->message;
172
173
        echo $this->view->render('theme/products/product-insert', [
174
            'head' => $head,
175
            'categories' => $categories,
176
            'mainURL' => BASE_API
177
        ]);
178
    }
179
180
    public function about(): void
181
    {
182
        $head = $this->seo->optimize(
183
            'RedStore | ' . site('name'),
184
            site('desc'),
185
            $this->router->route('web.about'),
186
            routeImage('About')
187
        )->render();
188
189
        echo $this->view->render('theme/main/about', [
190
            'head' => $head
191
        ]);
192
    }
193
194
    public function cart(): void
195
    {
196
        $head = $this->seo->optimize(
197
            'RedStore | ' . site('name'),
198
            site('desc'),
199
            $this->router->route('web.cart'),
200
            routeImage('Cart')
201
        )->render();
202
203
        echo $this->view->render('theme/main/cart', [
204
            'head' => $head
205
        ]);
206
    }
207
208
    public function error($data): void
209
    {
210
        $error = filter_var($data['errcode'], FILTER_VALIDATE_INT);
211
212
        $head = $this->seo->optimize(
213
            "Ooooppss {$error} |" . site('name'),
214
            site('desc'),
215
            $this->router->route('web.error', [ 'errcode' => $error ]),
216
            routeImage($error)
217
        )->render();
218
219
        echo $this->view->render('theme/main/error', [
220
            'head' => $head,
221
            'error' => $error
222
        ]);
223
    }
224
}
225