Verified Commit fffe8a1e authored by Korko's avatar Korko
Browse files

Use service URLParser instead of custom homemade url parser

parent c79d3f5c
......@@ -2,11 +2,11 @@
namespace App\Console\Commands;
use App\Models\Participant;
use Arr;
use DrawHandler;
use Illuminate\Console\Command;
use DrawCrypt;
use URLParser;
class FixOrganizer extends Command
{
......@@ -33,7 +33,7 @@ class FixOrganizer extends Command
{
$this->setCryptKeyFromUrl($this->argument('url'));
$draw = Participant::findByDearSantaUrlOrFail($this->argument('url'))->draw;
$draw = URLParser::parseByName('dearsanta', $this->argument('url'))->participant->draw;
$draw->organizer->email = $this->argument('email');
$draw->organizer->save();
......
......@@ -65,20 +65,6 @@ class Participant extends Model
return $this->belongsToMany(Participant::class, 'exclusions', 'participant_id', 'exclusion_id');
}
public function scopeFindByDearSantaUrlOrFail($query, $url)
{
$route = app('router')
->getRoutes()
->getByName('dearsanta');
$request = app('request')
->create($url);
$hash = $route->bind($request)->participant;
return $query->findByHashOrFail($hash);
}
public function getExclusionsNamesAttribute()
{
return $this->exclusions->pluck('name', 'id')->all();
......
<?php
namespace App\Services;
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
class URLParser
{
public $router;
public $request;
public function __construct(Router $router, Request $request)
{
$this->router = $router;
$this->request = $request;
}
public function parseByName($routeName, $url)
{
$route = $this->router
->getRoutes()
->getByName($routeName);
$request = $this->request
->create($url);
$route->bind($request);
$this->router->substituteBindings($route);
$this->router->substituteImplicitBindings($route);
return $route;
}
}
\ No newline at end of file
......@@ -248,6 +248,7 @@ return [
'Storage' => Illuminate\Support\Facades\Storage::class,
'Str' => Illuminate\Support\Str::class,
'URL' => Illuminate\Support\Facades\URL::class,
'URLParser' => Facades\App\Services\URLParser::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
......
......@@ -70,11 +70,7 @@ it('stores database crypted and uncryptable entries', function ($participants) {
$link = $m->panelLink;
});
// Check data stored are decryptable
$path = parse_url($link, PHP_URL_PATH);
$pathTheorical = parse_url(route('organizerPanel', ['draw' => '%s']), PHP_URL_PATH);
$data = sscanf($path, $pathTheorical);
$draw = Draw::findByHashOrFail($data[0]);
$draw = URLParser::parseByName('organizerPanel', $link)->draw;
assertNotEquals(0, $draw->participants->count());
......
......@@ -9,6 +9,7 @@ use Crypt;
use Hashids;
use Illuminate\Support\Facades\URL;
use Mail;
use URLParser;
test('a participant can write to their santa', function () {
Mail::fake();
......@@ -33,7 +34,7 @@ test('a participant can write to their santa', function () {
// Check data stored are decryptable
$path = parse_url($link, PHP_URL_PATH);
$santaTheorical = Participant::findByDearSantaUrlOrFail($path);
$santaTheorical = URLParser::parseByName('dearsanta', $path)->participant;
assertEquals($santa['name'], $santaTheorical->santa->name);
assertEquals($santa['email'], $santaTheorical->santa->email);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment