InstanceFinder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
dl 0
loc 68
ccs 20
cts 22
cp 0.9091
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B fetch() 0 40 4
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $instance is dead and can be removed.
Loading history...
49
50
        //get a request object
51 2
        $request = RequestFactory::build();
0 ignored issues
show
Bug introduced by
The method build() does not exist on Trucker\Facades\RequestFactory. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $request = RequestFactory::build();
Loading history...
52
53
        //init the request
54 2
        $request->createRequest(
55 2
            Config::get('request.base_uri'),
0 ignored issues
show
Bug introduced by
The method get() does not exist on Trucker\Facades\Config. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            Config::/** @scrutinizer ignore-call */ 
56
                    get('request.base_uri'),
Loading history...
56 2
            UrlGenerator::getInstanceUri($model, [':id' => $id]),
0 ignored issues
show
Bug introduced by
The method getInstanceUri() does not exist on Trucker\Facades\UrlGenerator. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            UrlGenerator::/** @scrutinizer ignore-call */ 
57
                          getInstanceUri($model, [':id' => $id]),
Loading history...
57 2
            'GET'
58
        );
59
60
        //add auth if it is needed
61 2
        if ($auth = AuthFactory::build()) {
0 ignored issues
show
Bug introduced by
The method build() does not exist on Trucker\Facades\AuthFactory. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        if ($auth = AuthFactory::/** @scrutinizer ignore-call */ build()) {
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The method build() does not exist on Trucker\Facades\ResponseInterpreterFactory. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        if (!ResponseInterpreterFactory::/** @scrutinizer ignore-call */ build()->success($response)) {
Loading history...
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