Passed
Pull Request — master (#23)
by Raúl
02:46
created

AbstractController::getHttpStatusCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 17
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 20
rs 9.7
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 AbstractController
12
 */
13
abstract class AbstractController 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
     * PMT_CODE
17
     */
18
    const PMT_CODE = 'paylater';
19
20
    /**
21
     * EXCEPTION RESPONSES
22
     */
23
    const CC_ERR_MSG = 'Unable to block resource';
24
    const CC_NO_MERCHANT_ORDERID = 'Merchant Order Id (cart_id) not found';
25
    const CC_NO_CONFIG = 'Unable to load module configuration';
26
    const CC_MALFORMED = 'Bad request, module may not be enabled';
27
28
    const GMO_ERR_MSG = 'Unable to find merchant Order';
29
    const GMO_CART_NOT_LOADED = 'Unable to load cart';
30
31
    const GPOI_ERR_MSG = 'Pmt Order Not Found';
32
    const GPOI_NO_ORDERID = 'We can not get the PagaMasTarde identification in database.';
33
34
    const GPO_ERR_MSG = 'Unable to get Order';
35
    const GPO_ERR_TYPEOF = 'The requested PMT Order is not a valid PMTOrder object';
36
37
    const COS_ERR_MSG = 'Order status is not authorized';
38
    const COS_WRONG_STATUS = 'Invalid Pmt status';
39
40
    const CMOS_ERR_MSG = 'Merchant Order status is invalid';
41
    const CMOS_WRONG_CURRENT_STATUS = 'The status of the merchant order is not correct';
42
    const CMOS_WRONG_PREVIOUS_STATUS = 'Previous merchant status order is not correct';
43
    const CMOS_PREVIOUSLY_PROCESSED = 'The merchant order has been already processed at least once';
44
45
    const VA_ERR_MSG = 'Amount conciliation error';
46
    const VA_WRONG_AMOUNT = 'Wrong order amount';
47
48
    const PMO_ERR_MSG = 'Unknown Error';
49
50
    const CPO_ERR_MSG = 'Order not confirmed';
51
    const CPO_OK_MSG = 'Order confirmed';
52
53
    const RMO_OK_MSG = 'Order process rollback successfully';
54
55
    /**
56
     * @var integer $statusCode
57
     */
58
    protected $statusCode = 200;
59
60
    /**
61
     * @var string $errorMessage
62
     */
63
    protected $errorMessage = '';
64
65
    /**
66
     * @var string $errorDetail
67
     */
68
    protected $errorDetail = '';
69
70
    /**
71
     * @var array $headers
72
     */
73
    protected $headers;
74
75
    /**
76
     * @var string $format
77
     */
78
    protected $format = 'json';
79
80
81
    /**
82
     * Return a printable response of the request
83
     *
84
     * @param array $extraOutput
85
     * @return mixed
86
     */
87
//    public function response($extraOutput = array())
88
//    {
89
//        $response = $this->getResponse();
90
//
91
//        $output = array();
92
//        if (!empty($this->errorMessage)) {
93
//            $output['errorMessage'] = $this->errorMessage;
94
//        }
95
//        if (!empty($this->errorDetail)) {
96
//            $output['errorDetail'] = $this->errorDetail;
97
//        }
98
//        if (count($extraOutput)) {
99
//            $output = array_merge($output, $extraOutput);
100
//        }
101
//        if ($this->format == 'json') {
102
//            $output = json_encode($output);
103
//            $response->setHeader('Content-Type', 'application/json');
104
//            $response->setHeader('Content-Length', strlen($output));
105
//        }
106
//
107
//        $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
108
//        $response->setHeader($protocol, $this->statusCode, $this->getHttpStatusCode($this->statusCode));
109
//        $response->setBody($output);
110
//
111
//        foreach ($this->headers as $key => $value) {
112
//            $response->setHeader($key, $value);
113
//        }
114
//        return $response;
115
//    }
116
117
    /**
118
     * Configure redirection
119
     *
120
     * @param bool   $error
121
     * @param string $url
122
     * @param array  $parameters
123
     */
124
    public function redirect($error = true, $url = '', $parameters = array())
125
    {
126
        if ($error) {
127
            Tools::redirect($url);
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...
128
            return;
129
        }
130
        $parsedUrl = parse_url($url);
131
        $separator = ($parsedUrl['query'] == null) ? '?' : '&';
132
        $redirectUrl = $url. $separator . http_build_query($parameters);
133
        Tools::redirect($redirectUrl);
134
    }
135
136
    /**
137
     * Return the HttpStatusCode description
138
     *
139
     * @param int $statusCode
140
     * @return string
141
     */
142
    public function getHttpStatusCode($statusCode = 200)
143
    {
144
        $httpStatusCodes = array(
145
            200 => "OK",
146
            201 => "Created",
147
            202 => "Accepted",
148
            400 => "Bad Request",
149
            401 => "Unauthorized",
150
            402 => "Payment Required",
151
            403 => "Forbidden",
152
            404 => "Not Found",
153
            405 => "Method Not Allowed",
154
            406 => "Not Acceptable",
155
            407 => "Proxy Authentication Required",
156
            408 => "Request Timeout",
157
            409 => "Conflict",
158
            429 => "Too Many Requests",
159
            500 => "Internal Server Error",
160
        );
161
        return isset($httpStatusCodes)? $httpStatusCodes[$statusCode] : $httpStatusCodes[200];
162
    }
163
164
    /**
165
     * Save log in SQL database
166
     *
167
     * @param array $data
168
     */
169
    public function saveLog($data = array())
170
    {
171
        try {
172
            $data = array_merge($data, array(
173
                'timestamp' => time(),
174
                'date' => date("Y-m-d H:i:s"),
175
            ));
176
177
            Db::getInstance()->insert('pmt_logs', array(
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...
178
                'log' => json_encode(str_replace('\'', '`', $data)),
179
            ));
180
        } catch (\Exception $exception) {
181
            // Do nothing
182
        }
183
    }
184
}
185