Completed
Push — master ( 2e57bf...fd9de3 )
by Stephen
02:19
created

StarCitizenAbstract::fillModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
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 12
    protected static function find($id, $profileType, $cache = false, $raw = false)
35
    {
36 12
        self::setupClient();
37
38 12
        $response = json_decode(
39
            self::$client
40 12
                ->getResult(
41 12
                    self::getParams($id, $profileType, $cache)
42 12
                )
43 12
                ->getBody()
44 12
                ->getContents(),
45
            true
46 12
        );
47
48 12
        return self::checkResponse($response, $profileType, $raw);
49
    }
50
51
    /**
52
     * @param $id
53
     * @param $profileType
54
     * @param $cache
55
     *
56
     * @return array
57
     */
58 12
    private static function getParams($id, $profileType, $cache)
59
    {
60 12
        $cache = ($cache === true)? "cache" : "live";
61
62
        return [
63 12
            'api_source' => $cache,
64 12
            'system' => static::$system,
65 12
            'action' => $profileType,
66 12
            'target_id' => $id,
67 12
            'expedite' => '0',
68
            'format' => 'json'
69 12
        ];
70
    }
71
72
    /**
73
     * @param $response
74
     * @param $profileType
75
     * @param $raw
76
     *
77
     * @return bool|mixed
78
     */
79 12
    private static function checkResponse($response, $profileType, $raw)
80
    {
81 12
        if ($response['request_stats']['query_status'] == "success")
82 12
            if ($raw === true)
83 12
                return $response;
84
            else
85 12
                return self::fillModel($profileType, $response['data']);
86
87 2
        return false;
88
    }
89
90
    /**
91
     * Setup the client, this is kind of singleton and anti-patterny but it will work nicely
92
     */
93 12
    private static function setupClient()
94
    {
95 12
        if (static::$client === false)
96 12
            static::$client = new StarCitizensClient();
97 12
    }
98
99
    /**
100
     * Fills our model in with the provided data
101
     *
102
     * @param $modelType
103
     * @param $fillData
104
     *
105
     * @return mixed
106
     */
107 12
    public static function fillModel($modelType, $fillData)
108
    {
109 12
        $object = new \ReflectionClass('StarCitizen\Models' . static::MODELS[$modelType]);
110 12
        return $object->newInstance($fillData);
111
    }
112
}