Passed
Push — master ( 400934...9d586b )
by Anton
02:32
created

HttpCollector::populate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 9.9666
cc 4
nc 5
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Debug\StateCollector;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Spiral\Debug\StateCollectorInterface;
19
use Spiral\Debug\StateInterface;
20
21
final class HttpCollector implements MiddlewareInterface, StateCollectorInterface
22
{
23
    /** @var ServerRequestInterface */
24
    private $request;
25
26
    /**
27
     * @param ServerRequestInterface  $request
28
     * @param RequestHandlerInterface $handler
29
     * @return ResponseInterface
30
     */
31
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33
        $this->request = $request;
34
        return $handler->handle($request);
35
    }
36
37
    /**
38
     * @param StateInterface $state
39
     */
40
    public function populate(StateInterface $state): void
41
    {
42
        if (!$this->request === null) {
0 ignored issues
show
introduced by
The condition ! $this->request === null is always false.
Loading history...
43
            return;
44
        }
45
46
        $state->setTag('method', $this->request->getMethod());
47
        $state->setTag('url', (string)$this->request->getUri());
48
49
        $state->setVariable('headers', $this->request->getHeaders());
50
51
        if ($this->request->getQueryParams() !== []) {
52
            $state->setVariable('query', $this->request->getQueryParams());
53
        }
54
55
        if ($this->request->getParsedBody() !== null) {
56
            $state->setVariable('data', $this->request->getParsedBody());
57
        }
58
    }
59
60
    /**
61
     * Reset captured request.
62
     */
63
    public function reset(): void
64
    {
65
        $this->request = null;
66
    }
67
}
68