2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Middleware;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
use Event;
|
2016-10-16 18:16:15 +08:00
|
|
|
use App\Models\Player;
|
2016-08-30 08:55:02 +08:00
|
|
|
use App\Events\CheckPlayerExists;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-12-17 22:45:08 +08:00
|
|
|
class CheckPlayerExist
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-28 10:05:21 +08:00
|
|
|
public function handle($request, \Closure $next)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-11-15 14:00:11 +08:00
|
|
|
if ($request->has('pid') && $request->isMethod('post')) {
|
|
|
|
if (is_null(Player::find($request->input('pid')))) {
|
2019-04-23 19:14:41 +08:00
|
|
|
return json(trans('general.unexistent-player'), 1);
|
2017-11-15 14:00:11 +08:00
|
|
|
} else {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
if (stripos($request->getUri(), '.json') != false) {
|
|
|
|
preg_match('/\/([^\/]*)\.json/', $request->getUri(), $matches);
|
|
|
|
} else {
|
|
|
|
preg_match('/\/([^\/]*)\.png/', $request->getUri(), $matches);
|
|
|
|
}
|
|
|
|
|
2016-08-29 15:10:51 +08:00
|
|
|
$player_name = urldecode($matches[1]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-10-29 09:19:02 +08:00
|
|
|
$responses = event(new CheckPlayerExists($player_name));
|
2016-11-06 10:58:01 +08:00
|
|
|
|
|
|
|
foreach ($responses as $r) {
|
2019-04-06 23:16:14 +08:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($r) {
|
|
|
|
return $next($request);
|
2019-04-06 23:16:14 +08:00
|
|
|
}
|
|
|
|
// @codeCoverageIgnoreEnd
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:16:51 +08:00
|
|
|
if (! Player::where('name', $player_name)->get()->isEmpty()) {
|
2016-11-06 10:58:01 +08:00
|
|
|
return $next($request);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-11-06 10:58:01 +08:00
|
|
|
|
2018-07-22 16:42:58 +08:00
|
|
|
if (option('return_204_when_notfound')) {
|
|
|
|
return response('', 204, [
|
2019-03-02 22:58:37 +08:00
|
|
|
'Cache-Control' => 'public, max-age='.option('cache_expire_time'),
|
2018-07-22 16:42:58 +08:00
|
|
|
]);
|
2016-11-06 10:58:01 +08:00
|
|
|
} else {
|
2017-10-29 09:19:02 +08:00
|
|
|
return abort(404, trans('general.unexistent-player'));
|
2016-11-06 10:58:01 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|