Passed
Push — master ( 2c24c4...fb6e78 )
by Artem
01:55
created

Account::getNetworkUtilization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
// ---------------------------------------------------------------------
4
//
5
//  Copyright (C) 2018-2024 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
// ---------------------------------------------------------------------
11
12
namespace Linode\Entity;
13
14
use Linode\Entity\Account\AccountInformation;
15
use Linode\Entity\Account\AccountSettings;
16
use Linode\Entity\Account\NetworkUtilization;
17
use Linode\Exception\LinodeException;
18
use Linode\Internal\Account\EventRepository;
19
use Linode\Internal\Account\InvoiceRepository;
20
use Linode\Internal\Account\NotificationRepository;
21
use Linode\Internal\Account\OAuthClientRepository;
22
use Linode\Internal\Account\PaymentRepository;
23
use Linode\Internal\Account\UserRepository;
24
use Linode\Internal\Longview\LongviewClientRepository;
25
use Linode\Internal\Managed\ManagedContactRepository;
26
use Linode\Internal\Managed\ManagedCredentialRepository;
27
use Linode\Internal\Managed\ManagedIssueRepository;
28
use Linode\Internal\Managed\ManagedLinodeSettingsRepository;
29
use Linode\Internal\Managed\ManagedServiceRepository;
30
use Linode\LinodeClient;
31
use Linode\Repository\Account as AccountRepository;
32
use Linode\Repository\Longview as LongviewRepository;
33
use Linode\Repository\Managed as ManagedRepository;
34
use Linode\Repository\RepositoryInterface;
35
36
/**
37
 * Current user account.
38
 *
39
 * @property AccountRepository\EventRepositoryInterface                 $events
40
 * @property AccountRepository\InvoiceRepositoryInterface               $invoices
41
 * @property LongviewRepository\LongviewClientRepositoryInterface       $longviews_clients
42
 * @property ManagedRepository\ManagedContactRepositoryInterface        $managed_contacts
43
 * @property ManagedRepository\ManagedCredentialRepositoryInterface     $managed_credentials
44
 * @property ManagedRepository\ManagedIssueRepositoryInterface          $managed_issues
45
 * @property ManagedRepository\ManagedLinodeSettingsRepositoryInterface $managed_linode_settings
46
 * @property ManagedRepository\ManagedServiceRepositoryInterface        $managed_services
47
 * @property AccountRepository\NotificationRepositoryInterface          $notifications
48
 * @property AccountRepository\OAuthClientRepositoryInterface           $oauth_clients
49
 * @property AccountRepository\PaymentRepositoryInterface               $payments
50
 * @property AccountRepository\UserRepositoryInterface                  $users
51
 */
