Passed
Push — 1.x ( a9c58b...abb6cb )
by Kevin
02:13
created

KernelBrowser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A catchExceptions() 0 5 1
A throwExceptions() 0 5 1
A actingAs() 0 5 1
A withProfiling() 0 5 1
A profile() 0 7 2
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Symfony\Bundle\FrameworkBundle\KernelBrowser as SymfonyKernelBrowser;
6
use Symfony\Component\HttpKernel\Profiler\Profile;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 *
12
 * @method SymfonyKernelBrowser inner()
13
 */
14
class KernelBrowser extends BrowserKitBrowser implements ProfileAware
15
{
16
    final public function __construct(SymfonyKernelBrowser $inner)
17
    {
18
        parent::__construct($inner);
19
    }
20
21
    /**
22
     * By default, exceptions made during a request are caught and converted
23
     * to responses by Symfony. This disables this behaviour and actually
24
     * throws the exception.
25
     *
26
     * @return static
27
     */
28
    final public function throwExceptions(): self
29
    {
30
        $this->inner()->catchExceptions(false);
31
32
        return $this;
33
    }
34
35
    /**
36
     * Re-enables catching exceptions.
37
     *
38
     * @return static
39
     */
40
    final public function catchExceptions(): self
41
    {
42
        $this->inner()->catchExceptions(true);
43
44
        return $this;
45
    }
46
47
    /**
48
     * Enable profiling for the next request. Not required if profiling is
49
     * globally enabled.
50
     *
51
     * @return static
52
     */
53
    final public function withProfiling(): self
54
    {
55
        $this->inner()->enableProfiler();
56
57
        return $this;
58
    }
59
60
    final public function actingAs(UserInterface $user): self
61
    {
62
        $this->inner()->loginUser($user);
63
64
        return $this;
65
    }
66
67
    final public function profile(): Profile
68
    {
69
        if (!$profile = $this->inner()->getProfile()) {
70
            throw new \RuntimeException('Profiler not enabled for this request. Try calling ->withProfiling() before the request.');
71
        }
72
73
        return $profile;
74
    }
75
}
76