Skip to content
This repository has been archived by the owner on Jul 17, 2021. It is now read-only.

Commit

Permalink
Add more tools
Browse files Browse the repository at this point in the history
  • Loading branch information
egorsmkv committed Sep 1, 2020
1 parent 71704ff commit 8fcb668
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 156 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
TELESCOPE_ENABLED=true
APP_URL=http://localhost:8000
OPCACHE_URL=http://localhost:8000

Expand Down
1 change: 1 addition & 0 deletions .env.prod.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_LOG_LEVEL=info
TELESCOPE_ENABLED=false
APP_URL=http://localhost:8000
OPCACHE_URL=http://localhost:8000

Expand Down
18 changes: 17 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@ tasks:
- ./vendor/bin/phpcs ./routes/*
ignore_error: true

laravel_microscope:
env:
TELESCOPE_ENABLED: false
cmds:
- php artisan check:events
- php artisan check:gates
- php artisan check:routes
- php artisan check:views
- php artisan check:imports
- php artisan check:dd
- php artisan check:compact
- php artisan check:blade_queries
- php artisan check:bad_practices
- php artisan check:psr4
ignore_error: true

code_style:
cmds:
- ./vendor/bin/psalm
- ./vendor/bin/phpmd app text phpmd-ruleset.xml
- task: phpcs
- php artisan code:analyse
- php artisan check:all
- task: laravel_microscope
ignore_error: true

pre_init:
Expand Down
14 changes: 1 addition & 13 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
Expand All @@ -16,25 +15,14 @@ class Kernel extends ConsoleKernel
Commands\CreateUser::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}

/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
/** @noinspection PhpIncludeInspection */
require base_path('routes/console.php');
}
}
28 changes: 0 additions & 28 deletions app/Domains/Http/Jobs/RespondWithJsonErrorJob.php

This file was deleted.

32 changes: 0 additions & 32 deletions app/Domains/Http/Jobs/RespondWithJsonJob.php

This file was deleted.

18 changes: 17 additions & 1 deletion app/Domains/Http/Jobs/RespondWithViewJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Domains\Http\Jobs;

use Illuminate\Http\Response;
use Lucid\Foundation\Job;
use Illuminate\Routing\ResponseFactory;

Expand All @@ -12,14 +13,29 @@ class RespondWithViewJob extends Job
protected $headers;
protected $template;

public function __construct($template, $data = [], $status = 200, array $headers = [])
/**
* RespondWithViewJob constructor.
*
* @param string $template
* @param array $data
* @param int $status
* @param array $headers
*/
public function __construct(string $template, $data = [], $status = 200, array $headers = [])
{
$this->template = $template;
$this->data = $data;
$this->status = $status;
$this->headers = $headers;
}

/**
* Handle this job
*
* @param ResponseFactory $factory
*
* @return Response
*/
public function handle(ResponseFactory $factory)
{
return $factory->view($this->template, $this->data, $this->status, $this->headers);
Expand Down
42 changes: 26 additions & 16 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Http\Controllers\ErrorsController;
use Lukasoppermann\Httpstatus\Httpstatus;

class Handler extends ExceptionHandler
{
Expand All @@ -20,11 +22,11 @@ class Handler extends ExceptionHandler
*/
protected $dontReport = [
AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
TokenMismatchException::class,
ValidationException::class,
];

/**
Expand All @@ -48,7 +50,7 @@ public function report(Exception $exception)
/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param mixed $request
* @param Exception $e
*
* @return mixed
Expand All @@ -58,11 +60,13 @@ public function render($request, Exception $e)
$requestURI = $request->getRequestUri();

if ($e instanceof HttpException) {
$code = $e->getStatusCode();
$code = (string)$e->getStatusCode();
$message = (new Httpstatus())->getReasonPhrase($code);

$parameters = [
'data' => [
'code' => $code,
'message' => Response::$statusTexts[$code],
'message' => $message,
'version' => 'unknown'
]
];
Expand Down Expand Up @@ -90,16 +94,22 @@ public function render($request, Exception $e)
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param Request $request
* @param mixed $request
* @param AuthenticationException $exception
*
* @return mixed
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return app()->call('App\Http\Controllers\ErrorsController@loginRequired');
}
$parameters = [
'data' => [
'code' => 401,
'message' => $exception->getMessage(),
'url' => $request->getUri(),
'version' => 'unknown'
]
];

return redirect()->guest('login');
return app()->call('App\Http\Controllers\ErrorsController@process', $parameters);
}
}
21 changes: 14 additions & 7 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Lucid\Foundation\Http\Controller;

class AuthController extends Controller
Expand All @@ -19,13 +20,15 @@ public function __construct()
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function login()
{
$credentials = request(['email', 'password']);

if (! $token = auth()->attempt($credentials)) {
$token = auth()->attempt($credentials);

if (!$token) {
return app()->call('App\Http\Controllers\ErrorsController@loginRequired');
}

Expand All @@ -35,7 +38,7 @@ public function login()
/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function logout()
{
Expand All @@ -47,26 +50,30 @@ public function logout()
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function refresh()
{
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
return $this->respondWithToken(auth()->refresh());
}

/**
* Get the token array structure.
*
* @param string $token
* @param mixed $token
*
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
protected function respondWithToken($token)
{
// TTL is one hour
$ttl = 3600;

return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
'expires_in' => $ttl
]);
}
}
23 changes: 9 additions & 14 deletions app/Http/Helpers/PrettyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
use EllipseSynergie\ApiResponse\AbstractResponse;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Contracts\Validation\Validator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\TransformerAbstract;

class PrettyResponse extends AbstractResponse
{
/**
* Respond with an array.
*
* @param array $array
* @param array $headers
* @param int $json_options @link http://php.net/manual/en/function.json-encode.php
* @param int $json_options
*
* @return mixed
*
* @SuppressWarnings(PHPMD)
*
* @link http://php.net/manual/en/function.json-encode.php
*/
public function withArray(array $array, array $headers = [], $json_options = 0)
{
Expand All @@ -35,11 +40,12 @@ public function withArray(array $array, array $headers = [], $json_options = 0)
* @param callable|TransformerAbstract $transformer
* @param string|null $resourceKey
* @param array $meta
*
* @return ResponseFactory
*/
public function withPaginator(LengthAwarePaginator $paginator, $transformer, $resourceKey = null, $meta = [])
{
$queryParams = array_diff_key($_GET, array_flip(['page']));
$queryParams = array_diff_key(request()->all(), array_flip(['page']));
$paginator->appends($queryParams);

$resource = new Collection($paginator->items(), $transformer, $resourceKey);
Expand All @@ -53,15 +59,4 @@ public function withPaginator(LengthAwarePaginator $paginator, $transformer, $re

return $this->withArray($rootScope->toArray());
}

/**
* Generates a Response with a 400 HTTP header and a given message from validator
*
* @param Validator $validator
* @return ResponseFactory
*/
public function errorWrongArgsValidator(Validator $validator)
{
return $this->errorWrongArgs($validator->getMessageBag()->toArray());
}
}
Loading

0 comments on commit 8fcb668

Please sign in to comment.