|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trucker\Finders; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Trucker\Facades\AuthFactory; |
|
7
|
|
|
use Trucker\Facades\Config; |
|
8
|
|
|
use Trucker\Facades\RequestFactory; |
|
9
|
|
|
use Trucker\Facades\ResponseInterpreterFactory; |
|
10
|
|
|
use Trucker\Facades\UrlGenerator; |
|
11
|
|
|
use Trucker\Resource\Model; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class for finding model instances over the remote API. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Alessandro Manno <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class InstanceFinder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The IoC Container. |
|
22
|
|
|
* |
|
23
|
|
|
* @var Container |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $app; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Build a new InstanceFinder. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Container $app |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function __construct(Container $app) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->app = $app; |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Function to find an instance of an Entity record. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Model $model model to use for URL generation etc |
|
41
|
|
|
* @param int $id The primary identifier value for the record |
|
42
|
|
|
* @param array $getParams Array of GET parameters to pass |
|
43
|
|
|
* |
|
44
|
|
|
* @return Model An instance of the entity requested |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function fetch($model, $id, array $getParams = []) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$instance = null; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
//get a request object |
|
51
|
2 |
|
$request = RequestFactory::build(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
//init the request |
|
54
|
2 |
|
$request->createRequest( |
|
55
|
2 |
|
Config::get('request.base_uri'), |
|
|
|
|
|
|
56
|
2 |
|
UrlGenerator::getInstanceUri($model, [':id' => $id]), |
|
|
|
|
|
|
57
|
2 |
|
'GET' |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
//add auth if it is needed |
|
61
|
2 |
|
if ($auth = AuthFactory::build()) { |
|
|
|
|
|
|
62
|
|
|
$request->authenticate($auth); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
//set any get parameters on the request |
|
66
|
2 |
|
$request->setGetParameters($getParams); |
|
67
|
|
|
|
|
68
|
|
|
//actually send the request |
|
69
|
2 |
|
$response = $request->sendRequest(); |
|
70
|
|
|
|
|
71
|
2 |
|
if (!ResponseInterpreterFactory::build()->success($response)) { |
|
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
//kraft the response into an object to return |
|
76
|
2 |
|
$data = $response->parseResponseToData(); |
|
77
|
2 |
|
$instance = new $model($data); |
|
78
|
|
|
|
|
79
|
|
|
//inflate the ID property that should be guarded |
|
80
|
2 |
|
$id = $instance->getIdentityProperty(); |
|
81
|
2 |
|
if (array_key_exists($id, $data)) { |
|
82
|
2 |
|
$instance->{$id} = $data[$id]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return $instance; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|