52
class Account
53
{
54
    /**
55
     * Account constructor.
56
     *
57
     * @param LinodeClient $client linode API client
58
     */
59
    public function __construct(protected LinodeClient $client) {}
60
61
    /**
62
     * Returns requested repository.
63
     *
64
     * @param string $name repository name
65
     */
66 1
    public function __get(string $name): ?RepositoryInterface
67
    {
68 1
        return match ($name) {
69 1
            'events'                  => new EventRepository($this->client),
70 1
            'invoices'                => new InvoiceRepository($this->client),
71 1
            'longviews_clients'       => new LongviewClientRepository($this->client),
72 1
            'managed_contacts'        => new ManagedContactRepository($this->client),
73 1
            'managed_credentials'     => new ManagedCredentialRepository($this->client),
74 1
            'managed_issues'          => new ManagedIssueRepository($this->client),
75 1
            'managed_linode_settings' => new ManagedLinodeSettingsRepository($this->client),
76 1
            'managed_services'        => new ManagedServiceRepository($this->client),
77 1
            'notifications'           => new NotificationRepository($this->client),
78 1
            'oauth_clients'           => new OAuthClientRepository($this->client),
79 1
            'payments'                => new PaymentRepository($this->client),
80 1
            'users'                   => new UserRepository($this->client),
81 1
            default                   => null,
82 1
        };
83
    }
84
85
    /**
86
     * Returns the contact and billing information related to your Account.
87
     *
88
     * @throws LinodeException
89
     */
90 1
    public function getAccountInformation(): AccountInformation
91
    {
92 1
        $response = $this->client->api($this->client::REQUEST_GET, '/account');
93 1
        $contents = $response->getBody()->getContents();
94 1
        $json     = json_decode($contents, true);
95
96 1
        return new AccountInformation($this->client, $json);
97
    }
98
99
    /**
100
     * Updates contact and billing information related to your Account.
101
     *
102
     * @throws LinodeException
103
     */
104 1
    public function setAccountInformation(array $parameters): AccountInformation
105
    {
106 1
        $response = $this->client->api($this->client::REQUEST_PUT, '/account', $parameters);
107 1
        $contents = $response->getBody()->getContents();
108 1
        $json     = json_decode($contents, true);
109
110 1
        return new AccountInformation($this->client, $json);
111
    }
112
113
    /**
114
     * Cancels an active Linode account. This action will cause
115
     * Linode to attempt to charge the credit card on file for the remaining
116
     * balance. An error will occur if Linode fails to charge the credit card
117
     * on file. Restricted users will not be able to cancel an account.
118
     *
119
     * @param null|string $comments any reason for cancelling the account, and any other comments
120
     *                              you might have about your Linode service
121
     *
122
     * @return string a link to Linode's exit survey
123
     *
124
     * @throws LinodeException
125
     */
126 1
    public function cancel(?string $comments = null): string
127
    {
128 1
        $parameters = [
129 1
            'comments' => $comments,
130 1
        ];
131
132 1
        $response = $this->client->api($this->client::REQUEST_POST, '/account/cancel', $parameters);
133 1
        $contents = $response->getBody()->getContents();
134 1
        $json     = json_decode($contents, true);
135
136 1
        return $json['survey_link'];
137
    }
138
139
    /**
140
     * Adds/edit credit card information to your Account.
141
     *
142
     * Only one credit card can be associated with your Account, so using this
143
     * endpoint will overwrite your currently active card information with the
144
     * new credit card.
145
     *
146
     * @param string $card_number  your credit card number (no spaces or dashes allowed)
147
     * @param string $expiry_month a value from 1-12 representing the expiration month of your credit card
148
     * @param string $expiry_year  a four-digit integer representing the expiration year of your credit card
149
     * @param string $cvv          the Card Verification Value on the back of the card
150
     *
151
     * @throws LinodeException
152
     */
153 1
    public function updateCreditCard(string $card_number, string $expiry_month, string $expiry_year, string $cvv): void
154
    {
155 1
        $parameters = [
156 1
            'card_number'  => $card_number,
157 1
            'expiry_month' => $expiry_month,
158 1
            'expiry_year'  => $expiry_year,
159 1
            'cvv'          => $cvv,
160 1
        ];
161
162 1
        $this->client->api($this->client::REQUEST_POST, '/account/credit-card', $parameters);
163
    }
164
165
    /**
166
     * Returns information related to your Account settings: Managed service
167
     * subscription, Longview subscription, and network helper.
168
     *
169
     * @throws LinodeException
170
     */
171 1
    public function getAccountSettings(): AccountSettings
172
    {
173 1
        $response = $this->client->api($this->client::REQUEST_GET, '/account/settings');
174 1
        $contents = $response->getBody()->getContents();
175 1
        $json     = json_decode($contents, true);
176
177 1
        return new AccountSettings($this->client, $json);
178
    }
179
180
    /**
181
     * Updates your Account settings.
182
     *
183
     * @throws LinodeException
184
     */
185 1
    public function setAccountSettings(array $parameters): AccountSettings
186
    {
187 1
        $response = $this->client->api($this->client::REQUEST_PUT, '/account/settings', $parameters);
188 1
        $contents = $response->getBody()->getContents();
189 1
        $json     = json_decode($contents, true);
190
191 1
        return new AccountSettings($this->client, $json);
192
    }
193
194
    /**
195
     * Returns a Transfer object showing your network utilization, in GB, for the current month.
196
     *
197
     * @throws LinodeException
198
     */
199 1
    public function getNetworkUtilization(): NetworkUtilization
200
    {
201 1
        $response = $this->client->api($this->client::REQUEST_GET, '/account/transfer');
202 1
        $contents = $response->getBody()->getContents();
203 1
        $json     = json_decode($contents, true);
204
205 1
        return new NetworkUtilization($this->client, $json);
206
    }
207
}
208