44
55use Aws \Credentials \Credentials ;
66use Aws \CloudFront \CloudFrontClient ;
7+ use Aws \Exception \AwsException ;
78use Meema \CloudFront \Contracts \CloudFront as CloudFrontInterface ;
89
910class CloudFront implements CloudFrontInterface
@@ -24,7 +25,7 @@ public function __construct(CloudFrontClient $client)
2425 {
2526 $ config = config ('cloudfront ' );
2627
27- $ this ->client = new CloudFrontClient ([
28+ $ this ->client = new $ client ([
2829 'version ' => $ config ['version ' ],
2930 'region ' => $ config ['region ' ],
3031 'credentials ' => new Credentials ($ config ['credentials ' ]['key ' ], $ config ['credentials ' ]['secret ' ]),
@@ -42,15 +43,95 @@ public function getClient(): CloudFrontClient
4243 }
4344
4445 /**
45- * Bust an item in CloudFront's cache.
46+ * Bust an item/s in CloudFront's cache.
4647 *
47- * @param string $id
48+ * @param array $items
49+ * @param int $quantity
50+ * @param string|null $distributionId
4851 * @return \Aws\Result
4952 */
50- public function invalidateCache ( string $ id )
53+ public function invalidate ( array $ items , int $ quantity = 1 , string $ distributionId = null )
5154 {
5255 return $ this ->client ->createInvalidation ([
53- 'Id ' => $ id ,
56+ 'DistributionId ' => $ distributionId ?? config ('cloudfront.distribution_id ' ),
57+ 'InvalidationBatch ' => [
58+ // CallerReference is a unique value that you provide and that CloudFront uses to prevent replays of your request.
59+ // You must provide a new caller reference value and other new information in the request for CloudFront to create a new invalidation request.
60+ 'CallerReference ' => microtime (true ),
61+ 'Paths ' => [
62+ 'Items ' => $ items ,
63+ 'Quantity ' => $ quantity ,
64+ ],
65+ ],
5466 ]);
5567 }
68+
69+ /**
70+ * Get a cache "invalidation".
71+ *
72+ * @param string $invalidationId
73+ * @param string|null $distributionId
74+ * @return string
75+ */
76+ public function getInvalidation (string $ invalidationId , string $ distributionId = null )
77+ {
78+ try {
79+ $ result = $ this ->client ->getInvalidation ([
80+ 'DistributionId ' => $ distributionId ?? config ('cloudfront.distribution_id ' ),
81+ 'Id ' => $ invalidationId ,
82+ ]);
83+
84+ $ message = '' ;
85+
86+ if (isset ($ result ['Invalidation ' ]['Status ' ])) {
87+ $ message = 'The status for the invalidation with the ID of ' .$ result ['Invalidation ' ]['Id ' ].' is ' .$ result ['Invalidation ' ]['Status ' ];
88+ }
89+
90+ if (isset ($ result ['@metadata ' ]['effectiveUri ' ])) {
91+ $ message .= ', and the effective URI is ' .$ result ['@metadata ' ]['effectiveUri ' ].'. ' ;
92+ } else {
93+ $ message = 'Error: Could not get information about ' .'the invalidation. The invalidation \'s status ' .'was not available. ' ;
94+ }
95+
96+ return $ message ;
97+ } catch (AwsException $ e ) {
98+ throw ($ e ->getAwsErrorMessage ());
99+ }
100+ }
101+
102+ /**
103+ * List all of the cache invalidations.
104+ *
105+ * @param string|null $distributionId
106+ * @return array
107+ */
108+ public function listInvalidations (string $ distributionId = null )
109+ {
110+ try {
111+ $ invalidations = $ this ->client ->listInvalidations ([
112+ 'DistributionId ' => $ distributionId ?? config ('cloudfront.distribution_id ' ) ,
113+ ]);
114+
115+ $ messages = [];
116+
117+ if (isset ($ invalidations ['InvalidationList ' ])) {
118+ if ($ invalidations ['InvalidationList ' ]['Quantity ' ] > 0 ) {
119+ foreach ($ invalidations ['InvalidationList ' ]['Items ' ] as $ invalidation ) {
120+ $ message = 'The invalidation with the ID of ' .$ invalidation ['Id ' ].' has the status of ' .$ invalidation ['Status ' ].'. ' ;
121+ $ messages [$ invalidation ['Id ' ]] = $ message ;
122+ }
123+ } else {
124+ $ message = 'Could not find any invalidations for the specified distribution. ' ;
125+ array_push ($ messages , $ message );
126+ }
127+ } else {
128+ $ message = 'Error: Could not get invalidation information. Could not get information about the specified distribution. ' ;
129+ array_push ($ messages , $ message );
130+ }
131+
132+ return $ messages ;
133+ } catch (AwsException $ e ) {
134+ throw ($ e ->getAwsErrorMessage ());
135+ }
136+ }
56137}
0 commit comments