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

RestModuleTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 25
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A responseBeforeSend() 0 16 3
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