PagantisApiModuleFrontController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 90
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B postProcess() 0 31 7
A jsonResponse() 0 13 1
A authorize() 0 21 4
1
<?php
2
/**
3
 * This file is part of the official Pagantis module for PrestaShop.
4
 *
5
 * @author    Pagantis <[email protected]>
6
 * @copyright 2019 Pagantis
7
 * @license   proprietary
8
 */
9
10
/**
11
 * Class PagantisApiModuleFrontController
12
 */
13
class PagantisApiModuleFrontController extends ModuleFrontController
0 ignored issues
show
Bug introduced by
The type ModuleFrontController 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...
14
{
15
    /**
16
     * @var string $message
17
     */
18
    protected $message;
19
20
    /**
21
     * @var bool $error
22
     */
23
    protected $error = false;
24
25
    /**
26
     * Controller index method:
27
     */
28
    public function postProcess()
29
    {
30
        if (!$this->authorize()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->authorize() of type null|true is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
31
            return;
32
        };
33
34
        $userId = Tools::getValue('user_id', false);
0 ignored issues
show
Bug introduced by
The type Tools 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...
35
        $from = Tools::getValue('from', false);
36
        $payment = Tools::getValue('payment', false);
37
38
        if (_PS_VERSION_ > '1.6') {
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
            $orders = new PrestaShopCollection('Order');
0 ignored issues
show
Bug introduced by
The type PrestaShopCollection 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...
40
        } else {
41
            $orders = new Collection('Order');
0 ignored issues
show
Bug introduced by
The type Collection 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...
42
        }
43
44
        if ($userId) {
45
            $orders = $orders->where('id_customer', '=', $userId);
46
        }
47
        if ($payment) {
48
            $orders->where('payment', '=', $payment);
49
        }
50
        if ($from) {
51
            $orders->where('date_add', '>', $from);
52
        }
53
54
        foreach ($orders as $order) {
55
            $this->message[] = $order;
56
        }
57
58
        $this->jsonResponse();
59
    }
60
61
    /**
62
     * Send a jsonResponse
63
     */
64
    public function jsonResponse()
65
    {
66
        $result = json_encode(array(
67
            'timestamp' => time(),
68
            'result' => $this->message,
69
        ));
70
71
        header('HTTP/1.1 200 Ok', true, 200);
72
        header('Content-Type: application/json', true);
73
        header('Content-Length: ' . Tools::strlen($result));
74
75
        echo $result;
76
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
77
    }
78
79
    /**
80
     * @return bool|null
81
     */
82
    public function authorize()
83
    {
84
        $productCode = Tools::getValue('product', false);
85
        $products = explode(',', Pagantis::getExtraConfig('PRODUCTS', null));
86
        $privateKey = Configuration::get(Tools::strtolower($productCode) . '_private_key');
0 ignored issues
show
Bug introduced by
The type Configuration 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...
87
        $privateKeyGet = Tools::getValue('secret', false);
88
        if (!empty($privateKeyGet) && $privateKeyGet === $privateKey && in_array(Tools::strtoupper($productCode), $products)) {
89
            return true;
90
        }
91
92
        $result = json_encode(array(
93
            'timestamp' => time(),
94
            'result' => 'Access Forbidden',
95
        ));
96
97
        header('HTTP/1.1 403 Forbidden', true, 403);
98
        header('Content-Type: application/json', true);
99
        header('Content-Length: ' . Tools::strlen($result));
100
101
        echo $result;
102
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
103
    }
104
}
105