CorsExtension::onAction()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 1
nop 1
dl 0
loc 22
rs 9.2568
c 0
b 0
f 0
ccs 20
cts 20
cp 1
crap 5
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action\Extension;
13
14
use CakeDC\Api\Service\Action\Action;
15
use Cake\Event\Event;
16
use Cake\Event\EventListenerInterface;
17
18
/**
19
 * Class CorsExtension
20
 *
21
 * @package CakeDC\Api\Service\Action\Extension
22
 */
23
class CorsExtension extends Extension implements EventListenerInterface
24
{
25
26
    /**
27
     * Events supported by this extension.
28
     *
29
     * @return array
30
     */
31 1
    public function implementedEvents()
32
    {
33
        return [
34 1
            'Action.beforeProcess' => 'onAction',
35 1
        ];
36
    }
37
38
    /**
39
     * new entity
40
     *
41
     * @param Event $Event An Event instance
42
     * @return void
43
     */
44 1
    public function onAction(Event $Event)
45
    {
46 1
        $action = $Event->getSubject();
47 1
        $request = $action->getService()->getRequest();
48 1
        $response = $action->getService()->getResponse();
49 1
        $action->getService()->setResponse(
50 1
            $response->cors($request)
51 1
                 ->allowOrigin($this->getConfig('origin') ?: ['*'])
52 1
                 ->allowMethods($this->getConfig('methods') ?: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'])
53 1
                 ->allowHeaders($this->getConfig('headers') ?: [
54 1
                     'X-CSRF-Token',
55 1
                     'Content-Type',
56 1
                     'Access-Control-Allow-Headers',
57 1
                     'Access-Control-Allow-Origin',
58 1
                     'Authorization',
59
                     'X-Requested-With'
60 1
                 ])
61 1
                 ->allowCredentials()
62 1
                 ->maxAge($this->getConfig('maxAge') ?: 300)
63 1
                 ->build()
64 1
        );
65 1
    }
66
}
67