Passed
Push — 1.x ( a4b838...b15378 )
by Kevin
07:01
created

Kernel   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 10
Bugs 1 Features 3
Metric Value
eloc 84
c 10
b 1
f 3
dl 0
loc 176
rs 10
wmc 25

17 Methods

Rating   Name   Duplication   Size   Complexity  
A configureContainer() 0 20 2
A text() 0 3 1
A redirect2() 0 3 1
A httpMethod() 0 12 1
A securityEnabled() 0 3 1
A xml() 0 3 1
A redirect3() 0 3 1
A json() 0 7 1
A javascript() 0 3 1
A registerBundles() 0 6 2
A page2() 0 3 1
A redirect1() 0 3 1
A page1() 0 3 1
A submitForm() 0 16 3
A user() 0 7 4
A configureRoutes() 0 33 2
A exception() 0 3 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\Loader\Configurator\RoutingConfigurator;
18
use Symfony\Component\Routing\RouteCollectionBuilder;
19
use Symfony\Component\Security\Core\User\InMemoryUser;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
22
/**
23
 * @author Kevin Bond <[email protected]>
24
 */
25
final class Kernel extends BaseKernel
26
{
27
    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...
28
29
    public function page1(): Response
30
    {
31
        return new Response(\file_get_contents(__DIR__.'/files/page1.html'));
32
    }
33
34
    public function page2(): Response
35
    {
36
        return new Response('success');
37
    }
38
39
    public function text(): Response
40
    {
41
        return new Response('text content', 200, ['Content-Type' => 'text/plain']);
42
    }
43
44
    public function javascript(): Response
45
    {
46
        return new Response(\file_get_contents(__DIR__.'/files/javascript.html'));
47
    }
48
49
    public function xml(): Response
50
    {
51
        return new Response(\file_get_contents(__DIR__.'/files/xml.xml'), 200, ['Content-Type' => 'text/xml']);
52
    }
53
54
    public function submitForm(Request $request): JsonResponse
55
    {
56
        $files = \array_map(
57
            static function($value) {
58
                if (\is_array($value)) {
59
                    return \array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $value);
60
                }
61
62
                return $value instanceof UploadedFile ? $value->getClientOriginalName() : null;
63
            },
64
            $request->files->all()
65
        );
66
67
        return new JsonResponse(\array_merge(
68
            $request->request->all(),
69
            \array_filter($files)
70
        ));
71
    }
72
73
    public function httpMethod(Request $request): Response
74
    {
75
        return new JsonResponse([
76
            'method' => $request->getMethod(),
77
            'headers' => $request->headers->all(),
78
            'query' => $request->query->all(),
79
            'attributes' => $request->attributes->all(),
80
            'files' => $request->files->all(),
81
            'server' => $request->server->all(),
82
            'request' => $request->request->all(),
83
            'content' => $request->getContent(),
84
            'ajax' => $request->isXmlHttpRequest(),
85
        ]);
86
    }
87
88
    public function json(Request $request): JsonResponse
89
    {
90
        return new JsonResponse(
91
            $request->getContent(),
92
            200,
93
            ['Content-Type' => $request->query->get('content-type', 'application/json')],
94
            true
95
        );
96
    }
97
98
    public function exception(): void
99
    {
100
        throw new \Exception('exception thrown');
101
    }
102
103
    public function redirect1(): RedirectResponse
104
    {
105
        return new RedirectResponse('/redirect2');
106
    }
107
108
    public function redirect2(): RedirectResponse
109
    {
110
        return new RedirectResponse('/redirect3');
111
    }
112
113
    public function redirect3(): RedirectResponse
114
    {
115
        return new RedirectResponse('/page1');
116
    }
117
118
    public function user(?UserInterface $user = null): Response
119
    {
120
        if ($user) {
121
            $username = \method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Securi...nterface::getUsername() has been deprecated: since Symfony 5.3, use getUserIdentifier() instead ( Ignorable by Annotation )

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

121
            $username = \method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : /** @scrutinizer ignore-deprecated */ $user->getUsername();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
122
        }
123
124
        return new Response($user ? "user: {$username}/{$user->getPassword()}" : 'anon');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $username does not seem to be defined for all execution paths leading up to this point.
Loading history...
125
    }
126
127
    public function registerBundles(): iterable
128
    {
129
        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...
130
131
        if (self::securityEnabled()) {
132
            yield new SecurityBundle();
133
        }
134
    }
135
136
    public static function securityEnabled(): bool
137
    {
138
        return self::VERSION_ID >= 50300;
139
    }
140
141
    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

141
    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...
142
    {
143
        $c->loadFromExtension('framework', [
144
            'secret' => 'S3CRET',
145
            'router' => ['utf8' => true],
146
            'test' => true,
147
            'profiler' => ['enabled' => true, 'collect' => true],
148
        ]);
149
        $c->register('logger', NullLogger::class); // disable logging
150
151
        if (self::securityEnabled()) {
152
            $c->loadFromExtension('security', [
153
                'enable_authenticator_manager' => true,
154
                'password_hashers' => [InMemoryUser::class => 'plaintext'],
155
                'providers' => ['users' => ['memory' => ['users' => ['kevin' => ['password' => 'pass']]]]],
156
                'firewalls' => ['main' => []],
157
            ]);
158
159
            $c->loadFromExtension('framework', [
160
                'session' => ['storage_factory_id' => 'session.storage.factory.mock_file'],
161
            ]);
162
        }
163
    }
164
165
    /**
166
     * @param RouteCollectionBuilder|RoutingConfigurator $routes
167
     */
168
    protected function configureRoutes($routes): void
169
    {
170
        if ($routes instanceof RouteCollectionBuilder) {
171
            $routes->add('/page1', 'kernel::page1');
172
            $routes->add('/page2', 'kernel::page2');
173
            $routes->add('/text', 'kernel::text');
174
            $routes->add('/submit-form', 'kernel::submitForm');
175
            $routes->add('/http-method', 'kernel::httpMethod');
176
            $routes->add('/exception', 'kernel::exception');
177
            $routes->add('/redirect1', 'kernel::redirect1');
178
            $routes->add('/redirect2', 'kernel::redirect2');
179
            $routes->add('/redirect3', 'kernel::redirect3');
180
            $routes->add('/json', 'kernel::json');
181
            $routes->add('/xml', 'kernel::xml');
182
            $routes->add('/javascript', 'kernel::javascript');
183
            $routes->add('/user', 'kernel::user');
184
185
            return;
186
        }
187
188
        $routes->add('page1', '/page1')->controller('kernel::page1');
189
        $routes->add('page2', '/page2')->controller('kernel::page2');
190
        $routes->add('text', '/text')->controller('kernel::text');
191
        $routes->add('submit-form', '/submit-form')->controller('kernel::submitForm');
192
        $routes->add('http-method', '/http-method')->controller('kernel::httpMethod');
193
        $routes->add('exception', '/exception')->controller('kernel::exception');
194
        $routes->add('redirect1', '/redirect1')->controller('kernel::redirect1');
195
        $routes->add('redirect2', '/redirect2')->controller('kernel::redirect2');
196
        $routes->add('redirect3', '/redirect3')->controller('kernel::redirect3');
197
        $routes->add('json', '/json')->controller('kernel::json');
198
        $routes->add('xml', '/xml')->controller('kernel::xml');
199
        $routes->add('javascript', '/javascript')->controller('kernel::javascript');
200
        $routes->add('user', '/user')->controller('kernel::user');
201
    }
202
}
203