1
|
|
|
<?php |
2
|
|
|
namespace Aoe\Imgix\Domain\Service; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2018 AOE GmbH <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use Aoe\Imgix\Domain\Model\ImagePurgeResult; |
29
|
|
|
use Aoe\Imgix\TYPO3\Configuration; |
30
|
|
|
use Aoe\Imgix\TYPO3\PurgeImgixCacheErrorHandler; |
31
|
|
|
use stdClass; |
32
|
|
|
|
33
|
|
|
class ImagePurgeService |
34
|
|
|
{ |
35
|
|
|
const IMG_PURGE_REQUEST_URL = 'https://api.imgix.com/v2/image/purger'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Configuration |
39
|
|
|
*/ |
40
|
|
|
private $configuration; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var PurgeImgixCacheErrorHandler |
44
|
|
|
*/ |
45
|
|
|
private $errorHandler; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Configuration $configuration |
49
|
|
|
* @param PurgeImgixCacheErrorHandler $errorHandler |
50
|
|
|
*/ |
51
|
3 |
|
public function __construct(Configuration $configuration, PurgeImgixCacheErrorHandler $errorHandler) |
52
|
|
|
{ |
53
|
3 |
|
$this->configuration = $configuration; |
54
|
3 |
|
$this->errorHandler = $errorHandler; |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return PurgeImgixCacheErrorHandler |
59
|
|
|
*/ |
60
|
|
|
public function getErrorHandler() |
61
|
|
|
{ |
62
|
|
|
return $this->errorHandler; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $imageUrl |
67
|
|
|
* @return ImagePurgeResult |
68
|
|
|
*/ |
69
|
3 |
|
public function purgeImgixCache($imageUrl) |
70
|
|
|
{ |
71
|
3 |
|
if (false === $this->configuration->isApiKeyConfigured()) { |
72
|
1 |
|
$this->errorHandler->handleCouldNotPurgeImgixCacheOnInvalidApiKey($imageUrl); |
73
|
1 |
|
$result = new ImagePurgeResult(); |
74
|
1 |
|
$result->markImagePurgeAsFailed(); |
75
|
1 |
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
$postRequest = new stdClass(); |
79
|
2 |
|
$postRequest->url = $imageUrl; |
80
|
|
|
|
81
|
2 |
|
$result = $this->doPostRequest($postRequest); |
82
|
2 |
|
if (false === $result->isSuccessful()) { |
83
|
1 |
|
$this->errorHandler->handleCouldNotPurgeImgixCacheOnFailedRestRequest($imageUrl, $result); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param stdClass $postRequest |
91
|
|
|
* @return ImagePurgeResult |
92
|
|
|
*/ |
93
|
|
|
protected function doPostRequest(stdClass $postRequest) |
94
|
|
|
{ |
95
|
|
|
$postJsonData = json_encode($postRequest); |
96
|
|
|
$ch = curl_init(); |
97
|
|
|
curl_setopt($ch, CURLOPT_URL, self::IMG_PURGE_REQUEST_URL); |
98
|
|
|
curl_setopt($ch, CURLOPT_USERNAME, $this->configuration->getApiKey()); |
99
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
100
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postJsonData); |
101
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
102
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($postJsonData)]); |
103
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
104
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
105
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
106
|
|
|
$response = curl_exec($ch); |
107
|
|
|
$responseInfo = curl_getinfo($ch); |
108
|
|
|
|
109
|
|
|
$result = new ImagePurgeResult(); |
110
|
|
|
if ($response === false || $responseInfo['http_code'] !== 200) { |
111
|
|
|
$result->markImagePurgeAsFailed(curl_error($ch), curl_errno($ch), $responseInfo['http_code']); |
112
|
|
|
} else { |
113
|
|
|
$result->markImagePurgeAsSuccessful(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
curl_close($ch); |
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|