Completed
Push — master ( bbba06...904527 )
by Rémi
04:22
created

CodebirdResponseParser::parseObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Twitter\API\REST\Gateway;
4
5
use Twitter\API\Exception\TwitterException;
6
use Twitter\API\REST\Response\ApiRate;
7
use Twitter\API\REST\Response\ApiResponse;
8
use Twitter\API\REST\Response\HttpStatus;
9
use Twitter\API\REST\Response\LimitedApiRate;
10
use Twitter\API\REST\Response\UnlimitedApiRate;
11
12
class CodebirdResponseParser
13
{
14
    /**
15
     * @param object $result
16
     *
17
     * @return ApiResponse
18
     *
19
     * @throws TwitterException
20
     */
21 42
    public function parseObject($result)
22
    {
23 42
        return $this->getResponse($result, false);
24
    }
25
26
    /**
27
     * @param object $result
28
     *
29
     * @return ApiResponse
30
     *
31
     * @throws TwitterException
32
     */
33 9
    public function parseList($result)
34
    {
35 9
        return $this->getResponse($result, true);
36
    }
37
38
    /**
39
     * Handles a twitter API response object
40
     *
41
     * @param object $result
42
     * @param bool   $isList
43
     *
44
     * @return ApiResponse
45
     *
46
     * @throws TwitterException
47
     */
48 51
    private function getResponse($result, $isList)
49
    {
50 51
        $this->handleErrors($result);
51
52 45
        $httpStatus = $this->getHttpStatus($result);
53 39
        $rate = $this->getRate($result);
54 39
        $content = $this->getContent($result, $isList);
55
56 39
        return new ApiResponse($httpStatus, $content, $rate);
57
    }
58
59
    /**
60
     * @param object $result
61
     *
62
     * @throws TwitterException
63
     */
64 51
    private function handleErrors($result)
65
    {
66 51
        if (isset($result->errors)) {
67 6
            $error = reset($result->errors);
68 6
            throw new TwitterException($error->message, $error->code);
69
        }
70 45
    }
71
72
    /**
73
     * @param object $result
74
     *
75
     * @return HttpStatus
76
     *
77
     * @throws TwitterException
78
     */
79 45
    private function getHttpStatus($result)
80
    {
81 45
        $httpStatus = new HttpStatus($result->httpstatus);
82 45
        if ($httpStatus->isError()) {
83 6
            throw new TwitterException($result->message);
84
        }
85
86 39
        return $httpStatus;
87
    }
88
89
    /**
90
     * @param object $result
91
     *
92
     * @return ApiRate
93
     */
94 39
    private function getRate($result)
95
    {
96 39
        if (isset($result->rate)) {
97 3
            return new LimitedApiRate(
98 3
                $result->rate['limit'],
99 3
                $result->rate['remaining'],
100 3
                $result->rate['reset']
101 2
            );
102
        }
103
104 36
        return new UnlimitedApiRate();
105
    }
106
107
    /**
108
     * @param object $result
109
     * @param bool   $isList
110
     *
111
     * @return object|array|null
112
     */
113 39
    private function getContent($result, $isList)
114
    {
115 39
        $content = $result;
116
117 39
        unset($content->httpstatus, $content->rate);
118
119 39
        if ($isList) {
120 9
            $content = [];
121 9
            foreach ($result as $index => $obj) {
122 3
                if (is_numeric($index)) {
123 5
                    $content[(int) $index] = $obj;
124 2
                }
125 6
            }
126 36
        } elseif (count((array) $content) === 0) {
127 18
            $content = null;
128 12
        }
129
130 39
        return $content;
131
    }
132
}
133