StarCitizens   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 0 Features 4
Metric Value
wmc 20
c 14
b 0
f 4
lcom 1
cbo 1
dl 0
loc 219
ccs 67
cts 67
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A readConfig() 0 4 1
A get() 0 14 1
A getCallParams() 0 15 2
A checkResponse() 0 12 3
A fillModel() 0 11 2
A __call() 0 8 2
A doCall() 0 12 2
A __callStatic() 0 5 1
A setupClient() 0 6 2
A standardGetArguments() 0 20 3
1
<?php
2
3
namespace StarCitizen;
4
5
use StarCitizen\Models\Model;
6
use StarCitizen\Client\StarCitizensClient;
7
8
/**
9
 * Class StarCitizens
10
 * @package StarCitizen
11
 *
12
 * @method accounts($id, $profileType = false, $cache = false, $raw = false) Get accounts system results
13
 * @method organizations($id, $profileType = false, $cache = false, $raw = false) Get org system results
14
 */
15
final class StarCitizens
16
{
17
    /**
18
     * @var bool|StarCitizensClient The client object
19
     */
20
    private static $client = false;
21
22
    /**
23
     * @var array  The config for the StarCitizens todo move this to a separate file
24
     */
25
    private $systems = [];
26
27
    /**
28
     * @var string
29
     */
30
    public $configFile;
31
32
    /**
33
     * StarCitizens constructor.
34
     *
35
     * @param string $configFile
36
     */
37 20
    public function __construct($configFile = "Resources/config/typesConfig.php")
38
    {
39 20
        $this->configFile = $configFile;
40 20
        self::setupClient();
41 20
        $this->readConfig();
42
        
43 20
    }
44
45
    /**
46
     * Reads the basic config.
47
     */
48 20
    private function readConfig()
49
    {
50 20
        $this->systems = include $this->configFile;
51 20
    }
52
53
    /**
54
     * Find an entity based on id and return the correct model or raw json output
55
     *
56
     * @param $id
57
     * @param $system
58
     * @param $profileType
59
     * @param bool $raw
60
     * @param bool $cache
61
     * @return bool|mixed
62
     */
63 19
    private function get($id, $system, $profileType, $raw = false, $cache = false)
64
    {
65 19
        $response = json_decode(
66 19
            self::$client
67 19
                ->getResult(
68 19
                    $this->getCallParams($id, $system, $profileType, $cache)
69
                )
70 19
                ->getBody()
71 19
                ->getContents(),
72 19
            true
73
        );
74
75 19
        return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw);
76
    }
77
78
    /**
79
     * Returns the params needed for an API call
80
     *
81
     * @param $id
82
     * @param $system
83
     * @param $profileType
84
     * @param $cache
85
     * @return array
86
     */
87 19
    private function getCallParams($id, $system, $profileType, $cache)
88
    {
89 19
        $cache = ($cache === true) ? "cache" : "live";
90
91
        return [
92 19
            'api_source' => $cache,
93 19
            'system' => $system,
94 19
            'action' => $profileType,
95 19
            'target_id' => $id,
96 19
            'expedite' => '0',
97 19
            'format' => 'json',
98 19
            'start_page' => '1',
99 19
            'end_page' => '5'
100
        ];
101
    }
102
103
    /**
104
     * Checks the response for success messages and returns the raw response or model
105
     *
106
     * @param $response
107
     * @param $profileType
108
     * @param $raw
109
     *
110
     * @return bool|mixed
111
     */
112 19
    private function checkResponse($response, $profileType, $raw)
113
    {
114 19
        if ($response['request_stats']['query_status'] == "success") {
115 19
            if ($raw === true) {
116 1
                return $response;
117
            } else {
118 19
                return $this->fillModel($profileType, $response['data']);
119
            }
120
        }
121
122 3
        return false;
123
    }
124
125
    /**
126
     * Setup the client
127
     */
128 20
    private static function setupClient()
129
    {
130 20
        if (self::$client === false) {
131 1
            self::$client = new StarCitizensClient();
132
        }
133 20
    }
134
135
    /**
136
     * Fills our model in with the provided data or sends the data to a store
137
     *
138
     * @param $model
139
     * @param $fillData
140
     *
141
     * @return Model
142
     */
143 19
    private function fillModel($model, $fillData)
144
    {
145 19
        if (is_array($model)) {
146 15
            list($className, $dataRoot, $idName) =$model;
147 15
            $object = new \ReflectionClass('StarCitizen\Models\Store');
148 15
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
149
        } else {
150 8
            $object = new \ReflectionClass('StarCitizen\Models' . $model);
151 8
            return $object->newInstance($fillData);
152
        }
153
    }
154
155
    /**
156
     * Magic call function based on the config information
157
     *
158
     * @param $name
159
     * @param $arguments
160
     * @return bool|mixed
161
     * @throws \Exception
162
     */
163 20
    public function __call($name, $arguments)
164
    {
165 20
        if (array_key_exists($name, $this->systems)) {
166 19
            return $this->doCall($name, $arguments);
167
        }
168
169 1
        throw new \Exception("Method {$name} not found");
170
    }
171
172
    /**
173
     * This is the real call function
174
     *
175
     * @param $system
176
     * @param array $arguments
177
     * @return bool|mixed
178
     */
179 19
    private function doCall($system, array $arguments = [])
180
    {
181 19
        $id = '';
182 19
        $cache = false;
183 19
        $raw = false;
184 19
        $action = "";
185
186 19
        $fixedArguments = $this->standardGetArguments($arguments);
187 19
        extract($fixedArguments, EXTR_OVERWRITE);
188 19
        $action = ($action == "") ? $this->systems[$system]['base_action'] : $action;
189 19
        return $this->get($id, $system, $action, $raw, $cache);
190
    }
191
192
    /**
193
     * Get the standard arguments for a find call and checks that we
194
     * have the correct arguments passed to the magic function
195
     *
196
     * @param array $arguments
197
     * @return array
198
     */
199 19
    private function standardGetArguments(array $arguments)
200
    {
201
202
        $defaults = [
203 19
            'id' => '',
204
            'action' => '',
205
            'raw' => false,
206
            'cache' => false,
207
        ];
208
209 19
        $varNames = array_keys($defaults);
210
211 19
        for ($argumentCount = 0; $argumentCount < 4; $argumentCount++) {
212 19
            if (array_key_exists($argumentCount, $arguments)) {
213 19
                $defaults[$varNames[$argumentCount]] = $arguments[$argumentCount];
214
            }
215
        }
216
        
217 19
        return $defaults;
218
    }
219
220
    /**
221
     * Allows our functions to be called statically, this is a bit hacky tbh.
222
     *
223
     * @param $name
224
     * @param $arguments
225
     * @return bool|mixed
226
     * @throws \Exception
227
     */
228 1
    public static function __callStatic($name, $arguments)
229
    {
230 1
        $starCitizens = new StarCitizens();
231 1
        return $starCitizens->__call($name, $arguments);
232
    }
233
}