Completed
Push — next ( ba018c...5105e3 )
by Mathias
10:34
created

ApiResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
    public function __construct(Response $response) {
48
        $this->response = $response->toString();
49
        $this->responseObject = $response;
50
    }
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
    public function __call($method, $args)
62
    {
63
        $response = $this->getResponse();
64
        $callback = [$response, $method];
65
66
        if (is_callable($callback)) {
67
            $returned = call_user_func_array($callback, $args);
68
69
            return $returned === $response ? $this : $returned;
70
        }
71
72
        throw new \BadMethodCallException('Unknown method "' . $method . '"');
73
    }
74
75
    /**
76
     * Get the response object.
77
     *
78
     * @return Response
79
     */
80
    public function getResponse()
81
    {
82
        if (!$this->responseObject) {
83
            $this->responseObject = Response::fromString($this->response);
84
        }
85
86
        return $this->responseObject;
87
    }
88
}
89