OptionsHandlerExtension::implementedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
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\Extension;
13
14
use CakeDC\Api\Service\Action\DummyAction;
15
use CakeDC\Api\Service\Action\Result;
16
use Cake\Event\Event;
17
use Cake\Event\EventListenerInterface;
18
19
/**
20
 * Class CorsExtension
21
 *
22
 * @package CakeDC\Api\Service\Extension
23
 */
24
class OptionsHandlerExtension extends Extension implements EventListenerInterface
25
{
26
27
    /**
28
     * Events supported by this extension.
29
     *
30
     * @return array
31
     */
32
    public function implementedEvents()
33
    {
34
        return [
35
            'Service.beforeDispatch' => 'onDispatch',
36
        ];
37
    }
38
39
    /**
40
     * Cors process before dispatch.
41
     *
42
     * @param Event $Event An Event instance
43
     * @return Result|void
44
     */
45
    public function onDispatch(Event $Event)
46
    {
47
        $service = $Event->getData('service');
48
        $request = $service->getRequest();
49
50
        if ($request->is('options')) {
51
            $action = new DummyAction([
52
                'name' => 'options',
53
                'service' => $service,
54
                'route' => null,
55
                'Extension' => [
56
                    'CakeDC/Api.Cors'
57
                ]
58
            ]);
59
            $action->Auth->allow('options');
60
            $result = $service->getResult();
61
            $result->getData($action->process());
62
63
            return $result;
64
        }
65
    }
66
}
67