APIHandler   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 65
c 1
b 0
f 0
dl 0
loc 118
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 24 6
A checkConnection() 0 20 4
A destroy() 0 13 3
A make() 0 12 2
A save() 0 15 3
A request() 0 12 3
1
<?php
2
3
namespace Ijeffro\Laralocker\LearningLocker\API;
4
5
use Config;
6
use Ijeffro\Laralocker\LearningLocker\Connection;
7
8
class APIHandler extends Connection {
9
10
    /**
11
     * Learning Locker: Requst URL
12
     *
13
     * @return  $response
0 ignored issues
show
Documentation Bug introduced by
The doc comment $response at position 0 could not be parsed: Unknown type name '$response' at position 0 in $response.
Loading history...
14
     */
15
    public function request($url) {
16
        try {
17
            $response = $this->client()->get($url, [
18
                'auth' => $this->auth(),
19
                'headers' => $this->headers()
20
            ]);
21
            if ( $response->getStatusCode() === 404 )
22
                throw new ClientException(404);
0 ignored issues
show
Bug introduced by
The type Ijeffro\Laralocker\Learn...ker\API\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
            return $response->getBody()->getContents();
25
        } catch (ClientException $e) {
26
            return $e;
27
        }
28
    }
29
30
    public function save($url, $data)
31
    {
32
        try {
33
            $data = json_encode($data);
34
            $response = $this->client()->patch($url, [
35
                'auth' => $this->auth(),
36
                'headers' => $this->headers(),
37
                'body' => $data
38
            ]);
39
            if ( $response->getStatusCode() === 404 )
40
                throw new ClientException(404);
41
42
            return $response->getBody()->getContents();
43
        } catch (ClientException $e) {
44
            return $e;
45
        }
46
    }
47
48
    public function destroy($url)
49
    {
50
        try {
51
            $response = $this->client()->delete($url, [
52
                'auth' => $this->auth(),
53
                'headers' => $this->headers()
54
            ]);
55
            if ( $response->getStatusCode() === 404 )
56
                throw new ClientException(404);
57
58
            return $response->getBody()->getContents();
59
        } catch (ClientException $e) {
60
            return $e;
61
        }
62
    }
63
64
    public function make($url, $data = null)
65
    {
66
        try {
67
            $data = json_encode($data);
68
            $response = $this->client()->post($url, [
69
                'auth' => $this->auth(),
70
                'headers' => $this->headers(),
71
                'body' => $data
72
            ]);
73
            return $response->getBody()->getContents();
74
        } catch (ClientException $e) {
75
            return $e;
76
        }
77
    }
78
79
    public function select($selected = [], $response)
80
    {
81
        if (!is_array($selected) || !$response)  {
82
            $error = [ "error" => "Select must be an array."];
83
            return json_encode($error);
84
        }
85
86
        if ($selected) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selected of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
            $response = (array) json_decode($response);
88
            $items = [];
89
90
            foreach($selected as $select) {
91
                $search = array_key_exists($select, $response);
92
93
                if ($search === true) {
94
                    $items[$select] = $response[$select];
95
                }
96
            }
97
98
            $response = json_encode($items);
99
            return $response;
100
        }
101
102
        return $response;
103
104
    }
105
106
    public function checkConnection()
107
    {
108
        try {
109
            $domain = Config::get("laralocker.learning_locker.api.url");
110
            $url = $domain . '/api';
111
            $response = $this->client()->get($url, [
112
                'auth' => $this->auth(),
113
                'headers' => $this->headers()
114
            ]);
115
116
            if ($response->getStatusCode() === 200) {
117
                return true;
118
            }
119
120
            if ( $response->getStatusCode() === 404 )
121
                throw new ClientException(404);
122
123
            return $response->getBody()->getContents();
124
        } catch (ClientException $e) {
125
            return $e;
126
        }
127
    }
128
        // dd($this->headers());
129
        // $response = $client->get('http://httpbin.org/get');
130
        // $response = $client->delete('http://httpbin.org/delete');
131
        // $response = $client->head('http://httpbin.org/get');
132
        // $response = $client->options('http://httpbin.org/get');
133
        // $response = $client->patch('http://httpbin.org/patch');
134
        // $response = $client->post('http://httpbin.org/post');
135
        // $response = $client->put('http://httpbin.org/put');
136
137
}
138