1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meema\CloudFront; |
4
|
|
|
|
5
|
|
|
use Aws\CloudFront\CloudFrontClient; |
6
|
|
|
use Aws\Credentials\Credentials; |
7
|
|
|
use Aws\Exception\AwsException; |
8
|
|
|
use Meema\CloudFront\Contracts\CloudFront as CloudFrontInterface; |
9
|
|
|
|
10
|
|
|
class CloudFront implements CloudFrontInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Client instance of CloudFront. |
14
|
|
|
* |
15
|
|
|
* @var \Aws\CloudFront\CloudFrontClient |
16
|
|
|
*/ |
17
|
|
|
protected CloudFrontClient $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Construct converter. |
21
|
|
|
* |
22
|
|
|
* @param \Aws\CloudFront\CloudFrontClient $client |
23
|
|
|
*/ |
24
|
|
|
public function __construct(CloudFrontClient $client) |
25
|
|
|
{ |
26
|
|
|
$config = config('cloudfront'); |
27
|
|
|
|
28
|
|
|
$this->client = new $client([ |
29
|
|
|
'version' => $config['version'], |
30
|
|
|
'region' => $config['region'], |
31
|
|
|
'credentials' => new Credentials($config['credentials']['key'], $config['credentials']['secret']), |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the CloudFront Client. |
37
|
|
|
* |
38
|
|
|
* @return \Aws\CloudFront\CloudFrontClient |
39
|
|
|
*/ |
40
|
|
|
public function getClient(): CloudFrontClient |
41
|
|
|
{ |
42
|
|
|
return $this->client; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Bust an item/s in CloudFront's cache. |
47
|
|
|
* |
48
|
|
|
* @param array|string $items |
49
|
|
|
* @param string|null $distributionId |
50
|
|
|
* @return \Aws\Result |
51
|
|
|
*/ |
52
|
|
|
public function invalidate($items, string $distributionId = null) |
53
|
|
|
{ |
54
|
|
|
if (is_string($items)) { |
55
|
|
|
$items[] = $items; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->client->createInvalidation([ |
59
|
|
|
'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'), |
60
|
|
|
'InvalidationBatch' => [ |
61
|
|
|
// CallerReference is a unique value that you provide and that CloudFront uses to prevent replays of your request. |
62
|
|
|
// You must provide a new caller reference value and other new information in the request for CloudFront to create a new invalidation request. |
63
|
|
|
'CallerReference' => microtime(true), |
64
|
|
|
'Paths' => [ |
65
|
|
|
'Items' => $items, |
66
|
|
|
'Quantity' => count($items), |
|
|
|
|
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Remove every item out of your CloudFront distribution. |
74
|
|
|
* |
75
|
|
|
* @param string|null $distributionId |
76
|
|
|
* @return \Aws\Result |
77
|
|
|
*/ |
78
|
|
|
public function reset(string $distributionId = null) |
79
|
|
|
{ |
80
|
|
|
return $this->invalidate('/*', $distributionId ?? config('cloudfront.distribution_id')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get a cache "invalidation". |
85
|
|
|
* |
86
|
|
|
* @param string $invalidationId |
87
|
|
|
* @param string|null $distributionId |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getInvalidation(string $invalidationId, string $distributionId = null) |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$result = $this->client->getInvalidation([ |
94
|
|
|
'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'), |
95
|
|
|
'Id' => $invalidationId, |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$message = ''; |
99
|
|
|
|
100
|
|
|
if (isset($result['Invalidation']['Status'])) { |
101
|
|
|
$message = 'The status for the invalidation with the ID of '.$result['Invalidation']['Id'].' is '.$result['Invalidation']['Status']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($result['@metadata']['effectiveUri'])) { |
105
|
|
|
$message .= ', and the effective URI is '.$result['@metadata']['effectiveUri'].'.'; |
106
|
|
|
} else { |
107
|
|
|
$message = 'Error: Could not get information about '.'the invalidation. The invalidation\'s status '.'was not available.'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $message; |
|
|
|
|
111
|
|
|
} catch (AwsException $e) { |
112
|
|
|
throw($e->getAwsErrorMessage()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* List all of the cache invalidations. |
118
|
|
|
* |
119
|
|
|
* @param string|null $distributionId |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function listInvalidations(string $distributionId = null) |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
|
|
$invalidations = $this->client->listInvalidations([ |
126
|
|
|
'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'), |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
$messages = []; |
130
|
|
|
|
131
|
|
|
if (isset($invalidations['InvalidationList'])) { |
132
|
|
|
if ($invalidations['InvalidationList']['Quantity'] > 0) { |
133
|
|
|
foreach ($invalidations['InvalidationList']['Items'] as $invalidation) { |
134
|
|
|
$message = 'The invalidation with the ID of '.$invalidation['Id'].' has the status of '.$invalidation['Status'].'.'; |
135
|
|
|
$messages[$invalidation['Id']] = $message; |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$message = 'Could not find any invalidations for the specified distribution.'; |
139
|
|
|
array_push($messages, $message); |
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
$message = 'Error: Could not get invalidation information. Could not get information about the specified distribution.'; |
143
|
|
|
array_push($messages, $message); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $messages; |
147
|
|
|
} catch (AwsException $e) { |
148
|
|
|
throw($e->getAwsErrorMessage()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|