1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Trucker\Responses; |
4
|
|
|
|
5
|
|
|
use Guzzle\Http\Message\Response as GuzzleResponse; |
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Trucker\Facades\TransporterFactory; |
8
|
|
|
|
9
|
|
|
class Response extends BaseResponse |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The IoC Container. |
13
|
|
|
* |
14
|
|
|
* @var Container |
15
|
|
|
*/ |
16
|
|
|
protected $app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Response object managed by this |
20
|
|
|
* class. |
21
|
|
|
* |
22
|
|
|
* @var GuzzleResponse |
23
|
|
|
*/ |
24
|
|
|
protected $response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Response constructor. |
28
|
|
|
* |
29
|
|
|
* @param Container $app |
30
|
|
|
* @param GuzzleResponse|null $response |
31
|
|
|
*/ |
32
|
32 |
|
public function __construct(Container $app, GuzzleResponse $response = null) |
33
|
|
|
{ |
34
|
32 |
|
$this->app = $app; |
35
|
|
|
|
36
|
32 |
|
parent::__construct($response); |
37
|
32 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Getter to access the IoC Container. |
41
|
|
|
* |
42
|
|
|
* @return Container |
43
|
|
|
*/ |
44
|
1 |
|
public function getApp() |
45
|
|
|
{ |
46
|
1 |
|
return $this->app; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Magic function to pass methods not found |
51
|
|
|
* on this class down to the guzzle response |
52
|
|
|
* object that is being wrapped. |
53
|
|
|
* |
54
|
|
|
* @param string $method name of called method |
55
|
|
|
* @param array $args arguments to the method |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
25 |
View Code Duplication |
public function __call($method, $args) |
|
|
|
|
60
|
|
|
{ |
61
|
25 |
|
if (!method_exists($this, $method)) { |
62
|
25 |
|
return call_user_func_array( |
63
|
25 |
|
[$this->response, $method], |
64
|
25 |
|
$args |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
// @codeCoverageIgnoreStart |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// @codeCoverageIgnoreEnd |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Container $app |
74
|
|
|
* @param GuzzleResponse $response |
75
|
|
|
* |
76
|
|
|
* @return Response |
77
|
|
|
*/ |
78
|
25 |
|
public function newInstance(Container $app, GuzzleResponse $response) |
79
|
|
|
{ |
80
|
|
|
// This method just provides a convenient way for us to generate fresh model |
81
|
|
|
// instances of this current model. It is particularly useful during the |
82
|
|
|
// hydration of new objects via the Eloquent query builder instances. |
83
|
25 |
|
return new static($app, $response); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Function to take a response object and convert it |
88
|
|
|
* into an array of data that is ready for use. |
89
|
|
|
* |
90
|
|
|
* @return array Parsed array of data |
91
|
|
|
*/ |
92
|
11 |
|
public function parseResponseToData() |
93
|
|
|
{ |
94
|
11 |
|
$transporter = TransporterFactory::build(); |
|
|
|
|
95
|
|
|
|
96
|
11 |
|
return $transporter->parseResponseToData($this->response); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Function to take a response string (as a string) and depending on |
101
|
|
|
* the type of string it is, parse it into an object. |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
7 |
|
public function parseResponseStringToObject() |
106
|
|
|
{ |
107
|
7 |
|
$transporter = TransporterFactory::build(); |
108
|
|
|
|
109
|
7 |
|
return $transporter->parseResponseStringToObject($this->response); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.