Completed
Push — master ( 9ae690...ba1bd4 )
by Stephen
02:32
created

StarCitizenAbstract::find()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.7972
cc 4
eloc 17
nc 6
nop 4
1
<?php
2
3
namespace StarCitizen\Base;
4
5
use StarCitizen\Client\StarCitizensClient;
6
7
/**
8
 * Class ClientAwareTrait
9
 *
10
 * @package StarCitizen\Contracts
11
 */
12
abstract class StarCitizenAbstract
13
{
14
    /**
15
     * @var bool|StarCitizensClient
16
     */
17
    protected static $client = false;
18
19
    protected static $system;
20
21
    const MODELS = [];
22
    const BASEPROFILE = '';
23
24
    /**
25
     * Find an account information
26
     *
27
     * @param $id
28
     * @param string $profileType
29
     * @param bool $cache
30
     * @param bool $raw
31
     *
32
     * @return bool|mixed
33
     */
34
    protected static function find($id, $profileType, $cache = false, $raw = false)
35
    {
36
        self::setupClient();
37
        $cache = ($cache === true)? "cache" : "live";
38
39
        $params = [
40
            'api_source' => $cache,
41
            'system' => static::$system,
42
            'action' => $profileType,
43
            'target_id' => $id,
44
            'expedite' => '0',
45
            'format' => 'json'
46
        ];
47
48
        $response = json_decode(self::$client->getResult($params)->getBody()->getContents(), true);
49
        if ($response['request_stats']['query_status'] == "success")
50
            if ($raw === true)
51
                return $response;
52
            else
53
                return self::fillModel($profileType, $response['data']);
54
55
        return false;
56
    }
57
58
    /**
59
     * Setup the client, this is kind of singleton and anti-patterny but it will work nicely
60
     */
61
    private static function setupClient()
62
    {
63
        if (static::$client === false)
64
            static::$client = new StarCitizensClient();
65
    }
66
67
    /**
68
     * Fills our model in with the provided data
69
     *
70
     * @param $modelType
71
     * @param $fillData
72
     *
73
     * @return mixed
74
     */
75
    public static function fillModel($modelType, $fillData)
76
    {
77
        $object = new \ReflectionClass('StarCitizen\Models' . static::MODELS[$modelType]);
78
        return $object->newInstance($fillData);
79
    }
80
}