Completed
Push — master ( efa3fa...174cfa )
by Albert
03:11
created

Vultr::createAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
/*
3
 *   This file is part of the Vultr PHP library.
4
 *
5
 *   (c) Albert Leitato <[email protected]>
6
 *
7
 *   For the full copyright and license information, please view the LICENSE
8
 *   file that was distributed with this source code.
9
 */
10
11
namespace Vultr;
12
13
/*
14
 * Vultr.com API Client.
15
 *
16
 * @version 1.0
17
 *
18
 * @author  Albert Leitato <[email protected]>
19
 * @license http://www.opensource.org/licenses/mit-license.php MIT
20
 *
21
 * @see     https://github.com/weezqyd/vultr-php
22
 */
23
24
use Http\Exceptions;
25
use Http\Adapter\GuzzleHttpAdapter;
26
use Http\Adapter\AdapterInterface;
27
28
class Vultr
29
{
30
    use Support\Str;
31
32
    /**
33
     * @var AdapterInterface
34
     */
35
    protected $adapter;
36
37
    /**
38
     * Create a new Vultr API instance.
39
     *
40
     * This is the entry point to the api resource
41
     *
42
     * @param AdapterInterface $adapter
43
     */
44
    public function __construct($token, AdapterInterface $adapter = null)
45
    {
46
        $this->createAdapter($adapter, $token);
0 ignored issues
show
Bug introduced by
It seems like $adapter defined by parameter $adapter on line 44 can also be of type object<Http\Adapter\AdapterInterface>; however, Vultr\Vultr::createAdapter() does only seem to accept object<Vultr\Http\Adapter\AdapterInterface>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47
    }
48
49
    /**
50
     * Call API resources as though the were Vultr::class properties.
51
     *
52
     * @param mixed $resource
53
     *
54
     * @return mixed Api Resource if it exists
55
     **/
56
    public function __get($resource)
57
    {
58
        return $this->getClass($resource);
59
    }
60
61
    /**
62
     * Create resource instance.
63
     *
64
     * @return mixed
65
     **/
66
    protected function getClass($class)
67
    {
68
        $resource = static::studly($class);
69
        $class = 'Vultr\\Api\\'.$resource;
70
        if (\class_exists($class)) {
71
            return new $class($this->adapter);
72
        }
73
        throw new Exceptions\ErrorException("The class $class does not exist");
74
    }
75
76
    /**
77
     * Set the default client adapter.
78
     *
79
     * @param \Http\Adapter\AdapterInterface $adapter
80
     **/
81
    public function setClient(AdapterInterface $adapter)
82
    {
83
        $this->adapter = $adapter;
84
    }
85
86
    /**
87
     * Set up the Guzzle adapter.
88
     *
89
     * @param string $token Api Token
90
     **/
91
    protected function createGuzzleAdapter($token)
92
    {
93
        $options = ['headers' => [
94
                        'API-Key' => $token,
95
                        'Accept' => 'application/json',
96
                        'Content-Type' => 'application/x-www-form-urlencoded',
97
                    ],
98
                ];
99
        $adapter = new GuzzleHttpAdapter($options);
100
        $this->setClient($adapter);
101
    }
102
103
    /**
104
     * Create a client adapter.
105
     *
106
     * @param Http\Adapter\AdapterInterface|null $adapter
107
     * @param string|null                        $token   API Token
108
     **/
109
    protected function createAdapter($adapter, $token = null)
110
    {
111
        if ($adapter instanceof AdapterInterface) {
112
            $this->setClient($adapter);
113
        } else {
114
            $this->createGuzzleAdapter($token);
115
        }
116
    }
117
}
118