Logger::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Components;
9
10
/**
11
 * Logger for connectGateway operations
12
 *
13
 * Class Logger
14
 * @package ShopwarePlugins\Connect\Components
15
 */
16
class Logger
17
{
18
    /** @var \PDO|\Enlight_Components_Db_Adapter_Pdo_Mysql */
19
    protected $db;
20
21
    protected $enabled;
22
23
    public function __construct($db, $enabled = true)
24
    {
25
        $this->db = $db;
26
        $this->enabled = $enabled;
27
    }
28
29
    /**
30
     * Write the log
31
     *
32
     * @param $isError
33
     * @param $request
34
     * @param $response
35
     * @param $custom
36
     */
37
    public function write($isError, $request, $response, $custom = null)
38
    {
39
        if ($response instanceof \Exception) {
40
            $this->formatException($response);
41
        }
42
43
        $service='general';
44
        $command = 'custom-error';
45
        if ($custom) {
46
            $command = $command . '-' . $custom;
47
        }
48
49
        if ($request && !$custom) {
50
            $document = simplexml_load_string($request);
51
            if ($document) {
52
                $service = $document->service;
0 ignored issues
show
Bug introduced by
The property service does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
                $command = $document->command;
0 ignored issues
show
Bug introduced by
The property command does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
            }
55
        }
56
57
        if (!$this->enabled && !in_array($command, ['checkProducts', 'reserveProducts', 'buy'])) {
58
            return;
59
        }
60
61
        $this->db->query('
62
            INSERT INTO `s_plugin_connect_log`
63
            (`isError`, `service`, `command`, `request`, `response`, `time`)
64
            VALUES (?, ?, ?, ?, ?, NOW())
65
        ', [$isError, $service, $command, $request, $response]);
66
67
        if (random_int(0, 100) === 0) {
68
            $this->cleanup();
69
        }
70
    }
71
72
    /**
73
     * Format a given exception for the log
74
     *
75
     * @param \Exception $e
76
     * @return string
77
     */
78
    public function formatException(\Exception $e)
79
    {
80
        return sprintf(
81
            "%s \n\n %s \n\n %s",
82
            $e->getMessage(),
83
            $e->getFile() . ': ' . $e->getLine(),
84
            $e->getTraceAsString()
85
        );
86
    }
87
88
    /**
89
     * Purge the log after one day
90
     */
91
    protected function cleanup()
92
    {
93
        $this->db->exec('DELETE FROM `s_plugin_connect_log`  WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) >= time');
94
    }
95
}
96