Passed
Push — master ( 851c5d...3501a4 )
by Artem
01:43
created

Account::getMaintenance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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 <https://opensource.org/licenses/MIT>.
9
//
10
// ---------------------------------------------------------------------
11
12
namespace Linode\Account;
13
14
use Linode\Account\Repository\EventRepository;
15
use Linode\Account\Repository\InvoiceRepository;
16
use Linode\Account\Repository\NotificationRepository;
17
use Linode\Account\Repository\OAuthClientRepository;
18
use Linode\Account\Repository\PaymentRepository;
19
use Linode\Account\Repository\UserRepository;
20
use Linode\Entity;
21
use Linode\Exception\LinodeException;
22
use Linode\LinodeClient;
23
use Linode\Longview\LongviewSubscription;
24
use Linode\Managed\StatsDataAvailable;
25
26
/**
27
 * Account object.
28
 *
29
 * @property string                          $first_name         The first name of the person associated with this Account.
30
 * @property string                          $last_name          The last name of the person associated with this Account.
31
 * @property string                          $email              The email address of the person associated with this Account.
32
 * @property float                           $balance            This Account's balance, in US dollars.
33
 * @property float                           $balance_uninvoiced This Account's current estimated invoice in US dollars. This is not your final
34
 *                                                               invoice balance. Bandwidth charges are not included in the estimate.
35
 * @property string                          $company            The company name associated with this Account.
36
 * @property string                          $address_1          First line of this Account's billing address.
37
 * @property string                          $address_2          Second line of this Account's billing address.
38
 * @property string                          $city               The city for this Account's billing address.
39
 * @property string                          $state              If billing address is in the United States, this is the State portion of the
40
 *                                                               Account's billing address. If the address is outside the US, this is the Province
41
 *                                                               associated with the Account's billing address.
42
 * @property string                          $zip                The zip code of this Account's billing address.
43
 * @property string                          $country            The two-letter country code of this Account's billing address.
44
 * @property string                          $phone              The phone number associated with this Account.
45
 * @property string                          $tax_id             The tax identification number associated with this Account, for tax calculations
46
 *                                                               in some countries. If you do not live in a country that collects tax, this should
47
 *                                                               be `null`.
48
 * @property CreditCard                      $credit_card        Credit Card information associated with this Account.
49
 * @property string                          $active_since       The datetime of when the account was activated.
50
 * @property string[]                        $capabilities       A list of capabilities your account supports.
51
 * @property Promotion[]                     $active_promotions  A list of active promotions on your account. Promotions generally
52
 *                                                               offer a set amount of credit that can be used toward your Linode
53
 *                                                               services, and the promotion expires after a specified date. As well,
54
 *                                                               a monthly cap on the promotional offer is set.
55
 *                                                               Simply put, a promotion offers a certain amount of credit every
56
 *                                                               month, until either the expiration date is passed, or until the total
57
 *                                                               promotional credit is used, whichever comes first.
58
 * @property string                          $euuid              An external unique identifier for this account.
59
 * @property EventRepositoryInterface        $events             List of Event objects representing actions taken on your Account. The Events
60
 *                                                               returned depends on your grants.
61
 * @property InvoiceRepositoryInterface      $invoices           List of Invoices against your Account.
62
 * @property NotificationRepositoryInterface $notifications      List of Notification objects representing important, often time-sensitive items
63
 *                                                               related to your Account.
64
 * @property OAuthClientRepositoryInterface  $oauth_clients      List of OAuth Clients registered to your Account. OAuth Clients allow users to log
65
 *                                                               into applications you write or host using their Linode Account, and may allow them
66
 *                                                               to grant some level of access to their Linodes or other entities to your
67
 *                                                               application.
68
 * @property PaymentRepositoryInterface      $payments           List of Payments made on this Account.
69
 * @property UserRepositoryInterface         $users              List of Users on your Account. Users may access all or part of your Account based
70
 *                                                               on their restricted status and grants. An unrestricted User may access everything
71
 *                                                               on the account, whereas restricted User may only access entities or perform
72
 *                                                               actions they've been given specific grants to.
73
 *
74
 * @codeCoverageIgnore This class was autogenerated.
75
 */
