Update middleware code

This commit is contained in:
Dane Everitt 2018-09-03 15:17:53 -07:00
parent 9706a8dc34
commit fd49e524c8
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 9 additions and 33 deletions

View file

@ -1,19 +1,10 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Http\Middleware; namespace Pterodactyl\Http\Middleware;
use Closure; use Closure;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Contracts\Config\Repository;
class LanguageMiddleware class LanguageMiddleware
{ {
@ -22,25 +13,18 @@ class LanguageMiddleware
*/ */
private $app; private $app;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
private $config;
/** /**
* LanguageMiddleware constructor. * LanguageMiddleware constructor.
* *
* @param \Illuminate\Foundation\Application $app * @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $config
*/ */
public function __construct(Application $app, Repository $config) public function __construct(Application $app)
{ {
$this->app = $app; $this->app = $app;
$this->config = $config;
} }
/** /**
* Handle an incoming request. * Handle an incoming request and set the user's preferred language.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Closure $next * @param \Closure $next
@ -48,7 +32,7 @@ class LanguageMiddleware
*/ */
public function handle(Request $request, Closure $next) public function handle(Request $request, Closure $next)
{ {
$this->app->setLocale($request->user()->language ?? $this->config->get('app.locale', 'en')); $this->app->setLocale($request->user()->language ?? config('app.locale', 'en'));
return $next($request); return $next($request);
} }

View file

@ -3,10 +3,9 @@
namespace Tests\Unit\Http\Middleware; namespace Tests\Unit\Http\Middleware;
use Mockery as m; use Mockery as m;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Http\Middleware\LanguageMiddleware;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Illuminate\Foundation\Application;
use Pterodactyl\Http\Middleware\LanguageMiddleware;
class LanguageMiddlewareTest extends MiddlewareTestCase class LanguageMiddlewareTest extends MiddlewareTestCase
{ {
@ -15,11 +14,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
*/ */
private $appMock; private $appMock;
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
private $config;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -28,7 +22,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
parent::setUp(); parent::setUp();
$this->appMock = m::mock(Application::class); $this->appMock = m::mock(Application::class);
$this->config = m::mock(Repository::class);
} }
/** /**
@ -37,8 +30,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
public function testLanguageIsSetForGuest() public function testLanguageIsSetForGuest()
{ {
$this->request->shouldReceive('user')->withNoArgs()->andReturnNull(); $this->request->shouldReceive('user')->withNoArgs()->andReturnNull();
$this->config->shouldReceive('get')->with('app.locale', 'en')->once()->andReturn('en');
$this->appMock->shouldReceive('setLocale')->with('en')->once()->andReturnNull(); $this->appMock->shouldReceive('setLocale')->with('en')->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
@ -47,7 +38,8 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
/** /**
* Test that a language is defined via the middleware for a user. * Test that a language is defined via the middleware for a user.
*/ */
public function testLanguageIsSetWithAuthenticatedUser() { public function testLanguageIsSetWithAuthenticatedUser()
{
$user = factory(User::class)->make(['language' => 'de']); $user = factory(User::class)->make(['language' => 'de']);
$this->request->shouldReceive('user')->withNoArgs()->andReturn($user); $this->request->shouldReceive('user')->withNoArgs()->andReturn($user);
@ -63,6 +55,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
*/ */
private function getMiddleware(): LanguageMiddleware private function getMiddleware(): LanguageMiddleware
{ {
return new LanguageMiddleware($this->appMock, $this->config); return new LanguageMiddleware($this->appMock);
} }
} }