AbstractApi   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleResponse() 0 11 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
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