Newer
Older
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
namespace Drupal\automatic_updates_9_3_shim;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Validation;
/**
* Provides a project release value object.
*/
final class ProjectRelease {
/**
* Whether the release is compatible with the site's Drupal core version.
*
* @var bool
*/
private $coreCompatible;
/**
* The core compatibility message or NULL if not set.
*
* @var string|null
*/
private $coreCompatibilityMessage;
/**
* The download URL or NULL if none is available.
*
* @var string|null
*/
private $downloadUrl;
/**
* The URL for the release.
*
* @var string
*/
private $releaseUrl;
/**
* The release types or NULL if not set.
*
* @var string[]|null
*/
private $releaseTypes;
/**
* Whether the release is published.
*
* @var bool
*/
private $published;
/**
* The release version.
*
* @var string
*/
private $version;
/**
* The release date as a Unix timestamp or NULL if no date was set.
*
* @var int|null
*/
private $date;
/**
* Constructs a ProjectRelease object.
*
* @param bool $published
* Whether the release is published.
* @param string $version
* The release version.
* @param string $release_url
* The URL for the release.
* @param string[]|null $release_types
* The release types or NULL if not set.
* @param bool|null $core_compatible
* Whether the release is compatible with the site's version of Drupal core.
* @param string|null $core_compatibility_message
* The core compatibility message or NULL if not set.
* @param string|null $download_url
* The download URL or NULL if not available.
* @param int|null $date
* The release date in Unix timestamp format.
*/
private function __construct(bool $published, string $version, string $release_url, ?array $release_types, ?bool $core_compatible, ?string $core_compatibility_message, ?string $download_url, ?int $date) {
$this->published = $published;
$this->version = $version;
$this->releaseUrl = $release_url;
$this->releaseTypes = $release_types;
$this->coreCompatible = $core_compatible;
$this->coreCompatibilityMessage = $core_compatibility_message;
$this->downloadUrl = $download_url;
$this->date = $date;
}
/**
* Creates a ProjectRelease instance from an array.
*
* @param array $release_data
* The project release data as returned by update_get_available().
*
* @return \Drupal\update\ProjectRelease
* The ProjectRelease instance.
*
* @throws \UnexpectedValueException
* Thrown if project release data is not valid.
*
* @see \update_get_available()
*/
public static function createFromArray(array $release_data): ProjectRelease {
static::validateReleaseData($release_data);
return new ProjectRelease(
$release_data['status'] === 'published',
$release_data['version'],
$release_data['release_link'],
$release_data['terms']['Release type'] ?? NULL,
$release_data['core_compatible'] ?? NULL,
$release_data['core_compatibility_message'] ?? NULL,
$release_data['download_link'] ?? NULL,
$release_data['date'] ?? NULL
);
}
/**
* Validates the project release data.
*
* @param array $data
* The project release data.
*
* @throws \UnexpectedValueException
* Thrown if project release data is not valid.
*/
private static function validateReleaseData(array $data): void {
$not_blank_constraints = [
new Type('string'),
new NotBlank(),
];
$collection_constraint = new Collection([
'fields' => [
'version' => $not_blank_constraints,
'date' => new Optional([new Type('numeric')]),
'core_compatible' => new Optional([new Type('boolean')]),
'core_compatibility_message' => new Optional($not_blank_constraints),
'status' => new Choice(['published', 'unpublished']),
'download_link' => new Optional($not_blank_constraints),
'release_link' => $not_blank_constraints,
'terms' => new Optional([
new Type('array'),
new Collection([
'Release type' => new Optional([
new Type('array'),
]),
]),
]),
],
'allowExtraFields' => TRUE,
]);
$violations = Validation::createValidator()->validate($data, $collection_constraint);
if (count($violations)) {
foreach ($violations as $violation) {
$violation_messages[] = "Field " . $violation->getPropertyPath() . ": " . $violation->getMessage();
}
throw new \UnexpectedValueException('Malformed release data: ' . implode(",\n", $violation_messages));
}
}
/**
* Gets the project version.
*
* @return string
* The project version.
*/
public function getVersion(): string {
return $this->version;
}
/**
* Gets the release date if set.
*
* @return int|null
* The date of the release or null if no date is available.
*/
public function getDate(): ?int {
return $this->date;
}
/**
* Determines if the release is a security release.
*
* @return bool
* TRUE if the release is security release, or FALSE otherwise.
*/
public function isSecurityRelease(): bool {
return $this->isReleaseType('Security update');
}
/**
* Determines if the release is unsupported.
*
* @return bool
* TRUE if the release is unsupported, or FALSE otherwise.
*/
public function isUnsupported(): bool {
return $this->isReleaseType('Unsupported');
}
/**
* Determines if the release is insecure.
*
* @return bool
* TRUE if the release is insecure, or FALSE otherwise.
*/
public function isInsecure(): bool {
return $this->isReleaseType('Insecure');
}
/**
* Determines if the release is matches a type.
*
* @param string $type
* The release type.
*
* @return bool
* TRUE if the release matches the type, or FALSE otherwise.
*/
private function isReleaseType(string $type): bool {
return $this->releaseTypes && in_array($type, $this->releaseTypes, TRUE);
}
/**
* Determines if the release is published.
*
* @return bool
* TRUE if the release is published, or FALSE otherwise.
*/
public function isPublished(): bool {
return $this->published;
}
/**
* Determines whether release is compatible the site's version of Drupal core.
*
* @return bool|null
* Whether the release is compatible or NULL if no data is set.
*/
public function isCoreCompatible(): ?bool {
return $this->coreCompatible;
}
/**
* Gets the core compatibility message for the site's version of Drupal core.
*
* @return string|null
* The core compatibility message or NULL if none is available.
*/
public function getCoreCompatibilityMessage(): ?string {
return $this->coreCompatibilityMessage;
}
/**
* Gets the download URL of the release.
*
* @return string|null
* The download URL or NULL if none is available.
*/
public function getDownloadUrl(): ?string {
return $this->downloadUrl;
}
/**
* Gets the URL of the release.
*
* @return string
* The URL of the release.
*/
public function getReleaseUrl(): string {
return $this->releaseUrl;
}
}