Passed
Pull Request — master (#111)
by Adam
01:41
created

CloudApiBase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLinkedResource() 0 38 2
1
<?php
2
3
namespace AcquiaCloudApi\Endpoints;
4
5
use AcquiaCloudApi\Connector\ClientInterface;
6
use AcquiaCloudApi\Exception\LinkedResourceNotImplementedException;
7
8
/**
9
 * Class CloudApiBase
10
 *
11
 * @package AcquiaCloudApi\CloudApi
12
 */
13
abstract class CloudApiBase implements CloudApiInterface
14
{
15
16
    /**
17
     * @var ClientInterface The API client.
18
     */
19
    protected $client;
20
21
    /**
22
     * Client constructor.
23
     *
24
     * @param ClientInterface $client
25
     */
26
    public function __construct(ClientInterface $client)
27
    {
28
        $this->client = $client;
29
    }
30
31
    /**
32
     * @param array{type:string, path:string} $link
33
     * @return mixed
34
     * @throws LinkedResourceNotImplementedException
35
     */
36
    public function getLinkedResource($link)
37
    {
38
        // Remove https://cloud.acquia.com/api from the path as this is already added by the Connector.
39
        $path = str_replace('https://cloud.acquia.com/api', '', $link['path']);
40
41
        $type = $link['type'];
42
43
        $typeMap = [
44
            'alerts' => 'InsightAlertsResponse',
45
            'applications' => 'ApplicationsResponse',
46
            'backups' => 'BackupsResponse',
47
            'code' => 'BranchesResponse',
48
            'crons' => 'CronsResponse',
49
            'databases' => 'DatabasesResponse',
50
            'domains' => 'DomainsResponse',
51
            'environments' => 'EnvironmentsResponse',
52
            'ides' => 'IdesResponse',
53
            'insight' => 'InsightsResponse',
54
            'logs' => 'LogsResponse',
55
            'members' => 'MembersResponse',
56
            'metrics' => 'MetricsResponse',
57
            'modules' => 'InsightModulesResponse',
58
            'notification' => 'NotificationResponse',
59
            'permissions' => 'PermissionsResponse',
60
            'servers' => 'ServersResponse',
61
            'ssl' => 'SslCertificatesResponse',
62
            'teams' => 'TeamsResponse',
63
            'variables' => 'VariablesResponse',
64
        ];
65
66
        if (isset($typeMap[$type])) {
67
            $class = "\AcquiaCloudApi\Response\\${typeMap[$type]}";
68
            return new $class(
69
                $this->client->request('get', $path)
70
            );
71
        }
72
73
        throw new LinkedResourceNotImplementedException($type . ' link not implemented in this SDK. Please file an issue here: https://github.com/typhonius/acquia-php-sdk-v2/issues');
74
    }
75
}
76