Client::getConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Atog\Api;
2
3
use Atog\Api\Exceptions\InvalidEndpointException;
4
use Jyggen\Curl\Request;
5
6
/**
7
 * Abstract Client
8
 * @package Atog\Api
9
 */
10
abstract class Client
11
{
12
    /**
13
     * Endpoints
14
     * @var string
15
     */
16
    protected $endpoints = [];
17
    
18
    /**
19
     * @var string
20
     */
21
    protected $domain;
22
    
23
    /**
24
     * @var array
25
     */
26
    protected $config;
27
28
    /**
29
     * A array containing the cached endpoints
30
     * @var array
31
     */
32
    private $cachedEndpoints = [];
33
    
34
    /**
35
     * Create a new client instance.
36
     * @param array $endpoints
37
     * @param array $config
38
     */
39 12
    public function __construct(array $endpoints, array $config = [])
40
    {
41 12
        $this->setEndpoints($endpoints);
42 12
        $this->config = $config;
43 12
    }
44
45
    /**
46
     * @param array $endpoints
47
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
48
     */
49 12
    protected function setEndpoints(array $endpoints)
50
    {
51 12
        foreach ($endpoints as $key => $endpoint) {
52
            // check if class exists
53 12
            if (!class_exists($endpoint)) {
54 1
                throw new InvalidEndpointException("Class {$endpoint} does not exists");
55
            }
56
57
            // get key
58 12
            if (!is_string($key)) {
59 12
                $parts = explode('\\', $endpoint);
60 12
                $key = end($parts);
61 12
            }
62
63
            // save
64 12
            $this->endpoints[studly_case($key)] = $endpoint;
65 12
        }
66 12
    }
67
68
    /**
69
     * @return array
70
     */
71 1
    public function getConfig()
72
    {
73 1
        return $this->config;
74
    }
75
    
76
    /**
77
     * Make a GET Request to an endpoint
78
     * @param       $endpoint
79
     * @param array $params
80
     * @return \Jyggen\Curl\Response
81
     * @throws \Jyggen\Curl\Exception\CurlErrorException
82
     * @throws \Jyggen\Curl\Exception\ProtectedOptionException
83
     */
84 2
    public function get($endpoint, $params = [])
85 1
    {
86
        // build url
87 2
        $url = $this->domain . '/' . $endpoint;
88
89 2
        if (!empty($params)) {
90 1
            $url = $url . '?' . http_build_query($params);
91 1
        }
92
93 2
        $request = new Request($url);
94
        
95
        // Add client secret header
96 2
        if (isset($this->config['secret'])) {
97 2
            $request->headers->add(['X-Client-Secret' => $this->config['secret']]);
98 2
        }
99
        
100
        // add curl options
101 2
        if (isset($this->config['curl']) && is_array($this->config['curl'])) {
102 2
            foreach ($this->config['curl'] as $option => $value) {
103 2
                $request->setOption($option, $value);
104 2
            }
105 2
        }
106
        
107 2
        $request->execute();
108
109 2
        return $request->getResponse();
110
    }
111
    
112
    /**
113
     * Get an API endpoint
114
     * @param string $endpoint
115
     * @return \Atog\Api\Endpoint
116
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
117
     */
118 4
    public function __get($endpoint)
119
    {
120 4
        return $this->getEndpoint($endpoint);
121
    }
122
    
123
    /**
124
     * Get an API endpoint.
125
     * @param string $endpoint
126
     * @return \Atog\Api\Endpoint
127
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
128
     */
129 8
    public function getEndpoint($endpoint)
130
    {
131
        // Get Endpoint Class name
132 8
        $endpoint = studly_case($endpoint);
133
134 8
        if (!array_key_exists($endpoint, $this->endpoints)) {
135 2
            throw new InvalidEndpointException("Endpoint {$endpoint} does not exists");
136
        }
137
138 6
        $class = $this->endpoints[$endpoint];
139
140
        // Check if an instance has already been initiated
141 6
        if (isset($this->cachedEndpoints[$endpoint]) === false) {
142
            // check if class is an EndPoint
143 6
            $endpointClass = new \ReflectionClass($class);
144 6
            if (!$endpointClass->isSubclassOf('Atog\Api\Endpoint')) {
145 2
                throw new InvalidEndpointException("Class {$class} does not extend Atog\\Api\\Endpoint");
146
            }
147
            
148
            // check for model
149 4
            $model = new Model();
150 4
            if (array_key_exists('models', $this->config) && array_key_exists($endpoint, $this->config['models'])) {
151 4
                $modelClass = $this->config['models'][$endpoint];
152 4
                if (class_exists($modelClass)) {
153 4
                    $model = new $modelClass();
154 4
                }
155 5
            }
156 4
            $this->cachedEndpoints[$endpoint] = new $class($this, $model);
157 4
        }
158
        
159 4
        return $this->cachedEndpoints[$endpoint];
160
    }
161
}
162