Completed
Push — master ( d442ab...0ebb9f )
by Marcel
10s
created

RequestContextProvider::getContext()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 7
nop 0
1
<?php
2
3
namespace BeyondCode\DumpServer;
4
5
use Illuminate\Http\Request;
6
use Symfony\Component\VarDumper\Cloner\VarCloner;
7
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
8
9
class RequestContextProvider implements ContextProviderInterface
10
{
11
    /** @var Request */
12
    private $currentRequest;
13
14
    /** @var VarCloner  */
15
    private $cloner;
16
17
    public function __construct(Request $currentRequest = null)
18
    {
19
        $this->currentRequest = $currentRequest;
20
        $this->cloner = new VarCloner();
21
        $this->cloner->setMaxItems(0);
22
    }
23
24
    public function getContext(): ?array
25
    {
26
        if (null === $this->currentRequest) {
27
            return null;
28
        }
29
30
        $controller = null;
31
32
        if ($route = $this->currentRequest->route()) {
33
            $controller = $route->controller;
34
35
            if (! $controller && ! is_string($route->action['uses'])) {
36
                $controller = $route->action['uses'];
37
            }
38
        }
39
40
        return array(
41
            'uri' => $this->currentRequest->getUri(),
42
            'method' => $this->currentRequest->getMethod(),
43
            'controller' => $controller ? $this->cloner->cloneVar(class_basename($controller)) : $controller,
44
            'identifier' => spl_object_hash($this->currentRequest),
45
        );
46
    }
47
}
48