Passed
Pull Request — master (#35)
by Raúl
03:04
created

AbstractController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 10
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
use Pagantis\ModuleUtils\Model\Log\LogEntry;
11
12
/**
13
 * Class AbstractController
14
 */
15
abstract class AbstractController extends ModuleFrontController
16
{
17
    /**
18
     * PAGANTIS_CODE
19
     */
20
    const PAGANTIS_CODE = 'pagantis';
21
22
    /**
23
     * @var array $headers
24
     */
25
    protected $headers;
26
27
    /**
28
     * Configure redirection
29
     *
30
     * @param string $url
31
     * @param array  $parameters
32
     */
33
    public function redirect($url = '', $parameters = array())
34
    {
35
        $parsedUrl = parse_url($url);
36
        $separator = ($parsedUrl['query'] == null) ? '?' : '&';
37
        $redirectUrl = $url. $separator . http_build_query($parameters);
38
        Tools::redirect($redirectUrl);
39
    }
40
41
    /**
42
     * Save log in SQL database
43
     *
44
     * @param array $data
45
     * @param \Exception  $exception
46
     */
47
    public function saveLog($data = array(), $exception = null)
48
    {
49
        try {
50
            $logEntry = new LogEntry();
51
            if (count($data) > 0) {
52
                if (isset($data['message'])) {
53
                    $logEntry->setMessage($data['message']);
54
                }
55
                if (isset($data['line'])) {
56
                    $logEntry->setLine($data['line']);
57
                }
58
                if (isset($data['file'])) {
59
                    $logEntry->setFile($data['file']);
60
                }
61
                if (isset($data['code'])) {
62
                    $logEntry->setCode($data['code']);
63
                }
64
                if (isset($data['trace'])) {
65
                    $logEntry->setTrace($data['trace']);
66
                }
67
            }  elseif (!isNull($exception)) {
68
                $logEntry->error($exception);
69
            }
70
71
            $response = $logEntry->toJson();
72
73
            if (isNull($response)) {
74
                if (count($data) > 0) {
75
                    $response = json_encode($data);
76
                } elseif (!isNull($exception)) {
77
                    $response = $exception->getMessage();
78
                } else {
79
                    $response = 'Unable to serialize log.'.
80
                        'data: '. json_encode($data).
81
                        'exception: '. json_encode($exception)
82
                }
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '}' on line 82 at column 16
Loading history...
83
            }
84
85
            Db::getInstance()->insert('pagantis_log', array(
86
                'log' => $response
87
            ));
88
        } catch (\Exception $exception) {
89
            // Do nothing
90
        }
91
    }
92
}
93