Completed
Push — master ( 43dee4...005b3e )
by Stephen
04:01
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 1
Bugs 0 Features 0
Metric Value
c 1
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)
13
 * @method organisations($id, $profileType = false, $cache = false, $raw = false)
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
    function __call($name, $arguments)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49 4
        if (array_key_exists($name, $this->systems)) {
50 3
            $argumentCount = count($arguments);
51 3
            if ($argumentCount == 0)
52 3
                throw new \InvalidArgumentException("Requires an argument");
53
54 3
            if ($argumentCount > 0 && $argumentCount< 2)
55 3
                return $this->find($arguments[0], $name, $this->systems[$name]['base_action']);
56
57 1
            if ($argumentCount == 2) {
58 1
                list($id, $profileType) = $arguments;
59 1
                if ($profileType == false)
60 1
                    $profileType = $this->systems[$name]['base_action'];
61 1
                return $this->find($id, $name, $profileType);
62
            }
63
            if ($argumentCount == 4 ) {
64
                list($id, $profileType, $cache, $raw) = $arguments;
65
                return $this->find($id, $name, $profileType, $cache, $raw);
66
            } else {
67
                throw new \InvalidArgumentException("Invalid arguments");
68
            }
69
        }
70
71 1
        throw new \Exception("Method not found");
72
    }
73
74
    /**
75
     * @param $name
76
     * @param $arguments
77
     * @return bool|mixed
78
     * @throws \Exception
79
     */
80 1
    public static function __callStatic($name, $arguments)
81
    {
82 1
        $starCitizens = new StarCitizens();
83 1
        return $starCitizens->__call($name, $arguments);
84
    }
85
86
87
    /**
88
     * Find an entity
89
     *
90
     * @param $id
91
     * @param $system
92
     * @param $profileType
93
     * @param bool $cache
94
     * @param bool $raw
95
     * @return bool|mixed
96
     */
97 3
    private function find($id, $system, $profileType, $cache = false, $raw = false)
98
    {
99 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...
100 3
        $response = json_decode(
101
            self::$client
102 3
                ->getResult(
103 3
                    $this->getParams($id, $system, $profileType, $cache)
104 3
                )
105 3
                ->getBody()
106 3
                ->getContents(),
107
            true
108 3
        );
109
110 3
        return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw);
111
    }
112
113
    /**
114
     * @param $id
115
     * @param $system
116
     * @param $profileType
117
     * @param $cache
118
     * @return array
119
     */
120 3
    private function getParams($id, $system, $profileType, $cache)
121
    {
122 3
        $cache = ($cache === true) ? "cache" : "live";
123
124
        return [
125 3
            'api_source' => $cache,
126 3
            'system' => $system,
127 3
            'action' => $profileType,
128 3
            'target_id' => $id,
129 3
            'expedite' => '0',
130 3
            'format' => 'json',
131 3
            'start_page' => '1',
132
            'end_page' => '5'
133 3
        ];
134
    }
135
136
    /**
137
     * @param $response
138
     * @param $profileType
139
     * @param $raw
140
     *
141
     * @return bool|mixed
142
     */
143 3
    private function checkResponse($response, $profileType, $raw)
144
    {
145 3
        if ($response['request_stats']['query_status'] == "success") {
146 3
            if ($raw === true) {
147
                return $response;
148
            } else {
149 3
                return $this->fillModel($profileType, $response['data']);
150
            }
151
        }
152
153
        return false;
154
    }
155
156
    /**
157
     * Setup the client
158
     */
159 4
    private static function setupClient()
160
    {
161 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...
162 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...
163 1
        }
164 4
    }
165
166
    /**
167
     * Fills our model in with the provided data
168
     *
169
     * @param $model
170
     * @param $fillData
171
     *
172
     * @return Model
173
     */
174 3
    private function fillModel($model, $fillData)
175
    {
176 3
        if (is_array($model)) {
177 1
            list($className, $dataRoot, $idName) =$model;
178 1
            $object = new \ReflectionClass('StarCitizen\Models\Store');
179 1
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
180
        } else {
181 2
            $object = new \ReflectionClass('StarCitizen\Models' . $model);
182 2
            return $object->newInstance($fillData);
183
        }
184
    }
185
}