Completed
Push — http_plug ( 0a2d32 )
by Robert
10:27
created

BaseApi   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 13
Bugs 2 Features 2
Metric Value
wmc 14
c 13
b 2
f 2
lcom 1
cbo 2
dl 0
loc 113
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A request() 0 12 3
A transform() 0 4 1
B sanitizeQuery() 0 28 6
A injectPager() 0 17 3
1
<?php
2
3
namespace Rs\VersionEye\Api;
4
5
use Rs\VersionEye\Http\HttpClient;
6
use Rs\VersionEye\Http\Pager;
7
8
/**
9
 * BaseApi.
10
 *
11
 * @author Robert Schönthal <[email protected]>
12
 */
13
abstract class BaseApi
14
{
15
    /**
16
     * @var HttpClient
17
     */
18
    protected $client;
19
20
    /**
21
     * @param HttpClient $client
22
     */
23
    public function __construct(HttpClient $client)
24
    {
25
        $this->client = $client;
26
    }
27
28
    /**
29
     * performs the request.
30
     *
31
     * @param string $url
32
     * @param string $method
33
     * @param array  $params
34
     *
35
     * @return array
36
     */
37
    protected function request($url, $method = 'GET', array $params = [])
38
    {
39
        $url = $this->sanitizeQuery($url);
40
41
        $response = $this->client->request($method, $url, $params);
42
43
        if (is_array($response) && isset($response['paging'])) {
44
            $response = $this->injectPager($response, $method, $url, $params);
45
        }
46
47
        return $response;
48
    }
49
50
    /**
51
     * converts names to the needed url path format.
52
     *
53
     * @param string $name
54
     *
55
     * @return string
56
     */
57
    protected function transform($name)
58
    {
59
        return str_replace(['/', '.'], [':', '~'], $name);
60
    }
61
62
    /**
63
     * removes empty query string parameters.
64
     *
65
     * @param string $query
66
     *
67
     * @return string
68
     */
69
    private function sanitizeQuery($query)
70
    {
71
        $parts = parse_url($query);
72
        $path  = $parts['path'];
73
74
        if (!isset($parts['query'])) {
75
            return $query;
76
        }
77
78
        $vars = explode('&', $parts['query']);
79
80
        $final = [];
81
82
        if (!empty($vars)) {
83
            foreach ($vars as $var) {
84
                $parts = explode('=', $var);
85
86
                $key = $parts[0];
87
                $val = $parts[1];
88
89
                if (!array_key_exists($key, $final) && !empty($val)) {
90
                    $final[$key] = $val;
91
                }
92
            }
93
        }
94
95
        return $path . '?' . http_build_query($final);
96
    }
97
98
    /**
99
     * converts the pageable data into a real pager.
100
     *
101
     * @param array  $response
102
     * @param string $method
103
     * @param string $url
104
     * @param array  $params
105
     *
106
     * @return array
107
     */
108
    private function injectPager(array $response, $method, $url, array $params = [])
109
    {
110
        while (next($response)) {
111
            if ('paging' === key($response)) {
112
                prev($response);
113
                break;
114
            }
115
        }
116
117
        $pageableKey = key($response);
118
119
        $response[$pageableKey] = new Pager($response, $pageableKey, $this->client, $method, $url, $params);
120
121
        reset($response);
122
123
        return $response;
124
    }
125
}
126