Issues (3641)

Application/Log/Processor/GuzzleBodyProcessor.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Shared\Application\Log\Processor;
9
10
use Spryker\Service\UtilEncoding\Model\Json;
11
use Spryker\Shared\Log\Sanitizer\SanitizerInterface;
12
13
class GuzzleBodyProcessor
14
{
15
    /**
16
     * @var string
17
     */
18
    public const EXTRA = 'guzzle-body';
19
20
    /**
21
     * @var string
22
     */
23
    public const RECORD_CONTEXT = 'context';
24
25
    /**
26
     * @var string
27
     */
28
    public const RECORD_EXTRA = 'extra';
29
30
    /**
31
     * @var \Spryker\Shared\Log\Sanitizer\SanitizerInterface
32
     */
33
    protected $sanitizer;
34
35
    /**
36
     * @param \Spryker\Shared\Log\Sanitizer\SanitizerInterface $sanitizer
37
     */
38
    public function __construct(SanitizerInterface $sanitizer)
39
    {
40
        $this->sanitizer = $sanitizer;
41
    }
42
43
    /**
44
     * @param array $record
45
     *
46
     * @return array
47
     */
48
    public function __invoke(array $record)
49
    {
50
        if (isset($record[static::RECORD_CONTEXT][static::EXTRA])) {
51
            $body = $record[static::RECORD_CONTEXT][static::EXTRA];
52
            $record[static::RECORD_EXTRA][static::EXTRA] = $this->prepareBody($body);
53
54
            unset($record[static::RECORD_CONTEXT][static::EXTRA]);
55
        }
56
57
        return $record;
58
    }
59
60
    /**
61
     * @param array|string $body
62
     *
63
     * @return array
64
     */
65
    protected function prepareBody($body)
66
    {
67
        if (is_string($body) && $this->isJson($body)) {
68
            $jsonUtil = new Json();
69
            $body = $jsonUtil->decode($body, true);
70
        }
71
72
        if (is_array($body)) {
73
            $body = $this->sanitizer->sanitize($body);
74
        }
75
76
        if (is_string($body)) {
77
            $body = ['transfer-response' => $body];
78
        }
79
80
        $body = $this->prepareValues($body);
0 ignored issues
show
It seems like $body can also be of type null and string; however, parameter $body of Spryker\Shared\Applicati...cessor::prepareValues() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $body = $this->prepareValues(/** @scrutinizer ignore-type */ $body);
Loading history...
81
82
        return $body;
83
    }
84
85
    /**
86
     * @param array $body
87
     *
88
     * @return array
89
     */
90
    protected function prepareValues(array $body)
91
    {
92
        foreach ($body as $key => $value) {
93
            if (is_array($value)) {
94
                $body[$key] = $this->prepareValues($value);
95
            }
96
            if (is_bool($value)) {
97
                $body[$key] = (int)$value;
98
            }
99
        }
100
101
        return $body;
102
    }
103
104
    /**
105
     * @param string $data
106
     *
107
     * @return bool
108
     */
109
    protected function isJson($data)
110
    {
111
        json_decode($data);
112
113
        return (json_last_error() === JSON_ERROR_NONE);
114
    }
115
}
116