Completed
Push — master ( 156e79...007c06 )
by Pascal
02:10
created

Client::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 8.8571
cc 5
eloc 9
nc 4
nop 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
     * Namespace for the endpoints
14
     * @var string
15
     */
16
    protected $endpointNamespace;
17
    /**
18
     * @var string
19
     */
20
    protected $domain;
21
    /**
22
     * @var array
23
     */
24
    protected $config;
25
    /**
26
     * A array containing the cached endpoints
27
     * @var array
28
     */
29
    private $cachedEndpoints = [];
30
    
31
    /**
32
     * Create a new client instance.
33
     * @param array $config
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        $this->config = $config;
38
    }
39
    
40
    /**
41
     * Make a GET Request to an endpoint
42
     * @param $endpoint
43
     * @return \Jyggen\Curl\Response
44
     * @throws \Jyggen\Curl\Exception\CurlErrorException
45
     * @throws \Jyggen\Curl\Exception\ProtectedOptionException
46
     */
47
    public function get($endpoint)
48
    {
49
        $request = new Request($this->domain . '/' . $endpoint);
50
        
51
        // Add client secret header
52
        if (isset($config['secret'])) {
0 ignored issues
show
Bug introduced by
The variable $config seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
53
            $request->headers->add(['X-Client-Secret' => $config['secret']]);
54
        }
55
        
56
        // add curl options
57
        if (isset($this->config['curl']) && is_array($this->config['curl'])) {
58
            foreach ($this->config['curl'] as $option => $value) {
59
                $request->setOption($option, $value);
60
            }
61
        }
62
        
63
        $request->execute();
64
        
65
        return $request->getResponse();
66
    }
67
    
68
    /**
69
     * Get an API endpoint
70
     * @param string $endpoint
71
     * @return \Atog\Api\Endpoint
72
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
73
     */
74
    public function __get($endpoint)
75
    {
76
        return $this->getEndpoint($endpoint);
77
    }
78
    
79
    /**
80
     * Get an API endpoint.
81
     * @param string $endpoint
82
     * @return \Atog\Api\Endpoint
83
     * @throws \Atog\Api\Exceptions\InvalidEndpointException
84
     */
85
    public function getEndpoint($endpoint)
86
    {
87
        // Create studly class name
88
        $endpoint = studly_case($endpoint);
89
        $class = "\\{$this->endpointNamespace}\\{$endpoint}";
90
        
91
        // Check if an instance has already been initiated
92
        if (isset($this->cachedEndpoints[$endpoint]) === false) {
93
            // check if class exists
94
            if (!class_exists($class)) {
95
                throw new InvalidEndpointException("Class {$class} does not exists");
96
            }
97
            
98
            // check if class is an EndPoint
99
            $endpointClass = new \ReflectionClass($class);
100
            if (!$endpointClass->isSubclassOf('Atog\Api\Endpoint')) {
101
                throw new InvalidEndpointException("Class {$class} does not extend Atog\\Api\\Endpoint");
102
            }
103
            
104
            // check for model
105
            $model = new Model();
106
            if (array_key_exists('models', $this->config) && array_key_exists($endpoint, $this->config['models'])) {
107
                $modelClass = $this->config['models'][$endpoint];
108
                if (class_exists($modelClass)) {
109
                    $model = new $modelClass();
110
                }
111
            }
112
            $this->cachedEndpoints[$endpoint] = new $class($this, $model);
113
        }
114
        
115
        return $this->cachedEndpoints[$endpoint];
116
    }
117
}
118