Passed
Pull Request — master (#28)
by Raúl
01:58
created

PaylaterConfigModuleFrontController::initContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 15
rs 9.9
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 = strtolower($_SERVER['REQUEST_METHOD']) . "Method";
22
        if (method_exists($this, $method)) {
23
            header('HTTP/1.1 200 Ok', true, 200);
24
            header('Content-Type: application/json', true);
25
            header('Content-Length: ' . Tools::strlen($result));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
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...
26
            echo json_encode($this->{$method}());
27
            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...
28
        }
29
        header('HTTP/1.1 405 Method not allowed', true, 405);
30
        header('Content-Type: application/json', true);
31
32
        exit();
33
    }
34
35
    /**
36
     * Update POST params in DB
37
     */
38
    public function postMethod()
39
    {
40
        $errors = array();
41
        if (count($_POST)) {
42
            foreach ($_POST as $config => $value) {
43
                $defaultConfigs = json_decode(getenv('PMT_DEFAULT_CONFIGS'), true);
44
                if (isset($defaultConfigs[$config])) {
45
                    Db::getInstance()->update(
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...
46
                        'pmt_config',
47
                        array('value' => $value),
48
                        'config = \''. $config .'\''
49
                    );
50
                } else {
51
                    $errors[$config] = $value;
52
                }
53
            }
54
        } else {
55
            $errors['NO_POST_DATA'] = 'No post data provided';
56
        }
57
58
        $dbConfigs = $this->getMethod();
59
        if (count($errors) > 0) {
60
            $dbConfigs['__ERRORS__'] = $errors;
61
        }
62
        return $dbConfigs;
63
    }
64
65
    /**
66
     * Read PTM configs
67
     *
68
     * @throws PrestaShopDatabaseException
69
     */
70
    public function getMethod()
71
    {
72
        $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...
73
        $dbConfigs = Db::getInstance()->executeS($sql_content);
74
75
        $simpleDbConfigs = array();
76
        foreach ($dbConfigs as $config) {
77
            $simpleDbConfigs[$config['config']] = $config['value'];
78
        }
79
        return $simpleDbConfigs;
80
    }
81
82
    /**
83
     * @return bool|null
84
     */
85
    public function authorize()
86
    {
87
        $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...
88
89
        if (Tools::getValue('secret', false) == $privateKey) {
90
            return true;
91
        }
92
93
        header('HTTP/1.1 403 Forbidden', true, 403);
94
        header('Content-Type: application/json', true);
95
96
        exit();
97
    }
98
}
99