Completed
Push — master ( 1268a0...fb80c8 )
by Stephen
03:23
created

StarCitizens::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
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 organisations($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
    ];
32
33
    /**
34
     * StarCitizens constructor.
35
     */
36 4
    public function __construct()
37
    {
38 4
        static::setupClient();
0 ignored issues
show
Comprehensibility introduced by
Since StarCitizen\StarCitizens is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
39 4
    }
40
41
    /**
42
     * @param $name
43
     * @param $arguments
44
     * @return bool|mixed
45
     * @throws \Exception
46
     */
47 4
    public function __call($name, $arguments)
48
    {
49 4
        if (array_key_exists($name, $this->systems)) {
50 3
            $this->doCall($name, $arguments);
51
        }
52
53 4
        throw new \Exception("Method not found");
54
    }
55
56
    /**
57
     * @param $name
58
     * @param array $arguments
59
     * @return bool|mixed
60
     */
61 3
    private function doCall($name, array $arguments)
62
    {
63 3
        $argumentCount = count($arguments);
64
65 3
        if ($argumentCount == 0)
66
            throw new \InvalidArgumentException("Requires an argument");
67
68 3
        if ($argumentCount > 0 && $argumentCount< 2)
69 2
            return $this->find($arguments[0], $name, $this->systems[$name]['base_action']);
70
71 1
        if ($argumentCount == 2) {
72 1
            list($id, $profileType) = $arguments;
73 1
            if ($profileType == false)
74
                $profileType = $this->systems[$name]['base_action'];
75 1
            return $this->find($id, $name, $profileType);
76
        }
77
78
        if ($argumentCount == 4 ) {
79
            list($id, $profileType, $cache, $raw) = $arguments;
80
            return $this->find($id, $name, $profileType, $cache, $raw);
81
        }
82
83
        throw new \InvalidArgumentException("Invalid arguments");
84
85
    }
86
87
    /**
88
     * @param $name
89
     * @param $arguments
90
     * @return bool|mixed
91
     * @throws \Exception
92
     */
93 1
    public static function __callStatic($name, $arguments)
94
    {
95 1
        $starCitizens = new StarCitizens();
96 1
        return $starCitizens->__call($name, $arguments);
97
    }
98
99
100
    /**
101
     * Find an entity
102
     *
103
     * @param $id
104
     * @param $system
105
     * @param $profileType
106
     * @param bool $cache
107
     * @param bool $raw
108
     * @return bool|mixed
109
     */
110 3
    private function find($id, $system, $profileType, $cache = false, $raw = false)
111
    {
112 3
        var_dump($this->getParams($id, $system, $profileType, $cache));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->getParam...$profileType, $cache)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
113 3
        $response = json_decode(
114 3
            self::$client
115 3
                ->getResult(
116 3
                    $this->getParams($id, $system, $profileType, $cache)
117
                )
118 3
                ->getBody()
119 3
                ->getContents(),
120 3
            true
121
        );
122
123 3
        return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw);
124
    }
125
126
    /**
127
     * @param $id
128
     * @param $system
129
     * @param $profileType
130
     * @param $cache
131
     * @return array
132
     */
133 3
    private function getParams($id, $system, $profileType, $cache)
134
    {
135 3
        $cache = ($cache === true) ? "cache" : "live";
136
137
        return [
138 3
            'api_source' => $cache,
139 3
            'system' => $system,
140 3
            'action' => $profileType,
141 3
            'target_id' => $id,
142 3
            'expedite' => '0',
143 3
            'format' => 'json',
144 3
            'start_page' => '1',
145 3
            'end_page' => '5'
146
        ];
147
    }
148
149
    /**
150
     * @param $response
151
     * @param $profileType
152
     * @param $raw
153
     *
154
     * @return bool|mixed
155
     */
156 3
    private function checkResponse($response, $profileType, $raw)
157
    {
158 3
        if ($response['request_stats']['query_status'] == "success") {
159 3
            if ($raw === true) {
160
                return $response;
161
            } else {
162 3
                return $this->fillModel($profileType, $response['data']);
163
            }
164
        }
165
166
        return false;
167
    }
168
169
    /**
170
     * Setup the client
171
     */
172 4
    private static function setupClient()
173
    {
174 4
        if (static::$client === false) {
0 ignored issues
show
Comprehensibility introduced by
Since StarCitizen\StarCitizens is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
175 1
            static::$client = new StarCitizensClient();
0 ignored issues
show
Comprehensibility introduced by
Since StarCitizen\StarCitizens is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
176
        }
177 4
    }
178
179
    /**
180
     * Fills our model in with the provided data
181
     *
182
     * @param $model
183
     * @param $fillData
184
     *
185
     * @return Model
186
     */
187 3
    private function fillModel($model, $fillData)
188
    {
189 3
        if (is_array($model)) {
190 1
            list($className, $dataRoot, $idName) =$model;
191 1
            $object = new \ReflectionClass('StarCitizen\Models\Store');
192 1
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
193
        } else {
194 2
            $object = new \ReflectionClass('StarCitizen\Models' . $model);
195 2
            return $object->newInstance($fillData);
196
        }
197
    }
198
}