Completed
Push — master ( f81184...3aae6e )
by Andrii
04:04
created

AbstractResponse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 80
ccs 15
cts 19
cp 0.7895
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequest() 0 4 1
A decodeData() 0 18 3
getRawData() 0 1 ?
A isRaw() 0 4 1
A isJson() 0 4 1
getHeader() 0 1 ?
A getData() 0 9 2
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
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 2
        }
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
        return !empty(preg_grep('|application/json|i', $this->getHeader('Content-Type')));
91
    }
92
93
    abstract public function getHeader($name);
94
}
95