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

PagantisConfigModuleFrontController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 10 2
A initContent() 0 16 2
A postMethod() 0 30 5
A authorize() 0 12 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
     * Update POST params in DB
38
     */
39
    public function postMethod()
40
    {
41
        $errors = array();
42
        $post = Tools::getAllValues();
43
        unset($post['fc']);
44
        unset($post['module']);
45
        unset($post['controller']);
46
        unset($post['secret']);
47
        if (count($post)) {
48
            foreach ($post as $config => $value) {
49
                $defaultConfigs = json_decode(getenv('PAGANTIS_DEFAULT_CONFIGS'), true);
50
                if (isset($defaultConfigs[$config])) {
51
                    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...
52
                        'pagantis_config',
53
                        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

53
                        array('value' => /** @scrutinizer ignore-call */ pSQL($value)),
Loading history...
54
                        'config = \''. pSQL($config) .'\''
55
                    );
56
                } else {
57
                    $errors[$config] = $value;
58
                }
59
            }
60
        } else {
61
            $errors['NO_POST_DATA'] = 'No post data provided';
62
        }
63
64
        $dbConfigs = $this->getMethod();
65
        if (count($errors) > 0) {
66
            $dbConfigs['__ERRORS__'] = $errors;
67
        }
68
        return $dbConfigs;
69
    }
70
71
    /**
72
     * Read PTM configs
73
     *
74
     * @throws PrestaShopDatabaseException
75
     */
76
    public function getMethod()
77
    {
78
        $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...
79
        $dbConfigs = Db::getInstance()->executeS($sql_content);
80
81
        $simpleDbConfigs = array();
82
        foreach ($dbConfigs as $config) {
83
            $simpleDbConfigs[$config['config']] = $config['value'];
84
        }
85
        return $simpleDbConfigs;
86
    }
87
88
    /**
89
     * @return bool|null
90
     */
91
    public function authorize()
92
    {
93
        $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...
94
95
        if (Tools::getValue('secret', false) == $privateKey) {
96
            return true;
97
        }
98
99
        header('HTTP/1.1 403 Forbidden', true, 403);
100
        header('Content-Type: application/json', true);
101
102
        exit();
103
    }
104
}
105