Passed
Pull Request — master (#17)
by Alexander
03:51
created

OptionsRequestBehavior::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Horat1us\Yii\Behaviors;
4
5
use yii\base\Behavior;
6
use yii\base\ExitException;
7
use yii\web;
8
use yii\di;
9
10
/**
11
 * Class OptionsRequestBehavior
12
 * @package Horat1us\Yii\Behaviors
13
 */
14
class OptionsRequestBehavior extends Behavior
15
{
16
    /** @var string|array|web\Request */
17
    public $request = 'request';
18
19
    /** @var string|array|web\Response */
20
    public $response = 'response';
21
22 2
    public function init()
23
    {
24 2
        parent::init();
25 2
        $this->request = di\Instance::ensure($this->request,  web\Request::class);
26 2
        $this->response = di\Instance::ensure($this->response,  web\Response::class);
27 2
    }
28
29
    public function events()
30
    {
31
        return [
32
            web\Controller::EVENT_BEFORE_ACTION => 'check',
33
        ];
34
    }
35
36 2
    public function check(): void
37
    {
38 2
        if ($this->request->method === 'OPTIONS') {
39 1
            $this->response->send();
40 1
            throw new ExitException();
41
        }
42 1
    }
43
}
44