Passed
Pull Request — master (#34)
by Raúl
02:07
created

PaylaterConfigModuleFrontController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 53
c 2
b 0
f 1
dl 0
loc 108
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A PagantisConfigModuleFrontController::initContent() 0 16 2
A PagantisConfigModuleFrontController::getMethod() 0 10 2
B PagantisConfigModuleFrontController::postMethod() 0 30 6
A PagantisConfigModuleFrontController::authorize() 0 12 2
A PagantisConfigModuleFrontController::getExtraConfigs() 0 11 2
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 PagantisLogModuleFrontController
12
 */
13
class PagantisConfigModuleFrontController 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
     * Initial method
17
     */
18
    public function initContent()
19
    {
20
        $this->authorize();
21
        $method = Tools::strtolower($_SERVER['REQUEST_METHOD']) . "Method";
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...
22
        if (method_exists($this, $method)) {
23
            header('HTTP/1.1 200 Ok', true, 200);
24
            header('Content-Type: application/json', true);
25
            $result = json_encode($this->{$method}());
26
            header('Content-Length: ' . Tools::strlen($result));
27
            echo $result;
28
            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...
29
        }
30
        header('HTTP/1.1 405 Method not allowed', true, 405);
31
        header('Content-Type: application/json', true);
32
33
        exit();
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getExtraConfigs()
40
    {
41
        $sql_content = 'select * from ' . _DB_PREFIX_. 'pagantis_config';
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
        $dbConfigs = Db::getInstance()->executeS($sql_content);
0 ignored issues
show
Bug introduced by
The type Db 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...
43
44
        $simpleDbConfigs = array();
45
        foreach ($dbConfigs as $config) {
46
            $simpleDbConfigs[$config['config']] = $config['value'];
47
        }
48
49
        return $simpleDbConfigs;
50
    }
51
52
    /**
53
     * Update POST params in DB
54
     */
55
    public function postMethod()
56
    {
57
        $errors = array();
58
        $post = (_PS_VERSION_ < 1.6) ? $_POST + $_GET : Tools::getAllValues();
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...
59
        unset($post['fc']);
60
        unset($post['module']);
61
        unset($post['controller']);
62
        unset($post['secret']);
63
        if (count($post)) {
64
            foreach ($post as $config => $value) {
65
                $defaultConfigs = $this->getExtraConfigs();
66
                if (isset($defaultConfigs[$config])) {
67
                    Db::getInstance()->update(
68
                        'pagantis_config',
69
                        array('value' => pSQL($value)),
0 ignored issues
show
Bug introduced by
The function pSQL was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
                        array('value' => /** @scrutinizer ignore-call */ pSQL($value)),
Loading history...
70
                        'config = \''. pSQL($config) .'\''
71
                    );
72
                } else {
73
                    $errors[$config] = $value;
74
                }
75
            }
76
        } else {
77
            $errors['NO_POST_DATA'] = 'No post data provided';
78
        }
79
80
        $dbConfigs = $this->getMethod();
81
        if (count($errors) > 0) {
82
            $dbConfigs['__ERRORS__'] = $errors;
83
        }
84
        return $dbConfigs;
85
    }
86
87
    /**
88
     * Read PTM configs
89
     *
90
     * @throws PrestaShopDatabaseException
91
     */
92
    public function getMethod()
93
    {
94
        $sql_content = 'select * from ' . _DB_PREFIX_. 'pagantis_config';
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
95
        $dbConfigs = Db::getInstance()->executeS($sql_content);
96
97
        $simpleDbConfigs = array();
98
        foreach ($dbConfigs as $config) {
99
            $simpleDbConfigs[$config['config']] = $config['value'];
100
        }
101
        return $simpleDbConfigs;
102
    }
103
104
    /**
105
     * @return bool|null
106
     */
107
    public function authorize()
108
    {
109
        $privateKey = Configuration::get('pagantis_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...
110
111
        if (Tools::getValue('secret', false) == $privateKey) {
112
            return true;
113
        }
114
115
        header('HTTP/1.1 403 Forbidden', true, 403);
116
        header('Content-Type: application/json', true);
117
118
        exit();
119
    }
120
}
121