Passed
Push — 1.x ( c24db9...389120 )
by Kevin
02:04
created

Kernel   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 3
Metric Value
eloc 50
c 8
b 0
f 3
dl 0
loc 114
rs 10
wmc 16

15 Methods

Rating   Name   Duplication   Size   Complexity  
A page2() 0 3 1
A page1() 0 3 1
A text() 0 3 1
A redirect2() 0 3 1
A httpMethod() 0 12 1
A redirect3() 0 3 1
A json() 0 3 1
A javascript() 0 3 1
A redirect1() 0 3 1
A submitForm() 0 5 1
A exception() 0 3 1
A configureContainer() 0 15 1
A registerBundles() 0 4 1
A user() 0 3 2
A configureRoutes() 0 14 1
1
<?php
2
3
namespace Zenstruck\Browser\Tests\Fixture;
4
5
use Psr\Log\NullLogger;
6
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
7
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
8
use Symfony\Bundle\SecurityBundle\SecurityBundle;
9
use Symfony\Component\Config\Loader\LoaderInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
17
use Symfony\Component\Routing\RouteCollectionBuilder;
18
use Symfony\Component\Security\Core\User\User;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
21
/**
22
 * @author Kevin Bond <[email protected]>
23
 */
24
final class Kernel extends BaseKernel
25
{
26
    use MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by Zenstruck\Browser\Tests\Fixture\Kernel.
Loading history...
27
28
    public function page1(): Response
29
    {
30
        return new Response(\file_get_contents(__DIR__.'/files/page1.html'));
31
    }
32
33
    public function page2(): Response
34
    {
35
        return new Response('success');
36
    }
37
38
    public function text(): Response
39
    {
40
        return new Response('text content', 200, ['Content-Type' => 'text/plain']);
41
    }
42
43
    public function javascript(): Response
44
    {
45
        return new Response(\file_get_contents(__DIR__.'/files/javascript.html'));
46
    }
47
48
    public function submitForm(Request $request): JsonResponse
49
    {
50
        return new JsonResponse(\array_merge(
51
            $request->request->all(),
52
            \array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $request->files->all())
53
        ));
54
    }
55
56
    public function httpMethod(Request $request): Response
57
    {
58
        return new JsonResponse([
59
            'method' => $request->getMethod(),
60
            'headers' => $request->headers->all(),
61
            'query' => $request->query->all(),
62
            'attributes' => $request->attributes->all(),
63
            'files' => $request->files->all(),
64
            'server' => $request->server->all(),
65
            'request' => $request->query->all(),
66
            'content' => $request->getContent(),
67
            'ajax' => $request->isXmlHttpRequest(),
68
        ]);
69
    }
70
71
    public function json(Request $request): JsonResponse
72
    {
73
        return new JsonResponse($request->getContent(), 200, [], true);
74
    }
75
76
    public function exception(): void
77
    {
78
        throw new \Exception('exception thrown');
79
    }
80
81
    public function redirect1(): RedirectResponse
82
    {
83
        return new RedirectResponse('/redirect2');
84
    }
85
86
    public function redirect2(): RedirectResponse
87
    {
88
        return new RedirectResponse('/redirect3');
89
    }
90
91
    public function redirect3(): RedirectResponse
92
    {
93
        return new RedirectResponse('/page1');
94
    }
95
96
    public function user(?UserInterface $user = null): Response
97
    {
98
        return new Response($user ? "user: {$user->getUsername()}/{$user->getPassword()}" : 'anon');
99
    }
100
101
    public function registerBundles(): iterable
102
    {
103
        yield new FrameworkBundle();
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new Symfony\Bundle...undle\FrameworkBundle() returns the type Generator which is incompatible with the return type mandated by Symfony\Component\HttpKe...face::registerBundles() of Symfony\Component\HttpKe...dleInterface[]|iterable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
104
        yield new SecurityBundle();
105
    }
106
107
    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

107
    protected function configureContainer(ContainerBuilder $c, /** @scrutinizer ignore-unused */ LoaderInterface $loader): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        $c->loadFromExtension('framework', [
110
            'secret' => 'S3CRET',
111
            'router' => ['utf8' => true],
112
            'test' => true,
113
            'profiler' => ['enabled' => true, 'collect' => true],
114
            'session' => ['storage_id' => 'session.storage.mock_file'],
115
        ]);
116
        $c->loadFromExtension('security', [
117
            'encoders' => [User::class => 'plaintext'],
118
            'providers' => ['users' => ['memory' => ['users' => ['kevin' => ['password' => 'pass']]]]],
119
            'firewalls' => ['main' => ['anonymous' => true]],
120
        ]);
121
        $c->register('logger', NullLogger::class); // disable logging
122
    }
123
124
    protected function configureRoutes(RouteCollectionBuilder $routes): void
125
    {
126
        $routes->add('/page1', 'kernel::page1');
127
        $routes->add('/page2', 'kernel::page2');
128
        $routes->add('/text', 'kernel::text');
129
        $routes->add('/submit-form', 'kernel::submitForm');
130
        $routes->add('/http-method', 'kernel::httpMethod');
131
        $routes->add('/exception', 'kernel::exception');
132
        $routes->add('/redirect1', 'kernel::redirect1');
133
        $routes->add('/redirect2', 'kernel::redirect2');
134
        $routes->add('/redirect3', 'kernel::redirect3');
135
        $routes->add('/json', 'kernel::json');
136
        $routes->add('/javascript', 'kernel::javascript');
137
        $routes->add('/user', 'kernel::user');
138
    }
139
}
140