Completed
Push — master ( a15c05...43dee4 )
by Stephen
03:34
created

AbstractEntity::checkResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
crap 3
1
<?php
2
3
namespace StarCitizen\Base;
4
5
use StarCitizen\Client\StarCitizensClient;
6
use StarCitizen\Models\Model;
7
8
/**
9
 * Class ClientAwareTrait
10
 *
11
 * @package StarCitizen\Contracts
12
 */
13
abstract class AbstractEntity
14
{
15
    /**
16
     * @var bool|StarCitizensClient
17
     */
18
    protected static $client = false;
19
20
    protected static $system;
21
22
    const MODELS = [];
23
    const BASEPROFILE = '';
24
25
    /**
26
     * Find an account information
27
     *
28
     * @param $id
29
     * @param string $profileType
30
     * @param bool $cache
31
     * @param bool $raw
32
     *
33
     * @return bool|mixed
34
     */
35 15
    protected static function find($id, $profileType, $cache = false, $raw = false)
36
    {
37 15
        self::setupClient();
38
39 15
        $response = json_decode(
40
            self::$client
41 15
                ->getResult(
42 15
                    self::getParams($id, $profileType, $cache)
43 15
                )
44 15
                ->getBody()
45 15
                ->getContents(),
46
            true
47 15
        );
48
49 15
        return self::checkResponse($response, $profileType, $raw);
50
    }
51
52
    /**
53
     * @param $id
54
     * @param $profileType
55
     * @param $cache
56
     *
57
     * @return array
58
     */
59 15
    private static function getParams($id, $profileType, $cache)
60
    {
61 15
        $cache = ($cache === true) ? "cache" : "live";
62
63
        return [
64 15
            'api_source' => $cache,
65 15
            'system' => static::$system,
66 15
            'action' => $profileType,
67 15
            'target_id' => $id,
68 15
            'expedite' => '0',
69 15
            'format' => 'json',
70 15
            'start_page' => '1',
71
            'end_page' => '5'
72 15
        ];
73
    }
74
75
    /**
76
     * @param $response
77
     * @param $profileType
78
     * @param $raw
79
     *
80
     * @return bool|mixed
81
     */
82 15
    private static function checkResponse($response, $profileType, $raw)
83
    {
84 15
        if ($response['request_stats']['query_status'] == "success") {
85 15
            if ($raw === true) {
86 1
                return $response;
87
            } else {
88 15
                return self::fillModel($profileType, $response['data']);
89
            }
90
        }
91
92 3
        return false;
93
    }
94
95
    /**
96
     * Setup the client
97
     */
98 15
    private static function setupClient()
99
    {
100 15
        if (static::$client === false) {
101 1
            static::$client = new StarCitizensClient();
102 1
        }
103 15
    }
104
105
    /**
106
     * Fills our model in with the provided data
107
     *
108
     * @param $modelType
109
     * @param $fillData
110
     *
111
     * @return Model
112
     */
113 15
    public static function fillModel($modelType, $fillData)
114
    {
115 15
        if (is_array(static::MODELS[$modelType])) {
116 13
            list($className, $dataRoot, $idName) = static::MODELS[$modelType];
117 13
            $object = new \ReflectionClass('StarCitizen\Models\Store');
118 13
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
119
        } else {
120 6
            $object = new \ReflectionClass('StarCitizen\Models' . static::MODELS[$modelType]);
121 6
            return $object->newInstance($fillData);
122
        }
123
    }
124
125
}