1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Services\Cache; |
4
|
|
|
|
5
|
|
|
use Aws\CloudFront\CloudFrontClient; |
6
|
|
|
use Illuminate\Config\Repository as Config; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
|
10
|
|
|
class CloudfrontCacheService |
11
|
|
|
{ |
12
|
|
|
protected $client = null; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Config |
16
|
|
|
*/ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
// Added for backwards compatibility. Should be removed in future releases. |
20
|
|
|
protected static $defaultRegion = 'us-east-1'; |
21
|
|
|
protected static $defaultSdkVersion = '2016-01-13'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public static function getSdkVersion() |
27
|
|
|
{ |
28
|
|
|
return config('services.cloudfront.sdk_version') ?? self::$defaultSdkVersion; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public static function getRegion() |
35
|
|
|
{ |
36
|
|
|
return config('services.cloudfront.region') ?? self::$defaultRegion; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return CloudFrontClient |
41
|
|
|
*/ |
42
|
|
|
public static function getClient() |
43
|
|
|
{ |
44
|
|
|
$cloudFront = new CloudFrontClient(array( |
45
|
|
|
'region' => self::getRegion(), |
46
|
|
|
'version' => self::getSdkVersion(), |
47
|
|
|
'credentials' => array( |
48
|
|
|
'key' => config('services.cloudfront.key'), |
49
|
|
|
'secret' => config('services.cloudfront.secret'), |
50
|
|
|
), |
51
|
|
|
)); |
52
|
|
|
|
53
|
|
|
return $cloudFront; |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Config $config |
59
|
|
|
*/ |
60
|
|
|
public function __construct(Config $config) |
61
|
|
|
{ |
62
|
|
|
$this->config = $config; |
63
|
|
|
|
64
|
|
|
$client = static::getClient(); |
65
|
|
|
if (is_object($client)) { |
66
|
|
|
$this->client = $client; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string[] $urls |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function invalidate($urls = ["/*"]) |
75
|
|
|
{ |
76
|
|
|
if (!$this->hasInProgressInvalidation()) { |
77
|
|
|
try { |
78
|
|
|
$this->createInvalidationRequest($urls); |
79
|
|
|
} catch (\Exception $e) { |
80
|
|
|
Log::debug('Cloudfront invalidation request failed'); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
Log::debug('Cloudfront : there are already some invalidations in progress'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
private function hasInProgressInvalidation() |
91
|
|
|
{ |
92
|
|
|
$list = $this->client->listInvalidations(array('DistributionId' => $this->config->get('services.cloudfront.distribution')))->get('InvalidationList'); |
93
|
|
|
if (isset($list['Items']) && !empty($list['Items'])) { |
94
|
|
|
return Collection::make($list['Items'])->where('Status', 'InProgress')->count() > 0; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $paths |
102
|
|
|
* @return \Aws\Result |
103
|
|
|
*/ |
104
|
|
|
private function createInvalidationRequest($paths = array()) |
105
|
|
|
{ |
106
|
|
|
if (is_object($this->client) && count($paths) > 0) { |
107
|
|
|
try { |
108
|
|
|
$result = $this->client->createInvalidation(array( |
109
|
|
|
'DistributionId' => $this->config->get('services.cloudfront.distribution'), |
110
|
|
|
'InvalidationBatch' => array( |
111
|
|
|
'Paths' => array( |
112
|
|
|
'Quantity' => count($paths), |
113
|
|
|
'Items' => $paths, |
114
|
|
|
), |
115
|
|
|
'CallerReference' => time(), |
116
|
|
|
), |
117
|
|
|
)); |
118
|
|
|
} catch (\Exception $e) { |
119
|
|
|
Log::debug('Cloudfront invalidation request failed'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $result; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|