1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AdobeClient\Repository; |
4
|
|
|
|
5
|
|
|
use Audiens\AdobeClient\Entity\Error; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class TraitRepository |
10
|
|
|
*/ |
11
|
|
|
class RepositoryResponse |
12
|
|
|
{ |
13
|
|
|
const STATUS_SUCCESS = 'OK'; |
14
|
|
|
|
15
|
|
|
/** @var bool */ |
16
|
|
|
protected $successful = false; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $response; |
20
|
|
|
|
21
|
|
|
/** @var Error */ |
22
|
|
|
protected $error; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function getResponse() |
28
|
|
|
{ |
29
|
|
|
return $this->response; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getResponseAsArray() |
36
|
|
|
{ |
37
|
|
|
return json_decode($this->response, true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $response |
42
|
|
|
*/ |
43
|
|
|
public function setResponse($response) |
44
|
|
|
{ |
45
|
|
|
$this->response = $response; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
|
|
public function isSuccessful() |
52
|
|
|
{ |
53
|
|
|
return $this->successful; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param boolean $successful |
58
|
|
|
*/ |
59
|
|
|
public function setSuccessful($successful) |
60
|
|
|
{ |
61
|
|
|
$this->successful = $successful; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Error |
66
|
|
|
*/ |
67
|
|
|
public function getError() |
68
|
|
|
{ |
69
|
|
|
return $this->error; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Error $error |
74
|
|
|
*/ |
75
|
|
|
public function setError(Error $error) |
76
|
|
|
{ |
77
|
|
|
$this->error = $error; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Response $response |
83
|
|
|
* |
84
|
|
|
* @return RepositoryResponse |
85
|
|
|
*/ |
86
|
|
|
public static function fromResponse(Response $response) |
87
|
|
|
{ |
88
|
|
|
$self = new self(); |
89
|
|
|
$error = new Error(); |
90
|
|
|
|
91
|
|
|
$self->setSuccessful(false); |
92
|
|
|
|
93
|
|
|
$responseContent = self::getResponseContent($response); |
94
|
|
|
$self->setResponse($responseContent); |
95
|
|
|
|
96
|
|
|
$responseArray = json_decode($responseContent, true); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
if (!isset($responseArray['error'])) { |
100
|
|
|
$self->setSuccessful(true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!$self->isSuccessful()) { |
104
|
|
|
$error = Error::fromArray($responseArray); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$self->setError($error); |
108
|
|
|
|
109
|
|
|
return $self; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Response $response |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private static function getResponseContent(Response $response) |
118
|
|
|
{ |
119
|
|
|
$responseContent = $response->getBody()->getContents(); |
120
|
|
|
|
121
|
|
|
$response->getBody()->rewind(); |
122
|
|
|
|
123
|
|
|
return $responseContent; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|