Completed
Push — master ( a15c05...43dee4 )
by Stephen
03:34
created

StarCitizens   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.66%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 158
ccs 52
cts 58
cp 0.8966
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C __call() 0 26 8
A find() 0 15 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)
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 3
    public function __construct()
37
    {
38 3
        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 3
    }
40
41
    /**
42
     * @param $name
43
     * @param $arguments
44
     * @return bool|mixed
45
     * @throws \Exception
46
     */
47 3
    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 3
        if (array_key_exists($name, $this->systems)) {
50 2
            $argumentCount = count($arguments);
51 2
            if ($argumentCount == 0)
52 2
                throw new \InvalidArgumentException("Requires an argument");
53
54 2
            if ($argumentCount > 0 && $argumentCount< 2)
55 2
                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
     * Find an entity
76
     *
77
     * @param $id
78
     * @param $system
79
     * @param $profileType
80
     * @param bool $cache
81
     * @param bool $raw
82
     * @return bool|mixed
83
     */
84 2
    private function find($id, $system, $profileType, $cache = false, $raw = false)
85
    {
86 2
        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...
87 2
        $response = json_decode(
88
            self::$client
89 2
                ->getResult(
90 2
                    $this->getParams($id, $system, $profileType, $cache)
91 2
                )
92 2
                ->getBody()
93 2
                ->getContents(),
94
            true
95 2
        );
96
97 2
        return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw);
98
    }
99
100
    /**
101
     * @param $id
102
     * @param $system
103
     * @param $profileType
104
     * @param $cache
105
     * @return array
106
     */
107 2
    private function getParams($id, $system, $profileType, $cache)
108
    {
109 2
        $cache = ($cache === true) ? "cache" : "live";
110
111
        return [
112 2
            'api_source' => $cache,
113 2
            'system' => $system,
114 2
            'action' => $profileType,
115 2
            'target_id' => $id,
116 2
            'expedite' => '0',
117 2
            'format' => 'json',
118 2
            'start_page' => '1',
119
            'end_page' => '5'
120 2
        ];
121
    }
122
123
    /**
124
     * @param $response
125
     * @param $profileType
126
     * @param $raw
127
     *
128
     * @return bool|mixed
129
     */
130 2
    private function checkResponse($response, $profileType, $raw)
131
    {
132 2
        if ($response['request_stats']['query_status'] == "success") {
133 2
            if ($raw === true) {
134
                return $response;
135
            } else {
136 2
                return $this->fillModel($profileType, $response['data']);
137
            }
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * Setup the client
145
     */
146 3
    private static function setupClient()
147
    {
148 3
        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...
149 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...
150 1
        }
151 3
    }
152
153
    /**
154
     * Fills our model in with the provided data
155
     *
156
     * @param $model
157
     * @param $fillData
158
     *
159
     * @return Model
160
     */
161 2
    private function fillModel($model, $fillData)
162
    {
163 2
        if (is_array($model)) {
164 1
            list($className, $dataRoot, $idName) =$model;
165 1
            $object = new \ReflectionClass('StarCitizen\Models\Store');
166 1
            return $object->newInstance($fillData, $className, $dataRoot, $idName);
167
        } else {
168 1
            $object = new \ReflectionClass('StarCitizen\Models' . $model);
169 1
            return $object->newInstance($fillData);
170
        }
171
    }
172
}