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 $paths |
||
49 | * @param string|null $distributionId |
||
50 | * @return \Aws\Result |
||
51 | */ |
||
52 | public function invalidate($paths, string $distributionId = null) |
||
53 | { |
||
54 | if (is_string($paths)) { |
||
55 | $arr[] = $paths; |
||
56 | $paths = $arr; |
||
57 | } |
||
58 | |||
59 | return $this->client->createInvalidation([ |
||
60 | 'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'), |
||
61 | 'InvalidationBatch' => [ |
||
62 | // CallerReference is a unique value that you provide and that CloudFront uses to prevent replays of your request. |
||
63 | // You must provide a new caller reference value and other new information in the request for CloudFront to create a new invalidation request. |
||
64 | 'CallerReference' => microtime(true), |
||
65 | 'Paths' => [ |
||
66 | 'Items' => $paths, |
||
67 | 'Quantity' => count($paths), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
68 | ], |
||
69 | ], |
||
70 | ]); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Remove every item out of your CloudFront distribution. |
||
75 | * |
||
76 | * @param string|null $distributionId |
||
77 | * @return \Aws\Result |
||
78 | */ |
||
79 | public function reset(string $distributionId = null) |
||
80 | { |
||
81 | return $this->invalidate('/*', $distributionId ?? config('cloudfront.distribution_id')); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get a cache "invalidation". |
||
86 | * |
||
87 | * @param string $invalidationId |
||
88 | * @param string|null $distributionId |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getInvalidation(string $invalidationId, string $distributionId = null) |
||
92 | { |
||
93 | try { |
||
94 | $result = $this->client->getInvalidation([ |
||
95 | 'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'), |
||
96 | 'Id' => $invalidationId, |
||
97 | ]); |
||
98 | |||
99 | $message = ''; |
||
100 | |||
101 | if (isset($result['Invalidation']['Status'])) { |
||
102 | $message = 'The status for the invalidation with the ID of '.$result['Invalidation']['Id'].' is '.$result['Invalidation']['Status']; |
||
103 | } |
||
104 | |||
105 | if (isset($result['@metadata']['effectiveUri'])) { |
||
106 | $message .= ', and the effective URI is '.$result['@metadata']['effectiveUri'].'.'; |
||
107 | } else { |
||
108 | $message = 'Error: Could not get information about '.'the invalidation. The invalidation\'s status '.'was not available.'; |
||
109 | } |
||
110 | |||
111 | return $message; |
||
0 ignored issues
–
show
The expression
return $message returns the type string which is incompatible with the return type mandated by Meema\CloudFront\Contrac...ront::getInvalidation() of Aws\Result .
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
![]() |
|||
112 | } catch (AwsException $e) { |
||
113 | throw($e->getAwsErrorMessage()); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * List all of the cache invalidations. |
||
119 | * |
||
120 | * @param string|null $distributionId |
||
121 | * @return array |
||
122 | */ |
||
123 | public function listInvalidations(string $distributionId = null) |
||
124 | { |
||
125 | try { |
||
126 | $invalidations = $this->client->listInvalidations([ |
||
127 | 'DistributionId' => $distributionId ?? config('cloudfront.distribution_id'), |
||
128 | ]); |
||
129 | |||
130 | $messages = []; |
||
131 | |||
132 | if (isset($invalidations['InvalidationList'])) { |
||
133 | if ($invalidations['InvalidationList']['Quantity'] > 0) { |
||
134 | foreach ($invalidations['InvalidationList']['Items'] as $invalidation) { |
||
135 | $message = 'The invalidation with the ID of '.$invalidation['Id'].' has the status of '.$invalidation['Status'].'.'; |
||
136 | $messages[$invalidation['Id']] = $message; |
||
137 | } |
||
138 | } else { |
||
139 | $message = 'Could not find any invalidations for the specified distribution.'; |
||
140 | array_push($messages, $message); |
||
141 | } |
||
142 | } else { |
||
143 | $message = 'Error: Could not get invalidation information. Could not get information about the specified distribution.'; |
||
144 | array_push($messages, $message); |
||
145 | } |
||
146 | |||
147 | return $messages; |
||
148 | } catch (AwsException $e) { |
||
149 | throw($e->getAwsErrorMessage()); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 |