|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: famoser |
|
5
|
|
|
* Date: 07/06/2016 |
|
6
|
|
|
* Time: 17:54 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Famoser\XKCDCache\Controllers; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use Famoser\XKCDCache\Controllers\Base\BaseController; |
|
14
|
|
|
use Famoser\XKCDCache\Entities\Comic; |
|
15
|
|
|
use Famoser\XKCDCache\Exceptions\ServerException; |
|
16
|
|
|
use Famoser\XKCDCache\Models\Response\RefreshResponse; |
|
17
|
|
|
use Famoser\XKCDCache\Models\Response\StatusResponse; |
|
18
|
|
|
use Famoser\XKCDCache\Models\XKCD\XKCDJson; |
|
19
|
|
|
use Famoser\XKCDCache\Types\DownloadStatus; |
|
20
|
|
|
use Famoser\XKCDCache\Types\ServerError; |
|
21
|
|
|
use Slim\Http\Request; |
|
22
|
|
|
use Slim\Http\Response; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* the api controller provides the two public api methods |
|
26
|
|
|
* |
|
27
|
|
|
* @package Famoser\XKCDCache\Controllers |
|
28
|
|
|
*/ |
|
29
|
|
|
class ApiController extends BaseController |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* returns the newest XKCD comic |
|
33
|
|
|
* @param $number |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
3 |
|
private function cacheComic($number) |
|
37
|
|
|
{ |
|
38
|
|
|
try { |
|
39
|
|
|
/* @var XKCDJson $myJsonObject */ |
|
40
|
3 |
|
$myJsonObject = $this->getXKCDService()->getComic($number); |
|
41
|
3 |
|
return $this->getCacheService()->persistComic($myJsonObject); |
|
42
|
|
|
} catch (Exception $ex) { |
|
43
|
|
|
$this->getLoggingService()->log("failed to cache comic: " . $ex); |
|
44
|
|
|
} |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* the newest comic contained in cache |
|
50
|
|
|
* |
|
51
|
|
|
* @return int |
|
52
|
|
|
* @throws ServerException |
|
53
|
|
|
* @throws ServerException |
|
54
|
|
|
*/ |
|
55
|
4 |
|
private function getNewestCacheNumber() |
|
56
|
|
|
{ |
|
57
|
4 |
|
$newestCache = $this->getCacheService()->getNewestComic(); |
|
58
|
4 |
|
if ($newestCache instanceof Comic) { |
|
59
|
1 |
|
$newestCachedComic = $newestCache->num; |
|
60
|
|
|
} else { |
|
61
|
4 |
|
$newestCachedComic = 0; |
|
62
|
|
|
} |
|
63
|
4 |
|
return $newestCachedComic; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* the newest comic available online |
|
68
|
|
|
* |
|
69
|
|
|
* @return int|bool |
|
70
|
|
|
* @throws ServerException |
|
71
|
|
|
* @throws ServerException |
|
72
|
|
|
*/ |
|
73
|
4 |
|
private function getNewestOnlineNumber() |
|
74
|
|
|
{ |
|
75
|
4 |
|
$newestOnline = $this->getXKCDService()->getNewestComic(); |
|
76
|
4 |
|
if ($newestOnline != null) { |
|
77
|
4 |
|
return $newestOnline->num; |
|
78
|
|
|
} else { |
|
79
|
|
|
$this->getLoggingService()->log("XKCD server not available"); |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* show basic info about this application |
|
86
|
|
|
* |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
* @param Response $response |
|
89
|
|
|
* @return Response |
|
90
|
|
|
* @throws ServerException |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function refresh(/** @scrutinizer ignore-unused */ Request $request, Response $response) |
|
93
|
|
|
{ |
|
94
|
3 |
|
return $this->doRefresh($response); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param Response $response |
|
99
|
|
|
* @param bool $noLimit |
|
100
|
|
|
* @return Response |
|
101
|
|
|
* @throws ServerException |
|
102
|
|
|
*/ |
|
103
|
3 |
|
protected function doRefresh(Response $response, $noLimit = false) |
|
104
|
|
|
{ |
|
105
|
3 |
|
$newestOnlineNumber = $this->getNewestOnlineNumber(); |
|
106
|
3 |
|
if ($newestOnlineNumber === false) { |
|
107
|
|
|
throw new ServerException(ServerError::XKCD_CONNECTION_FAILED); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
$newestCachedNumber = $this->getNewestCacheNumber(); |
|
111
|
|
|
|
|
112
|
3 |
|
$refreshCount = 0; |
|
113
|
3 |
|
if ($newestCachedNumber < $newestOnlineNumber) { |
|
114
|
3 |
|
$maxIterations = $this->getSettingService()->getMaxRefreshImages(); |
|
115
|
3 |
|
if ($noLimit) { |
|
116
|
|
|
$maxIterations = 1000; |
|
117
|
|
|
} |
|
118
|
3 |
|
$newestCachedNumber++; |
|
119
|
3 |
|
for (; $newestCachedNumber < $newestOnlineNumber && $maxIterations > 0; $newestCachedNumber++) { |
|
120
|
3 |
|
$maxIterations--; |
|
121
|
3 |
|
$refreshCount++; |
|
122
|
3 |
|
$this->cacheComic($newestCachedNumber); |
|
123
|
|
|
} |
|
124
|
3 |
|
$this->getCacheService()->createImageZip($newestCachedNumber); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
3 |
|
$refreshResponse = new RefreshResponse(); |
|
128
|
3 |
|
$refreshResponse->refresh_count = $refreshCount; |
|
129
|
3 |
|
$refreshResponse->refresh_cap = $this->getSettingService()->getMaxRefreshImages(); |
|
130
|
3 |
|
$refreshResponse->refresh_pending = $newestCachedNumber != $newestOnlineNumber; |
|
131
|
|
|
|
|
132
|
3 |
|
$failed = $this->getDatabaseService()->getFromDatabase(new Comic(), "status <> :status", ["status" => DownloadStatus::SUCCESSFUL]); |
|
133
|
3 |
|
foreach ($failed as $item) { |
|
134
|
|
|
$refreshResponse->missing_images[] = $item->num; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
3 |
|
$failed = $this->getDatabaseService()->getFromDatabase(new Comic(), "json IS NULL"); |
|
138
|
3 |
|
foreach ($failed as $item) { |
|
139
|
|
|
$refreshResponse->missing_json[] = $item->num; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
3 |
|
return $this->returnJson($response, $refreshResponse); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* show basic info about this application |
|
147
|
|
|
* |
|
148
|
|
|
* @param Request $request |
|
149
|
|
|
* @param Response $response |
|
150
|
|
|
* @return Response |
|
151
|
|
|
* @throws ServerException |
|
152
|
|
|
*/ |
|
153
|
|
|
public function refreshNoLimit(/** @scrutinizer ignore-unused */ Request $request, Response $response) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->doRefresh($response, true); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* show api info as json. Should be enough to configure the C# library |
|
160
|
|
|
* |
|
161
|
|
|
* @param Request $request |
|
162
|
|
|
* @param Response $response |
|
163
|
|
|
* @return Response |
|
164
|
|
|
* @throws ServerException |
|
165
|
|
|
*/ |
|
166
|
2 |
|
public function status(/** @scrutinizer ignore-unused */ Request $request, Response $response) |
|
167
|
|
|
{ |
|
168
|
2 |
|
$newestOnlineNumber = $this->getNewestOnlineNumber(); |
|
169
|
2 |
|
if ($newestOnlineNumber === false) { |
|
170
|
|
|
throw new ServerException(ServerError::XKCD_CONNECTION_FAILED); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
2 |
|
$newestCachedNumber = $this->getNewestCacheNumber(); |
|
174
|
|
|
|
|
175
|
2 |
|
$apiInfo = new StatusResponse(); |
|
176
|
2 |
|
$apiInfo->api_version = 1; |
|
177
|
2 |
|
$apiInfo->latest_image_cached = $newestCachedNumber; |
|
178
|
2 |
|
$apiInfo->latest_image_published = $newestOnlineNumber; |
|
179
|
2 |
|
$apiInfo->hot = $newestCachedNumber >= $newestOnlineNumber; |
|
180
|
|
|
|
|
181
|
2 |
|
return $this->returnJson($response, $apiInfo); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|