Completed
Push — master ( 63793c...96fdaa )
by vistart
06:04
created

RestModuleTrait::responseBeforeSend()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * This trait is used for building module with RESTful API.
17
 *
18
 * @version 2.0
19
 * @author vistart <[email protected]>
20
 */
21
trait RestModuleTrait
22
{
23
24
    /**
25
     * This event will move response data into array which contains `success` and
26
     * `data` elements, and replace status code with 200.
27
     * @param \yii\base\Event $event
28
     */
29
    protected function responseBeforeSend($event)
30
    {
31
        $response = $event->sender;
32
        /* @var $response \yii\web\Response */
33
        if ($response->data !== null) {
34
            // Clear the 'type' property in all responses.
35
            if (isset($response->data['type'])) {
36
                unset($response->data['type']);
37
            }
38
            $response->data = [
39
                'success' => $response->isSuccessful,
40
                'data' => $response->data
41
            ];
42
            $response->statusCode = 200;
43
        }
44
    }
45
}
46