Completed
Push — master ( c30967...3bd52a )
by Andrii
03:06
created

AbstractResponse::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart;
12
13
use yii\helpers\Json;
14
15
abstract class AbstractResponse implements ResponseInterface
16
{
17
    /**
18
     * @var RequestInterface
19
     */
20
    protected $request;
21
22
    /**
23
     * @var string response data. The property contains RAW response data
24
     * @see decodeData()
25
     * @see isDecoded
26
     */
27
    protected $data;
28
29
    /**
30
     * @var bool whether response is already decoded
31
     */
32
    protected $isDecoded = false;
33
34
    public function getRequest()
35
    {
36
        return $this->request;
37
    }
38
39 2
    public function getData()
40
    {
41 2
        if (!$this->isDecoded) {
42 2
            $this->data = $this->decodeData();
43 2
            $this->isDecoded = true;
44
        }
45
46 2
        return $this->data;
47
    }
48
49 2
    public function decodeData()
50
    {
51 2
        $data = $this->getRawData();
52
53 2
        if ($this->isRaw()) {
54
            return $data;
55
        }
56
57 2
        if ($this->isJson()) {
58 2
            return Json::decode($data);
59
        }
60
61
        // TODO: implement decoding for XML and other types
62
63
        // throw new ResponseDecodingException('Failed to detect response data type', $this);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
        // TODO: throw exception instead of returning
65
        return $data;
66
    }
67
68
    /**
69
     * Method returns RAW request data.
70
     *
71
     * @return string
72
     */
73
    abstract public function getRawData();
74
75
    /**
76
     * Whether the request is RAW and should not be decoded.
77
     * @return bool
78
     */
79 2
    public function isRaw()
80
    {
81 2
        return $this->request->isRaw();
82
    }
83
84
    /**
85
     * Method checks whether response is a JSON response.
86
     * @return bool
87
     */
88 2
    public function isJson()
89
    {
90 2
        $value = $this->getHeader('Content-Type');
91 2
        if ($value === null) {
92
            return false;
93
        }
94
95 2
        return !empty(preg_grep('|application/json|i', $value));
96
    }
97
98
    /**
99
     * @param $name
100
     * @return array
101
     */
102
    abstract public function getHeader($name);
103
}
104