MessageFormatter::parsePhrase()   A
last analyzed

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;
6
use GuzzleHttp\Psr7;
7
use Psr\Http\Message\MessageInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
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('parse_'.$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 parseRequest()
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 parseResponse()
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 parseReqHeaders()
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 parseResHeaders()
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 parseReqBody()
95
    {
96
        return $this->request->getBody();
97
    }
98
99
    protected function parseResBody()
100
    {
101
        return $this->response ? $this->response->getBody() : 'NULL';
102
    }
103
104
    protected function parseDateIso8601()
105
    {
106
        return $this->parseTs();
107
    }
108
109
    protected function parseTs()
110
    {
111
        return gmdate('c');
112
    }
113
114
    protected function parseDateCommonLog()
115
    {
116
        return date('d/M/Y:H:i:s O');
117
    }
118
119
    protected function parseMethod()
120
    {
121
        return $this->request->getMethod();
122
    }
123
124
    protected function parseVersion()
125
    {
126
        return $this->request->getProtocolVersion();
127
    }
128
129
    protected function parseUrl()
130
    {
131
        return $this->parseUri();
132
    }
133
134
    protected function parseUri()
135
    {
136
        return $this->request->getUri();
137
    }
138
139
    protected function parseTarget()
140
    {
141
        return $this->request->getRequestTarget();
142
    }
143
144
    protected function parseReqVersion()
145
    {
146
        return $this->request->getProtocolVersion();
147
    }
148
149
    protected function parseResVersion()
150
    {
151
        return $this->response ? $this->response->getProtocolVersion() : 'NULL';
152
    }
153
154
    protected function parseHost()
155
    {
156
        return $this->request->getHeaderLine('Host');
157
    }
158
159
    protected function parseHostname()
160
    {
161
        return gethostname();
162
    }
163
164
    protected function parseCode()
165
    {
166
        return $this->response ? $this->response->getStatusCode() : 'NULL';
167
    }
168
169
    protected function parsePhrase()
170
    {
171
        return $this->response ? $this->response->getReasonPhrase() : 'NULL';
172
    }
173
174
    protected function parseError()
175
    {
176
        if ($this->error) {
177
            if ($this->error instanceof \Exception) {
0 ignored issues
show
introduced by
$this->error is always a sub-type of Exception. If $this->error can have other possible types, add them to Guzzle/GuzzleHttp/MessageFormatter.php:27.
Loading history...
178
                return $this->error->getMessage();
179
            }
180
181
            return (string)$this->error;
182
        }
183
184
        return 'NULL';
185
    }
186
}
187