blessing-skin-server/app/Http/Middleware/CheckPlayerExist.php

53 lines
1.5 KiB
PHP
Raw Normal View History

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;
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
{
if ($request->has('pid') && $request->isMethod('post')) {
if (is_null(Player::find($request->input('pid')))) {
return response()->json([
'errno' => 1,
'msg' => trans('general.unexistent-player'),
]);
} 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);
}
$player_name = urldecode($matches[1]);
2016-07-21 22:01:57 +08:00
$responses = event(new CheckPlayerExists($player_name));
foreach ($responses as $r) {
if ($r) {
return $next($request);
} // @codeCoverageIgnore
2016-07-21 22:01:57 +08:00
}
if (! Player::where('player_name', $player_name)->get()->isEmpty()) {
return $next($request);
}
if (option('return_204_when_notfound')) {
return response('', 204, [
'Cache-Control' => 'public, max-age='.option('cache_expire_time'),
]);
} else {
return abort(404, trans('general.unexistent-player'));
}
2016-07-21 22:01:57 +08:00
}
}