ApiResponse   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 65
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 13 3
A getResponse() 0 8 2
1
<?php
2
/**
3
 * YAWIK Stackoverflow API
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2016 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace StackoverflowApi\Entity;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
use Core\Entity\EntityTrait;
15
use StackoverflowApi\Client\Response;
16
17
/**
18
 * Stackoverflow API Response entity.
19
 *
20
 * @ODM\EmbeddedDocument
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @since 0.1.0
23
 */
24
class ApiResponse implements ApiResponseInterface
25
{
26
    use EntityTrait;
27
28
    /**
29
     * The stringified response.
30
     *
31
     * @ODM\Field(type="string")
32
     * @var string
33
     */
34
    protected $response;
35
36
    /**
37
     * The response object.
38
     *
39
     * @var Response
40
     */
41
    protected $responseObject;
42
43
44
    /**
45
     * @param Response $response
46
     */
47 5
    public function __construct(Response $response) {
48 5
        $this->response = $response->toString();
49 5
        $this->responseObject = $response;
50 5
    }
51
52
    /**
53
     * Proxies to Response object.
54
     *
55
     * @param string $method
56
     * @param array $args
57
     *
58
     * @return self|Response
59
     * @throws \BadMethodCallException
60
     */
61 3
    public function __call($method, $args)
62
    {
63 3
        $response = $this->getResponse();
64 3
        $callback = [$response, $method];
65
66 3
        if (is_callable($callback)) {
67 2
            $returned = call_user_func_array($callback, $args);
68
69 2
            return $returned === $response ? $this : $returned;
70
        }
71
72 1
        throw new \BadMethodCallException('Unknown method "' . $method . '"');
73
    }
74
75
    /**
76
     * Get the response object.
77
     *
78
     * @return Response
79
     */
80 4
    public function getResponse()
81
    {
82 4
        if (!$this->responseObject) {
83 1
            $this->responseObject = Response::fromString($this->response);
84 1
        }
85
86 4
        return $this->responseObject;
87
    }
88
}
89