76
class Account extends Entity
77
{
78
    // Available fields.
79
    public const FIELD_FIRST_NAME         = 'first_name';
80
    public const FIELD_LAST_NAME          = 'last_name';
81
    public const FIELD_EMAIL              = 'email';
82
    public const FIELD_BALANCE            = 'balance';
83
    public const FIELD_BALANCE_UNINVOICED = 'balance_uninvoiced';
84
    public const FIELD_COMPANY            = 'company';
85
    public const FIELD_ADDRESS_1          = 'address_1';
86
    public const FIELD_ADDRESS_2          = 'address_2';
87
    public const FIELD_CITY               = 'city';
88
    public const FIELD_STATE              = 'state';
89
    public const FIELD_ZIP                = 'zip';
90
    public const FIELD_COUNTRY            = 'country';
91
    public const FIELD_PHONE              = 'phone';
92
    public const FIELD_TAX_ID             = 'tax_id';
93
    public const FIELD_CREDIT_CARD        = 'credit_card';
94
    public const FIELD_ACTIVE_SINCE       = 'active_since';
95
    public const FIELD_CAPABILITIES       = 'capabilities';
96
    public const FIELD_ACTIVE_PROMOTIONS  = 'active_promotions';
97
    public const FIELD_EUUID              = 'euuid';
98
99
    /**
100
     * Returns the contact and billing information related to your Account.
101
     *
102
     * @throws LinodeException
103
     */
104
    public function __construct(LinodeClient $client)
105
    {
106
        parent::__construct($client);
107
108
        $response = $this->client->get('/account');
109
        $contents = $response->getBody()->getContents();
110
        $json     = json_decode($contents, true);
111
112
        $this->data = $json;
113
    }
114
115
    /**
116
     * @codeCoverageIgnore This method was autogenerated.
117
     */
118
    public function __get(string $name): mixed
119
    {
120
        return match ($name) {
121
            self::FIELD_CREDIT_CARD       => new CreditCard($this->client, $this->data[$name]),
122
            self::FIELD_ACTIVE_PROMOTIONS => array_map(fn ($data) => new Promotion($this->client, $data), $this->data[$name]),
123
            'events'                      => new EventRepository($this->client),
124
            'invoices'                    => new InvoiceRepository($this->client),
125
            'notifications'               => new NotificationRepository($this->client),
126
            'oauth_clients'               => new OAuthClientRepository($this->client),
127
            'payments'                    => new PaymentRepository($this->client),
128
            'users'                       => new UserRepository($this->client),
129
            default                       => parent::__get($name),
130
        };
131
    }
132
133
    /**
134
     * Updates contact and billing information related to your Account.
135
     *
136
     * @param array $parameters Update contact and billing information.
137
     *
138
     * @throws LinodeException
139
     */
140
    public function updateAccount(array $parameters = []): self
141
    {
142
        $response   = $this->client->put('/account', $parameters);
143
        $contents   = $response->getBody()->getContents();
144
        $this->data = json_decode($contents, true);
145
146
        return $this;
147
    }
148
149
    /**
150
     * Cancels an active Linode account. This action will cause Linode to attempt to
151
     * charge the credit card on file for the remaining balance. An error will occur if
152
     * Linode fails to charge the credit card on file. Restricted users will not be able
153
     * to cancel an account.
154
     *
155
     * @param string $comments Any reason for cancelling the account, and any other comments you might have about
156
     *                         your Linode service.
157
     *
158
     * @return string A link to Linode's exit survey.
159
     *
160
     * @throws LinodeException
161
     */
162
    public function cancelAccount(string $comments): string
163
    {
164
        $parameters = [
165
            'comments' => $comments,
166
        ];
167
168
        $response = $this->client->post('/account/cancel', $parameters);
169
        $contents = $response->getBody()->getContents();
170
        $json     = json_decode($contents, true);
171
172
        return $json['survey_link'];
173
    }
174
175
    /**
176
     * Adds/edit credit card information to your Account.
177
     * Only one credit card can be associated with your Account, so using this endpoint
178
     * will overwrite your currently active card information with the new credit card.
179
     *
180
     * @param string $card_number  Your credit card number. No spaces or dashes allowed.
181
     * @param string $expiry_month A value from 1-12 representing the expiration month of your credit card.
182
     * @param string $expiry_year  A four-digit integer representing the expiration year of your credit card.
183
     * @param string $cvv          The Card Verification Value on the back of the card.
184
     *
185
     * @throws LinodeException
186
     */
187
    public function createCreditCard(string $card_number, string $expiry_month, string $expiry_year, string $cvv): void
188
    {
189
        $parameters = [
190
            'card_number'  => $card_number,
191
            'expiry_month' => $expiry_month,
192
            'expiry_year'  => $expiry_year,
193
            'cvv'          => $cvv,
194
        ];
195
196
        $this->client->post('/account/credit-card', $parameters);
197
    }
198
199
    /**
200
     * Returns a collection of successful logins for all users on the account during the
201
     * last 90 days. This command can only be accessed by the unrestricted users of an
202
     * account.
203
     *
204
     * @return Login[] A collection of successful logins for all users on the account during the last 90
205
     *                 days.
206
     *
207
     * @throws LinodeException
208
     */
209
    public function getAccountLogins(): array
210
    {
211
        $response = $this->client->get('/account/logins');
212
        $contents = $response->getBody()->getContents();
213
        $json     = json_decode($contents, true);
214
215
        return array_map(fn ($data) => new Login($this->client, $data), $json['data']);
216
    }
217
218
    /**
219
     * Returns a Login object that displays information about a successful login. The
220
     * logins that can be viewed can be for any user on the account, and are not limited
221
     * to only the logins of the user that is accessing this API endpoint. This command
222
     * can only be accessed by the unrestricted users of the account.
223
     *
224
     * @param int $loginId The ID of the login object to access.
225
     *
226
     * @throws LinodeException
227
     */
228
    public function getAccountLogin(int $loginId): Login
229
    {
230
        $response = $this->client->get("/account/logins/{$loginId}");
231
        $contents = $response->getBody()->getContents();
232
        $json     = json_decode($contents, true);
233
234
        return new Login($this->client, $json);
235
    }
236
237
    /**
238
     * Returns a collection of Maintenance objects for any entity a user has permissions
239
     * to view.
240
     *
241
     * Currently, Linodes are the only entities available for viewing.
242
     *
243
     * **Beta**: This endpoint is in beta. Please make sure to prepend all requests with
244
     * `/v4beta` instead of `/v4`, and be aware that this endpoint may receive breaking
245
     * updates in the future. This notice will be removed when this endpoint is out of
246
     * beta.
247
     *
248
     * @throws LinodeException
249
     */
250
    public function getMaintenance(): array
251
    {
252
        $response = $this->client->get('/account/maintenance');
253
        $contents = $response->getBody()->getContents();
254
255
        return json_decode($contents, true);
256
    }
257
258
    /**
259
     * Returns a Transfer object showing your network utilization, in GB, for the current
260
     * month.
261
     *
262
     * @throws LinodeException
263
     */
264
    public function getTransfer(): Transfer
265
    {
266
        $response = $this->client->get('/account/transfer');
267
        $contents = $response->getBody()->getContents();
268
        $json     = json_decode($contents, true);
269
270
        return new Transfer($this->client, $json);
271
    }
272
273
    /**
274
     * Returns information related to your Account settings: Managed service
275
     * subscription, Longview subscription, and network helper.
276
     *
277
     * @throws LinodeException
278
     */
279
    public function getAccountSettings(): AccountSettings
280
    {
281
        $response = $this->client->get('/account/settings');
282
        $contents = $response->getBody()->getContents();
283
        $json     = json_decode($contents, true);
284
285
        return new AccountSettings($this->client, $json);
286
    }
287
288
    /**
289
     * Updates your Account settings.
290
     *
291
     * To update your Longview subscription plan, send a request to Update Longview Plan.
292
     *
293
     * @param array $parameters Update Account settings information.
294
     *
295
     * @throws LinodeException
296
     */
297
    public function updateAccountSettings(array $parameters = []): AccountSettings
298
    {
299
        $response = $this->client->put('/account/settings', $parameters);
300
        $contents = $response->getBody()->getContents();
301
        $json     = json_decode($contents, true);
302
303
        return new AccountSettings($this->client, $json);
304
    }
305
306
    /**
307
     * Enables Linode Managed for the entire account and sends a welcome email to the
308
     * account's associated email address. Linode Managed can monitor any service or
309
     * software stack reachable over TCP or HTTP. See our Linode Managed guide to learn
310
     * more.
311
     *
312
     * @throws LinodeException
313
     */
314
    public function enableAccountManged(): void
315
    {
316
        $this->client->post('/account/settings/managed-enable');
317
    }
318
319
    /**
320
     * Get the details of your current Longview plan. This returns a
321
     * `LongviewSubscription` object for your current Longview Pro plan, or an empty set
322
     * `{}` if your current plan is Longview Free.
323
     *
324
     * You must have at least one of the following `global` User Grants in order to
325
     * access this endpoint:
326
     *
327
     *   - `"account_access": read_write`
328
     *   - `"account_access": read_only`
329
     *   - `"longview_subscription": true`
330
     *   - `"add_longview": true`
331
     *
332
     * To update your subscription plan, send a request to Update Longview Plan.
333
     *
334
     * @throws LinodeException
335
     */
336
    public function getLongviewPlan(): LongviewSubscription
337
    {
338
        $response = $this->client->get('/longview/plan');
339
        $contents = $response->getBody()->getContents();
340
        $json     = json_decode($contents, true);
341
342
        return new LongviewSubscription($this->client, $json);
343
    }
344
345
    /**
346
     * Update your Longview plan to that of the given subcription ID. This returns a
347
     * `LongviewSubscription` object for the updated Longview Pro plan, or an empty set
348
     * `{}` if the updated plan is Longview Free.
349
     *
350
     * You must have `"longview_subscription": true` configured as a `global` User Grant
351
     * in order to access this endpoint.
352
     *
353
     * You can send a request to the List Longview Subscriptions endpoint to receive the
354
     * details, including `id`'s, of each plan.
355
     *
356
     * @param array $parameters Update your Longview subscription plan.
357
     *
358
     * @throws LinodeException
359
     */
360
    public function updateLongviewPlan(array $parameters = []): LongviewSubscription
361
    {
362
        $response = $this->client->put('/longview/plan', $parameters);
363
        $contents = $response->getBody()->getContents();
364
        $json     = json_decode($contents, true);
365
366
        return new LongviewSubscription($this->client, $json);
367
    }
368
369
    /**
370
     * Returns a list of Managed Stats on your Account in the form of x and y data
371
     * points.
372
     * You can use these data points to plot your own graph visualizations. These stats
373
     * reflect the last 24 hours of combined usage across all managed Linodes on your
374
     * account
375
     * giving you a high-level snapshot of data for the following:
376
     *
377
     * * cpu
378
     * * disk
379
     * * swap
380
     * * network in
381
     * * network out
382
     *
383
     * @return StatsDataAvailable A list of Managed Stats from the last 24 hours.
384
     *
385
     * @throws LinodeException
386
     */
387
    public function getManagedStats(): StatsDataAvailable
388
    {
389
        $response = $this->client->get('/managed/stats');
390
        $contents = $response->getBody()->getContents();
391
        $json     = json_decode($contents, true);
392
393
        return new StatsDataAvailable($this->client, $json);
394
    }
395
}
396