Passed
Pull Request — master (#111)
by Adam
03:17 queued 01:21
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, responseClass:class-string} $link
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{type:string, path:...onseClass:class-string} at position 12 could not be parsed: Unknown type name 'class-string' at position 12 in array{type:string, path:string, responseClass:class-string}.
Loading history...
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
        $responseClass = $link['responseClass'];
43
44
        $classMap = [
45
            'alerts' => '\AcquiaCloudApi\Response\InsightAlertsResponse',
46
            'applications' => '\AcquiaCloudApi\Response\ApplicationsResponse',
47
            'backups' => '\AcquiaCloudApi\Response\BackupsResponse',
48
            'code' => '\AcquiaCloudApi\Response\BranchesResponse',
49
            'crons' => '\AcquiaCloudApi\Response\CronsResponse',
50
            'databases' => '\AcquiaCloudApi\Response\DatabasesResponse',
51
            'domains' => '\AcquiaCloudApi\Response\DomainsResponse',
52
            'environments' => '\AcquiaCloudApi\Response\EnvironmentsResponse',
53
            'ides' => '\AcquiaCloudApi\Response\IdesResponse',
54
            'insight' => '\AcquiaCloudApi\Response\InsightsResponse',
55
            'logs' => '\AcquiaCloudApi\Response\LogsResponse',
56
            'members' => '\AcquiaCloudApi\Response\MembersResponse',
57
            'metrics' => '\AcquiaCloudApi\Response\MetricsResponse',
58
            'modules' => '\AcquiaCloudApi\Response\InsightModulesResponse',
59
            'notification' => '\AcquiaCloudApi\Response\NotificationResponse',
60
            'permissions' => '\AcquiaCloudApi\Response\PermissionsResponse',
61
            'self' => $responseClass,
62
            'servers' => '\AcquiaCloudApi\Response\ServersResponse',
63
            'ssl' => '\AcquiaCloudApi\Response\SslCertificatesResponse',
64
            'teams' => '\AcquiaCloudApi\Response\TeamsResponse',
65
            'variables' => '\AcquiaCloudApi\Response\VariablesResponse',
66
        ];
67
68
        // Clear any queries attached to the client to prevent sorts etc being carried through
69
        // from the original query.
70
        $this->client->clearQuery();
71
72
        if (isset($classMap[$type])) {
73
            return new $classMap[$type](
74
                $this->client->request('get', $path)
75
            );
76
        }
77
78
        throw new LinkedResourceNotImplementedException($type . ' link not implemented in this SDK. Please file an issue here: https://github.com/typhonius/acquia-php-sdk-v2/issues');
79
    }
80
}
81