-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSubscription.php
More file actions
205 lines (178 loc) · 6.3 KB
/
Subscription.php
File metadata and controls
205 lines (178 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
namespace Platformsh\Client\Model;
use GuzzleHttp\ClientInterface;
use Platformsh\Client\Model\Ref\OrganizationRef;
/**
* Represents a Platform.sh subscription.
*
* @property-read int $id
* @property-read string $status
* @property-read string $owner
* @property-read string $plan
* @property-read int $environments Available environments.
* @property-read int $storage Available storage (in MiB).
* @property-read int $user_licenses Number of users.
* @property-read string $project_id
* @property-read string $project_title
* @property-read string $project_options
* @property-read string $project_region
* @property-read string $project_region_label
* @property-read string $project_ui
*/
class Subscription extends ResourceWithReferences
{
public const STATUS_ACTIVE = 'active';
public const STATUS_REQUESTED = 'requested';
public const STATUS_PROVISIONING = 'provisioning';
public const STATUS_FAILED = 'provisioning failure';
public const STATUS_SUSPENDED = 'suspended';
public const STATUS_DELETED = 'deleted';
/**
* List of available plans.
*
* @deprecated
* @see \Platformsh\Client\PlatformClient::getPlans()
*
* @var string[]
*/
public static array $availablePlans = ['development', 'standard', 'medium', 'large'];
/**
* List of available regions.
*
* @deprecated
* @see \Platformsh\Client\PlatformClient::getRegions()
*
* @var string[]
*/
public static array $availableRegions = ['eu-3.platform.sh', 'us-2.platform.sh'];
protected static array $required = ['project_region'];
/**
* @internal Use PlatformClient::createSubscription() to create a new subscription.
*
* @see \Platformsh\Client\PlatformClient::createSubscription()
*/
public static function create(array $body, string $collectionUrl, ClientInterface $client): static
{
$result = parent::create($body, $collectionUrl, $client);
return new static($result->getData(), $collectionUrl, $client);
}
/**
* Wait for the subscription's project to be provisioned.
*
* @param callable|null $onPoll A function that will be called every time the
* subscription is refreshed. It will be passed
* one argument: the Subscription object.
* @param int $interval The polling interval, in seconds.
*/
public function wait(callable $onPoll = null, int $interval = 2): void
{
while ($this->isPending()) {
sleep(max($interval, 1));
$this->refresh();
if ($onPoll !== null) {
$onPoll($this);
}
}
}
/**
* Check whether the subscription is pending (requested or provisioning).
*/
public function isPending(): bool
{
$status = $this->getStatus();
return $status === self::STATUS_PROVISIONING || $status === self::STATUS_REQUESTED;
}
/**
* Find whether the subscription is active.
*/
public function isActive(): bool
{
return $this->getStatus() === self::STATUS_ACTIVE;
}
/**
* Get the subscription status.
*
* This could be one of Subscription::STATUS_ACTIVE,
* Subscription::STATUS_REQUESTED, Subscription::STATUS_PROVISIONING,
* Subscription::STATUS_FAILED, Subscription::STATUS_SUSPENDED,
* or Subscription::STATUS_DELETED.
*/
public function getStatus(): string
{
return $this->getProperty('status');
}
/**
* Get the account for the project's owner.
*/
public function getOwner(): false|Account
{
$uuid = $this->getProperty('owner');
$url = $this->makeAbsoluteUrl('/api/users', $this->getLink('project'));
return Account::get($uuid, $url, $this->client);
}
/**
* Get the project associated with this subscription.
*/
public function getProject(): Project|false
{
if (! $this->hasLink('project') && ! $this->isActive()) {
throw new \BadMethodCallException('Inactive subscriptions do not have projects.');
}
$url = $this->getLink('project');
return Project::get($url, null, $this->client);
}
public static function wrapCollection(array|Collection $data, string $baseUrl, ClientInterface $client): array
{
$dataArray = $data instanceof Collection ? $data->getData() : $data;
if (isset($dataArray['items'])) {
static::$collectionItemsKey = 'items';
} elseif (isset($dataArray['subscriptions'])) {
static::$collectionItemsKey = 'subscriptions';
}
return parent::wrapCollection($data, $baseUrl, $client);
}
public function operationAvailable(string $op, bool $refreshDuringCheck = false): bool
{
if ($op === 'edit') {
return true;
}
return parent::operationAvailable($op, $refreshDuringCheck);
}
public function getLink(string $rel, bool $absolute = false): string
{
if ($rel === '#edit') {
return $this->getUri($absolute);
}
return parent::getLink($rel, $absolute);
}
/**
* Returns detailed information about the subscription's organization, if known.
*/
public function getOrganizationInfo(): ?OrganizationRef
{
if (isset($this->data['organization_id']) && isset($this->data['ref:organizations'][$this->data['organization_id']])) {
return $this->data['ref:organizations'][$this->data['organization_id']];
}
return null;
}
protected static function checkProperty(string $property, mixed $value): array
{
$errors = [];
if ($property === 'storage' && $value < 1024) {
$errors[] = 'Storage must be at least 1024 MiB';
} elseif ($property === 'activation_callback') {
if (! isset($value['uri'])) {
$errors[] = "A 'uri' key is required in the activation callback";
} elseif (! filter_var($value['uri'], FILTER_VALIDATE_URL)) {
$errors[] = 'Invalid URI in activation callback';
}
}
return $errors;
}
protected function setData(array $data): void
{
$data = $data['subscriptions'][0] ?? $data;
$this->data = $data;
}
}