Completed
Push — master ( 30e125...baa9ff )
by Cesar
19s queued 11s
created

getExtraConfigs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the official Paylater module for PrestaShop.
4
 *
5
 * @author    Paga+Tarde <[email protected]>
6
 * @copyright 2019 Paga+Tarde
7
 * @license   proprietary
8
 */
9
10
/**
11
 * Class PaylaterLogModuleFrontController
12
 */
13
class PaylaterConfigModuleFrontController 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 false|string
38
     * @throws PrestaShopDatabaseException
39
     */
40
    public function getExtraConfigs()
41
    {
42
        $sql_content = 'select * from ' . _DB_PREFIX_. 'pmt_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...
43
        $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...
44
45
        // Convert a multimple dimension array for SQL insert statements into a simple key/value
46
        $simpleDbConfigs = array();
47
        foreach ($dbConfigs as $config) {
48
            $simpleDbConfigs[$config['config']] = $config['value'];
49
        }
50
51
        return $simpleDbConfigs;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $simpleDbConfigs returns the type array which is incompatible with the documented return type false|string.
Loading history...
52
    }
53
54
    /**
55
     * Update POST params in DB
56
     */
57
    public function postMethod()
58
    {
59
        $errors = array();
60
        $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...
61
        unset($post['fc']);
62
        unset($post['module']);
63
        unset($post['controller']);
64
        unset($post['secret']);
65
        if (count($post)) {
66
            foreach ($post as $config => $value) {
67
                $defaultConfigs = $this->getExtraConfigs();
68
                if (isset($defaultConfigs[$config])) {
69
                    Db::getInstance()->update(
70
                        'pmt_config',
71
                        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

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