Passed
Push — master ( a51f35...a51f35 )
by Dāvis
05:00
created

MessageFormatter::sludioCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Guzzle\GuzzleHttp;
4
5
use GuzzleHttp\MessageFormatter as BaseMessageFormatter;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\MessageFormatter 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...
6
use GuzzleHttp\Psr7;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Psr7 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...
7
use Psr\Http\Message\MessageInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\MessageInterface 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...
8
use Psr\Http\Message\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\RequestInterface 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...
9
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface 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...
10
use Sludio\HelperBundle\Script\Utils\Helper;
11
12
class MessageFormatter extends BaseMessageFormatter
13
{
14
    public $template;
15
16
    /**
17
     * @var RequestInterface
18
     */
19
    protected $request;
20
21
    /**
22
     * @var ResponseInterface
23
     */
24
    protected $response;
25
26
    /**
27
     * @var \Exception
28
     */
29
    protected $error;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function format(RequestInterface $request, ResponseInterface $response = null, \Exception $error = null)
35
    {
36
        $cache = [];
37
38
        return preg_replace_callback('/{\s*([A-Za-z_\-\.0-9]+)\s*}/', function(array $matches) use ($request, $response, $error, &$cache) {
39
            if (isset($cache[$matches[1]])) {
40
                return $cache[$matches[1]];
41
            }
42
43
            $cache[$matches[1]] = '';
44
45
            $method = Helper::toCamelCase('sludio_'.$matches[1]);
46
            if (method_exists($this, $method)) {
47
                $this->request = $request;
48
                $this->response = $response;
49
                $this->error = $error;
50
51
                $cache[$matches[1]] = $this->{$method}();
52
            } else {
53
                if (strpos($matches[1], 'req_header_') === 0) {
54
                    $cache[$matches[1]] = $request->getHeaderLine(substr($matches[1], 11));
55
                } elseif (strpos($matches[1], 'res_header_') === 0) {
56
                    $cache[$matches[1]] = $response ? $response->getHeaderLine(substr($matches[1], 11)) : 'NULL';
57
                }
58
            }
59
60
            return $cache[$matches[1]];
61
        }, $this->template);
62
    }
63
64
    protected function sludioRequest()
65
    {
66
        return Psr7\str($this->request);
0 ignored issues
show
Bug introduced by
The function str 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

66
        return /** @scrutinizer ignore-call */ Psr7\str($this->request);
Loading history...
67
    }
68
69
    protected function sludioResponse()
70
    {
71
        return $this->response ? Psr7\str($this->response) : '';
0 ignored issues
show
Bug introduced by
The function str 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

71
        return $this->response ? /** @scrutinizer ignore-call */ Psr7\str($this->response) : '';
Loading history...
72
    }
73
74
    protected function sludioReqHeaders()
75
    {
76
        return trim($this->request->getMethod().' '.(string)$this->request).' HTTP/'.$this->request->getProtocolVersion()."\r\n".$this->headers($this->request);
77
    }
78
79
    private function headers(MessageInterface $message)
80
    {
81
        $result = '';
82
        foreach ($message->getHeaders() as $name => $values) {
83
            $result .= $name.': '.implode(', ', $values)."\r\n";
84
        }
85
86
        return trim($result);
87
    }
88
89
    protected function sludioResHeaders()
90
    {
91
        return $this->response ? sprintf('HTTP/%s %d %s', $this->response->getProtocolVersion(), $this->response->getStatusCode(), $this->response->getReasonPhrase())."\r\n".$this->headers($this->response) : 'NULL';
92
    }
93
94
    protected function sludioReqBody()
95
    {
96
        return $this->request->getBody();
97
    }
98
99
    protected function sludioResBody()
100
    {
101
        return $this->response ? $this->response->getBody() : 'NULL';
102
    }
103
104
    protected function sludioDateIso8601()
105
    {
106
        return $this->sludioTs();
107
    }
108
109
    protected function sludioTs()
110
    {
111
        return gmdate('c');
112
    }
113
114
    protected function sludioDateCommonLog()
115
    {
116
        return date('d/M/Y:H:i:s O');
117
    }
118
119
    protected function sludioMethod()
120
    {
121
        return $this->request->getMethod();
122
    }
123
124
    protected function sludioVersion()
125
    {
126
        return $this->request->getProtocolVersion();
127
    }
128
129
    protected function sludioUrl()
130
    {
131
        return $this->sludioUri();
132
    }
133
134
    protected function sludioUri()
135
    {
136
        return $this->request->getUri();
137
    }
138
139
    protected function sludioTarget()
140
    {
141
        return $this->request->getRequestTarget();
142
    }
143
144
    protected function sludioReqVersion()
145
    {
146
        return $this->request->getProtocolVersion();
147
    }
148
149
    protected function sludioResVersion()
150
    {
151
        return $this->response ? $this->response->getProtocolVersion() : 'NULL';
152
    }
153
154
    protected function sludioHost()
155
    {
156
        return $this->request->getHeaderLine('Host');
157
    }
158
159
    protected function sludioHostname()
160
    {
161
        return gethostname();
162
    }
163
164
    protected function sludioCode()
165
    {
166
        return $this->response ? $this->response->getStatusCode() : 'NULL';
167
    }
168
169
    protected function sludioPhrase()
170
    {
171
        return $this->response ? $this->response->getReasonPhrase() : 'NULL';
172
    }
173
174
    protected function sludioError()
175
    {
176
        if ($this->error) {
177
            if ($this->error instanceof \Exception) {
0 ignored issues
show
introduced by
The condition $this->error instanceof Exception can never be false since $this->error is always a sub-type of Exception.
Loading history...
178
                return $this->error->getMessage();
179
            }
180
181
            return (string)$this->error;
182
        }
183
184
        return 'NULL';
185
    }
186
}
187