Passed
Pull Request — 1.x (#74)
by Kevin
02:19
created

Kernel::securityEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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(Request $request): Response
30
    {
31
        if ($request->query->has('start-session')) {
32
            $request->getSession()->set('key', 'value');
33
        }
34
35
        return new Response(\file_get_contents(__DIR__.'/files/page1.html'));
36
    }
37
38
    public function page2(): Response
39
    {
40
        return new Response('success');
41
    }
42
43
    public function text(): Response
44
    {
45
        return new Response('text content', 200, ['Content-Type' => 'text/plain']);
46
    }
47
48
    public function javascript(): Response
49
    {
50
        return new Response(\file_get_contents(__DIR__.'/files/javascript.html'));
51
    }
52
53
    public function xml(): Response
54
    {
55
        return new Response(\file_get_contents(__DIR__.'/files/xml.xml'), 200, ['Content-Type' => 'text/xml']);
56
    }
57
58
    public function submitForm(Request $request): JsonResponse
59
    {
60
        $files = \array_map(
61
            static function($value) {
62
                if (\is_array($value)) {
63
                    return \array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $value);
64
                }
65
66
                return $value instanceof UploadedFile ? $value->getClientOriginalName() : null;
67
            },
68
            $request->files->all()
69
        );
70
71
        return new JsonResponse(\array_merge(
72
            $request->request->all(),
73
            \array_filter($files)
74
        ));
75
    }
76
77
    public function httpMethod(Request $request): Response
78
    {
79
        return new JsonResponse([
80
            'method' => $request->getMethod(),
81
            'headers' => $request->headers->all(),
82
            'query' => $request->query->all(),
83
            'attributes' => $request->attributes->all(),
84
            'files' => $request->files->all(),
85
            'server' => $request->server->all(),
86
            'request' => $request->request->all(),
87
            'content' => $request->getContent(),
88
            'ajax' => $request->isXmlHttpRequest(),
89
        ]);
90
    }
91
92
    public function json(Request $request): JsonResponse
93
    {
94
        return new JsonResponse(
95
            $request->getContent(),
96
            200,
97
            ['Content-Type' => $request->query->get('content-type', 'application/json')],
98
            true
99
        );
100
    }
101
102
    public function exception(): void
103
    {
104
        throw new \Exception('exception thrown');
105
    }
106
107
    public function redirect1(): RedirectResponse
108
    {
109
        return new RedirectResponse('/redirect2');
110
    }
111
112
    public function redirect2(): RedirectResponse
113
    {
114
        return new RedirectResponse('/redirect3');
115
    }
116
117
    public function redirect3(): RedirectResponse
118
    {
119
        return new RedirectResponse('/page1');
120
    }
121
122
    public function user(?UserInterface $user = null): Response
123
    {
124
        if ($user) {
125
            $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

125
            $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...
126
        }
127
128
        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...
129
    }
130
131
    public function registerBundles(): iterable
132
    {
133
        yield new FrameworkBundle();
134
        yield new SecurityBundle();
135
    }
136
137
    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

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