Completed
Push — master ( fb80c8...0946ea )
by Stephen
04:26 queued 02:04
created

StarCitizens   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.48%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 21
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 183
ccs 57
cts 63
cp 0.9048
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __callStatic() 0 5 1
A __call() 0 8 2
C doCall() 0 25 7
A find() 0 14 1
A getParams() 0 15 2
A checkResponse() 0 12 3
A setupClient() 0 6 2
A fillModel() 0 11 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 3
        }
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 3
            throw new \InvalidArgumentException("Requires an argument");
67
68 3
        if ($argumentCount > 0 && $argumentCount< 2)
69 3
            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 1
                $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
        $response = json_decode(
113
            self::$client
114 3
                ->getResult(
115 3
                    $this->getParams($id, $system, $profileType, $cache)
116 3
                )
117 3
                ->getBody()
118 3
                ->getContents(),
119
            true
120 3
        );
121
122 3
        return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw);
123
    }
124
125
    /**
126
     * @param $id
127
     * @param $system
128
     * @param $profileType
129
     * @param $cache
130
     * @return array
131
     */
132 3
    private function getParams($id, $system, $profileType, $cache)
133
    {
134 3
        $cache = ($cache === true) ? "cache" : "live";
135
136
        return [
137 3
            'api_source' => $cache,
138 3
            'system' => $system,
139 3
            'action' => $profileType,
140 3
            'target_id' => $id,
141 3
            'expedite' => '0',
142 3
            'format' => 'json',
143 3
            'start_page' => '1',
144
            'end_page' => '5'
145 3
        ];
146
    }
147
148
    /**
149
     * @param $response
150
     * @param $profileType
151
     * @param $raw
152
     *
153
     * @return bool|mixed
154
     */
155 3
    private function checkResponse($response, $profileType, $raw)
156
    {
157 3
        if ($response['request_stats']['query_status'] == "success") {
158 3
            if ($raw === true) {
159
                return $response;
160
            } else {
161 3
                return $this->fillModel($profileType, $response['data']);
162
            }
163
        }
164
165
        return false;
166
    }
167
168
    /**
169
     * Setup the client
170
     */
171 4
    private static function setupClient()
172
    {
173 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...
174 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...
175 1
        }
176 4
    }
177
178
    /**
179
     * Fills our model in with the provided data
180
     *
181
     * @param $model
182
     * @param $fillData
183
     *
184
     * @return Model
185
     */
186 3
    private function fillModel($model, $fillData)
187
    {
188 3
        if (is_array($model)) {
189 1
            list($className, $dataRoot, $idName) =$model;
190 1
            $object = new \ReflectionClass('StarCitizen\Models\Store');
191 1
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
192
        } else {
193 2
            $object = new \ReflectionClass('StarCitizen\Models' . $model);
194 2
            return $object->newInstance($fillData);
195
        }
196
    }
197
}