Completed
Push — master ( 0a266f...c03660 )
by Pascal
02:42
created

Client::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
crap 5
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 9
    public function __construct(array $endpoints, array $config = [])
40
    {
41 9
        $this->setEndpoints($endpoints);
42 9
        $this->config = $config;
43 9
    }
44
45
    /**
46
     * @param array $endpoints
47
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
48
     */
49 9
    protected function setEndpoints(array $endpoints)
50
    {
51 9
        foreach ($endpoints as $key => $endpoint) {
52
            // check if class exists
53 9
            if (!class_exists($endpoint)) {
54 1
                throw new InvalidEndpointException("Class {$endpoint} does not exists");
55
            }
56
57
            // get key
58 9
            if (!is_string($key)) {
59 9
                $key = basename($endpoint);
60 9
            }
61
62
            // save
63 9
            $this->endpoints[$key] = $endpoint;
64 9
        }
65 9
    }
66
67
    /**
68
     * @return array
69
     */
70 1
    public function getConfig()
71
    {
72 1
        return $this->config;
73
    }
74
    
75
    /**
76
     * Make a GET Request to an endpoint
77
     * @param $endpoint
78
     * @return \Jyggen\Curl\Response
79
     * @throws \Jyggen\Curl\Exception\CurlErrorException
80
     * @throws \Jyggen\Curl\Exception\ProtectedOptionException
81
     */
82 1
    public function get($endpoint)
83
    {
84 1
        $request = new Request($this->domain . '/' . $endpoint);
85
        
86
        // Add client secret header
87 1
        if (isset($this->config['secret'])) {
88 1
            $request->headers->add(['X-Client-Secret' => $this->config['secret']]);
89 1
        }
90
        
91
        // add curl options
92 1
        if (isset($this->config['curl']) && is_array($this->config['curl'])) {
93 1
            foreach ($this->config['curl'] as $option => $value) {
94 1
                $request->setOption($option, $value);
95 1
            }
96 1
        }
97
        
98 1
        $request->execute();
99
        
100 1
        return $request->getResponse();
101
    }
102
    
103
    /**
104
     * Get an API endpoint
105
     * @param string $endpoint
106
     * @return \Atog\Api\Endpoint
107
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
108
     */
109 3
    public function __get($endpoint)
110
    {
111 3
        return $this->getEndpoint($endpoint);
112
    }
113
    
114
    /**
115
     * Get an API endpoint.
116
     * @param string $endpoint
117
     * @return \Atog\Api\Endpoint
118
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
119
     */
120 6
    public function getEndpoint($endpoint)
121
    {
122
        // Get Endpoint Class name
123 6
        $endpoint = studly_case($endpoint);
124
125 6
        if (!array_key_exists($endpoint, $this->endpoints)) {
126 4
            throw new InvalidEndpointException("Endpoint {$endpoint} does not exists");
127
        }
128
129 2
        $class = $this->endpoints[$endpoint];
130
131
        // Check if an instance has already been initiated
132 2
        if (isset($this->cachedEndpoints[$endpoint]) === false) {
133
            // check if class is an EndPoint
134 2
            $endpointClass = new \ReflectionClass($class);
135 2
            if (!$endpointClass->isSubclassOf('Atog\Api\Endpoint')) {
136 2
                throw new InvalidEndpointException("Class {$class} does not extend Atog\\Api\\Endpoint");
137
            }
138
            
139
            // check for model
140
            $model = new Model();
141
            if (array_key_exists('models', $this->config) && array_key_exists($endpoint, $this->config['models'])) {
142
                $modelClass = $this->config['models'][$endpoint];
143
                if (class_exists($modelClass)) {
144
                    $model = new $modelClass();
145
                }
146
            }
147
            $this->cachedEndpoints[$endpoint] = new $class($this, $model);
148
        }
149
        
150
        return $this->cachedEndpoints[$endpoint];
151
    }
152
}
153