Completed
Push — master ( d919b4...0e4636 )
by Francesco
05:43
created

RepositoryResponse::fromResponse()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 25
rs 8.8571
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace Audiens\AppnexusClient\repository;
4
5
use Audiens\AppnexusClient\entity\Error;
6
use GuzzleHttp\Psr7\Response;
7
8
/**
9
 * Class RepositoryResponse
10
 */
11
class RepositoryResponse
12
{
13
14
    // OK RESPONSE {"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"id":4959394,"segment":{"id":4959394,"active":true,"description":null,"member_id":3847,"code":null,"provider":"","price":0,"short_name":"Test segment4996","expire_minutes":null,"category":null,"enable_rm_piggyback":false,"last_activity":"2016-03-23 11:21:48","max_usersync_pixels":null,"parent_segment_id":null,"querystring_mapping":null,"querystring_mapping_key_value":null},"dbg":{"instance":"41.api.prod.ams1","slave_hit":false,"db":"master","user::reads":0,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":2,"user::write_limit":60,"user::write_limit_seconds":60,"reads":0,"read_limit":1073741824,"read_limit_seconds":60,"writes":2,"write_limit":1073741824,"write_limit_seconds":60,"parent_dbg_info":{"instance":"44.bm-api.prod.nym2","slave_hit":false,"db":"master","user::reads":0,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":2,"user::write_limit":60,"user::write_limit_seconds":60,"reads":0,"read_limit":1073741824,"read_limit_seconds":60,"writes":2,"write_limit":1073741824,"write_limit_seconds":60,"time":88.721990585327,"version":"1.16.497","warnings":["Field `member_id` is not available"],"slave_lag":0,"start_microtime":1458732108.3462},"time":347.10383415222,"version":"1.16.497","warnings":[],"slave_lag":0,"start_microtime":1458732108.1427}}}
0 ignored issues
show
Unused Code Comprehensibility introduced by
99% 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...
15
    // OK DELETE "{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"id":"4967144","dbg":{"instance":"41.api.prod.ams1","slave_hit":true,"db":"10.2.78.139","user::reads":1,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":5,"user::write_limit":60,"user::write_limit_seconds":60,"reads":1,"read_limit":1073741824,"read_limit_seconds":60,"writes":5,"write_limit":1073741824,"write_limit_seconds":60,"parent_dbg_info":{"instance":"45.bm-api.prod.nym2","slave_hit":true,"db":"10.3.81.15",
16
    // KO RESPONSE {"response":{"error_id":"SYNTAX","error":"Invalid path \/segment - member is required","error_description":null,"error_code":null,"service":"segment","method":"POST","dbg":{"instance":"40.api.prod.ams1","slave_hit":false,"db":"master","user::reads":0,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":1,"user::write_limit":60,"user::write_limit_seconds":60,"reads":0,"read_limit":1073741824,"read_limit_seconds":60,"writes":1,"write_limit":1073741824,"write_limit_seconds":60,"parent_dbg_info":{"instance":"44.bm-api.prod.nym2","slave_hit":false,"db":"master","user::reads":0,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":1,"user::write_limit":60,"user::write_limit_seconds":60,"reads":0,"read_limit":1073741824,"read_limit_seconds":60,"writes":1,"write_limit":1073741824,"write_limit_seconds":60,"time":48.772096633911,"version":"1.16.497","warnings":[],"slave_lag":0,"start_microtime":1458732144.0725},"time":278.25713157654,"version":"1.16.497","warnings":[],"slave_lag":0,"start_microtime":1458732143.8902}}}
0 ignored issues
show
Unused Code Comprehensibility introduced by
98% 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...
17
    // OK RESPONSE {"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"id":25069653,"batch_segment_upload_job":{"id":25069653,"job_id":"PkIyNufLvcuVMdZ37CqXoonp3KKpjs1459321995","member_id":3847,"last_modified":"2016-03-30 07:13:15","upload_url":"https:\/\/data-api-gslb.adnxs.net\/segment-upload\/PkIyNufLvcuVMdZ37CqXoonp3KKpjs1459321995"},"dbg":{"instance":"40.api.prod.ams1","slave_hit":false,"db":"master","user::reads":0,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":2,"user::write_limit":60,"user::write_limit_seconds":60,"reads":0,"read_limit":1073741824,"read_limit_seconds":60,"writes":2,"write_limit":1073741824,"write_limit_seconds":60,"parent_dbg_info":{"instance":"45.bm-api.prod.nym2","slave_hit":false,"db":"master","user::reads":0,"user::read_limit":100,"user::read_limit_seconds":60,"user::writes":2,"user::write_limit":60,"user::write_limit_seconds":60,"reads":0,"read_limit":1073741824,"read_limit_seconds":60,"writes":2,"write_limit":1073741824,"write_limit_seconds":60,"time":102.07200050354,"version":"1.16.516","warnings":[],"slave_lag":0,"start_microtime":1459321995.0088},"time":323.7988948822,"version":"1.16.516","warnings":[],"slave_lag":1,"start_microtime":1459321994.8377}}}
0 ignored issues
show
Unused Code Comprehensibility introduced by
98% 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...
18
19
    const STATUS_SUCCESS = 'OK';
20
21
    /** @var bool */
22
    protected $successful = true;
23
24
    /** @var  string */
25
    protected $response;
26
27
    /** @var  Error */
28
    protected $error;
29
30
    /**
31
     * @return string
32
     */
33
    public function getResponse($asArray = false)
0 ignored issues
show
Unused Code introduced by
The parameter $asArray is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        return $this->response;
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getResponseAsArray()
42
    {
43
        return json_decode($this->response, true);
44
    }
45
46
    /**
47
     * @param mixed $response
48
     */
49
    public function setResponse($response)
50
    {
51
        $this->response = $response;
52
    }
53
54
    /**
55
     * @return boolean
56
     */
57
    public function isSuccessful()
58
    {
59
        return $this->successful;
60
    }
61
62
    /**
63
     * @param boolean $successful
64
     */
65
    public function setSuccessful($successful)
66
    {
67
        $this->successful = $successful;
68
    }
69
70
    /**
71
     * @return Error
72
     */
73
    public function getError()
74
    {
75
        return $this->error;
76
    }
77
78
    /**
79
     * @param Error $error
80
     */
81
    public function setError(Error $error)
82
    {
83
        $this->error = $error;
84
    }
85
86
87
    /**
88
     * @param Response $response
89
     *
90
     * @return RepositoryResponse
91
     */
92
    public static function fromResponse(Response $response)
93
    {
94
        $self = new self();
95
        $error = new Error();
96
97
        $self->setSuccessful(false);
98
99
        $responseContent = self::getResponseContent($response);
100
        $self->setResponse($responseContent);
101
102
        $responseArray = json_decode($responseContent, true);
103
104
        if (isset($responseArray['response']['status'])) {
105
            $self->setSuccessful($responseArray['response']['status'] == self::STATUS_SUCCESS);
106
        }
107
108
        if (!$self->isSuccessful()) {
109
            $error = Error::fromArray($responseArray['response']);
110
        }
111
112
        $self->setError($error);
113
114
        return $self;
115
116
    }
117
118
    /**
119
     * @param Response $response
120
     *
121
     * @return string
122
     */
123
    private static function getResponseContent(Response $response)
124
    {
125
        $responseContent = $response->getBody()->getContents();
126
        $response->getBody()->rewind();
127
128
        return $responseContent;
129
130
    }
131
}
132