Completed
Push — master ( 098e87...46dc65 )
by Stephen
03:21
created

StarCitizens::__callStatic()   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

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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
19
     */
20
    private static $client = false;
21
22
    private $systems = [
23
        "accounts" => [
24
            "base_action" => "full_profile",
25
            "actions" => [
26
                "full_profile" => '\Profile',
27
                "threads" => ['\Thread', '', 'thread_id'],
28
                "posts" => ['\Post', 'post', 'post_id'],
29
            ]
30
        ],
31
        "organizations" => [
32
            "base_action" => "single_organization",
33
            "actions" => [
34
                "single_organization" => '\Organisation',
35
                "organization_members" => ['\OrgMember', '', 'handle']
36
            ]
37
        ],
38
    ];
39
40
    /**
41
     * StarCitizens constructor.
42
     */
43 20
    public function __construct()
44
    {
45 20
        self::setupClient();
46 20
    }
47
48
    /**
49
     * @param $name
50
     * @param $arguments
51
     * @return bool|mixed
52
     * @throws \Exception
53
     */
54 20
    public function __call($name, $arguments)
55
    {
56 20
        if (array_key_exists($name, $this->systems)) {
57 19
            return $this->doCall($name, $arguments);
58
        }
59
60 1
        throw new \Exception("Method {$name} not found");
61
    }
62
63
    /**
64
     * @param $system
65
     * @param array $arguments
66
     * @return bool|mixed
67
     */
68 19
    private function doCall($system, array $arguments = [])
69
    {
70 19
        $id = '';
71 19
        $cache = false;
72 19
        $raw = false;
73 19
        $action = "";
74 19
        extract($this->standardFindArguments($arguments), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->standardFindArguments($arguments) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
75 19
        $action = ($action == "") ? $this->systems[$system]['base_action'] : $action;
76
77 19
        return $this->find($id, $system, $action, $cache, $raw);
78
    }
79
80
    /**
81
     * @param array $arguments
82
     * @return array
83
     */
84 19
    private function standardFindArguments(array $arguments)
85
    {
86
        $varNames = [
87 19
            'id',
88
            'action',
89
            'cache',
90
            'raw',
91
        ];
92
93
        $defaults = [
94 19
            'id' => '',
95
            'action' => '',
96
            'cache' => false,
97
            'raw' => false,
98
        ];
99
100 19
        for ($argumentCount = 0; $argumentCount < 4; $argumentCount++) {
101 19
            if (array_key_exists($argumentCount, $arguments)) {
102 19
                $defaults[$varNames[$argumentCount]] = $arguments[$argumentCount];
103
            }
104
        }
105
106 19
        return $defaults;
107
    }
108
109
    /**
110
     * @param $name
111
     * @param $arguments
112
     * @return bool|mixed
113
     * @throws \Exception
114
     */
115 1
    public static function __callStatic($name, $arguments)
116
    {
117 1
        $starCitizens = new StarCitizens();
118 1
        return $starCitizens->__call($name, $arguments);
119
    }
120
121
122
    /**
123
     * Find an entity
124
     *
125
     * @param $id
126
     * @param $system
127
     * @param $profileType
128
     * @param bool $cache
129
     * @param bool $raw
130
     * @return bool|mixed
131
     */
132 19
    private function find($id, $system, $profileType, $cache = false, $raw = false)
133
    {
134 19
        $response = json_decode(
135 19
            self::$client
136 19
                ->getResult(
137 19
                    $this->getParams($id, $system, $profileType, $cache)
138
                )
139 19
                ->getBody()
140 19
                ->getContents(),
141 19
            true
142
        );
143
144 19
        return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw);
145
    }
146
147
    /**
148
     * @param $id
149
     * @param $system
150
     * @param $profileType
151
     * @param $cache
152
     * @return array
153
     */
154 19
    private function getParams($id, $system, $profileType, $cache)
155
    {
156 19
        $cache = ($cache === true) ? "cache" : "live";
157
158
        return [
159 19
            'api_source' => $cache,
160 19
            'system' => $system,
161 19
            'action' => $profileType,
162 19
            'target_id' => $id,
163 19
            'expedite' => '0',
164 19
            'format' => 'json',
165 19
            'start_page' => '1',
166 19
            'end_page' => '5'
167
        ];
168
    }
169
170
    /**
171
     * @param $response
172
     * @param $profileType
173
     * @param $raw
174
     *
175
     * @return bool|mixed
176
     */
177 19
    private function checkResponse($response, $profileType, $raw)
178
    {
179 19
        if ($response['request_stats']['query_status'] == "success") {
180 19
            if ($raw === true) {
181 1
                return $response;
182
            } else {
183 19
                return $this->fillModel($profileType, $response['data']);
184
            }
185
        }
186
187 3
        return false;
188
    }
189
190
    /**
191
     * Setup the client
192
     */
193 20
    private static function setupClient()
194
    {
195 20
        if (self::$client === false) {
196 1
            self::$client = new StarCitizensClient();
197
        }
198 20
    }
199
200
    /**
201
     * Fills our model in with the provided data
202
     *
203
     * @param $model
204
     * @param $fillData
205
     *
206
     * @return Model
207
     */
208 19
    private function fillModel($model, $fillData)
209
    {
210 19
        if (is_array($model)) {
211 15
            list($className, $dataRoot, $idName) =$model;
212 15
            $object = new \ReflectionClass('StarCitizen\Models\Store');
213 15
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
214
        } else {
215 8
            $object = new \ReflectionClass('StarCitizen\Models' . $model);
216 8
            return $object->newInstance($fillData);
217
        }
218
    }
219
}