Completed
Push — 4-cactus ( 59c50f...21a27c )
by Alberto
02:40
created

BEdita/API/src/Event/CommonEventHandler.php (1 issue)

Severity
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2017 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace BEdita\API\Event;
14
15
use BEdita\API\Middleware\AnalyticsMiddleware;
16
use BEdita\API\Middleware\CorsMiddleware;
17
use BEdita\Core\Utility\LoggedUser;
18
use Cake\Core\Configure;
19
use Cake\Error\Middleware\ErrorHandlerMiddleware;
20
use Cake\Event\Event;
21
use Cake\Event\EventListenerInterface;
22
use Cake\Http\MiddlewareQueue;
23
24
/**
25
 * CommonEventsHandler class.
26
 *
27
 * This class contains common event listener attached bootstrapping the API plugin
28
 *
29
 * @since 4.0.0
30
 */
31
class CommonEventHandler implements EventListenerInterface
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function implementedEvents()
37
    {
38
        return [
39
            'Server.buildMiddleware' => 'buildMiddlewareStack',
40
            'Auth.afterIdentify' => 'afterIdentify',
41
        ];
42
    }
43
44
    /**
45
     * Customize middlewares for API needs
46
     *
47
     * Setup CORS from configuration
48
     * An optional 'CORS' key in should be like this example:
49
     *
50
     * ```
51
     * 'CORS' => [
52
     *   'allowOrigin' => '*.example.com',
53
     *   'allowMethods' => ['GET', 'POST'],
54
     *   'allowHeaders' => ['X-CSRF-Token']
55
     * ]
56
     * ```
57
     *
58
     * @param \Cake\Event\Event $event The event object
59
     * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue
60
     * @return void
61
     * @see \BEdita\API\Middleware\CorsMiddleware to more info on CORS configuration
62
     */
63
    public function buildMiddlewareStack(Event $event, MiddlewareQueue $middleware)
0 ignored issues
show
The parameter $event 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

63
    public function buildMiddlewareStack(/** @scrutinizer ignore-unused */ Event $event, MiddlewareQueue $middleware)

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...
64
    {
65
        $middleware
66
            ->prepend(new AnalyticsMiddleware())
67
            ->insertBefore(
68
                ErrorHandlerMiddleware::class,
69
                new CorsMiddleware(Configure::read('CORS'))
70
            );
71
    }
72
73
    /**
74
     * Set the user identified.
75
     * Called after user authentication.
76
     *
77
     * @param \Cake\Event\Event $event The event object
78
     * @param array $user The user data
79
     * @return void
80
     * @throws \Cake\Network\Exception\UnauthorizedException
81
     */
82
    public function afterIdentify(Event $event, array $user)
83
    {
84
        LoggedUser::setUser($user);
85
    }
86
}
87