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

CloudApiBase::getLinkedResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 27
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 36
rs 9.488
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
        $type = $link['type'];
41
42
        $classMap = [
43
            'alerts' => '\AcquiaCloudApi\Response\InsightAlertsResponse',
44
            'applications' => '\AcquiaCloudApi\Response\ApplicationsResponse',
45
            'backups' => '\AcquiaCloudApi\Response\BackupsResponse',
46
            'code' => '\AcquiaCloudApi\Response\BranchesResponse',
47
            'crons' => '\AcquiaCloudApi\Response\CronsResponse',
48
            'databases' => '\AcquiaCloudApi\Response\DatabasesResponse',
49
            'domains' => '\AcquiaCloudApi\Response\DomainsResponse',
50
            'environments' => '\AcquiaCloudApi\Response\EnvironmentsResponse',
51
            'ides' => '\AcquiaCloudApi\Response\IdesResponse',
52
            'insight' => '\AcquiaCloudApi\Response\InsightsResponse',
53
            'logs' => '\AcquiaCloudApi\Response\LogsResponse',
54
            'members' => '\AcquiaCloudApi\Response\MembersResponse',
55
            'metrics' => '\AcquiaCloudApi\Response\MetricsResponse',
56
            'modules' => '\AcquiaCloudApi\Response\InsightModulesResponse',
57
            'notification' => '\AcquiaCloudApi\Response\NotificationResponse',
58
            'permissions' => '\AcquiaCloudApi\Response\PermissionsResponse',
59
            'servers' => '\AcquiaCloudApi\Response\ServersResponse',
60
            'ssl' => '\AcquiaCloudApi\Response\SslCertificatesResponse',
61
            'teams' => '\AcquiaCloudApi\Response\TeamsResponse',
62
            'variables' => '\AcquiaCloudApi\Response\VariablesResponse',
63
        ];
64
65
        if (isset($classMap[$type])) {
66
            return new $classMap[$type](
67
                $this->client->request('get', $path)
68
            );
69
        }
70
71
        throw new LinkedResourceNotImplementedException($type . ' link not implemented in this SDK. Please file an issue here: https://github.com/typhonius/acquia-php-sdk-v2/issues');
72
    }
73
}
74