AbstractApi::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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
namespace Vultr\Api;
11
12
use Http\Adapter\AdapterInterface;
13
use Vultr\Entity\Meta;
14
15
/**
16
 * @author Albert Leitato <[email protected]>
17
 */
18
abstract class AbstractApi
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $endpoint = 'https://api.vultr.com/v1';
24
25
    /**
26
     * @var AdapterInterface
27
     */
28
    protected $adapter;
29
30
    /**
31
     * @var Meta
32
     */
33
    protected $meta;
34
35
    /**
36
     * @param AdapterInterface $adapter
37
     */
38
    public function __construct(AdapterInterface $adapter)
39
    {
40
        $this->adapter = $adapter;
41
    }
42
43
    /**
44
     * Bind response to Entity.
45
     *
46
     * Decode API response and bind the response to the respective entity
47
     *
48
     * @param string $response the response from the API
49
     * @param string $class    Entity to bind the response to
50
     * @param bool   $isArray  If the response is a collection
51
     *
52
     * @return object Entity instance
53
     **/
54
    protected function handleResponse($response, $class, $isArray = false)
55
    {
56
        $object = \json_decode($response, true);
57
        if ($isArray) {
58
            return \array_map(function ($entity) use ($class) {
59
                return new $class($entity);
60
            }, $object);
61
        }
62
63
        return new $class($object);
64
    }
65
}
66