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

CloudApiBase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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