Commit 40f46316 authored by Korko's avatar Korko
Browse files

PSR2 + Change App namespace + Add new validations

parent 1f1788ef
......@@ -3,3 +3,5 @@
Homestead.yaml
Homestead.json
.env
/app/Http/Controllers/RecoveryFormController.php
/resources/views/recoveryForm.blade.php
<?php
namespace App\Console;
namespace Korko\SecretSanta\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
......
<?php
namespace App\Events;
namespace Korko\SecretSanta\Events;
abstract class Event
{
......
<?php
namespace App\Exceptions;
namespace Korko\SecretSanta\Exceptions;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
......
<?php
namespace App\Http\Controllers;
namespace Korko\SecretSanta\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
......
<?php
namespace App\Http\Controllers;
namespace Korko\SecretSanta\Http\Controllers;
use App\Libs\Randomizer;
use Korko\SecretSanta\Http\Requests\RandomFormRequest;
use Korko\SecretSanta\Libs\Randomizer;
use Illuminate\Http\Request;
use Mail;
......@@ -26,12 +27,15 @@ class RandomFormController extends Controller
}
$hat = Randomizer::randomize($participants);
dd($participants, $hat);
foreach ($hat as $santaIdx => $targetName) {
$santa = $participants[$santaIdx];
// Mail::send('emails.secretsanta', ['name' => $santa['name'], 'secret' => $targetName], function ($m) use ($santa) {
// $m->to($santa['email'], $santa['name'])->subject("Soirée 'Secret Santa' du dimanche 20 décembre (2ème essai)");
// });
$santa = $participants[$santaIdx];
$content = str_replace(['{SANTA}', '{TARGET}'], [$santa['name'], $targetName], $request->input('content'));
Mail::raw($content, function ($m) use ($santa, $request) {
$m->to($santa['email'], $santa['name'])->subject($request->input('title'));
});
}
return 'Envoyé !';
......
<?php
namespace App\Http;
namespace Korko\SecretSanta\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
......@@ -13,11 +13,11 @@ class Kernel extends HttpKernel
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\KorkoSecretSanta\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\KorkoSecretSanta\Http\Middleware\VerifyCsrfToken::class,
];
/**
......
<?php
namespace App\Http\Middleware;
namespace Korko\SecretSanta\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter;
......
<?php
namespace App\Http\Middleware;
namespace Korko\SecretSanta\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
......
<?php
namespace App\Http\Requests;
namespace Korko\SecretSanta\Http\Requests;
use App\Http\Requests\Request;
use Korko\SecretSanta\Http\Requests\Request;
class RandomFormRequest extends Request
{
......@@ -23,22 +23,23 @@ class RandomFormRequest extends Request
*/
public function rules()
{
// TODO: Add new rules: arrayunique and fieldin:fieldName (fieldin only if defined?)
$rules = [
'name' => 'required|array|arrayunique',
'email' => 'array',
'number' => 'array',
'partner' => 'array'
];
$rules = [
'name' => 'required|arrayunique',
'title' => 'required|string',
'content' => 'required|contains:{TARGET}',
'email' => 'array',
'number' => 'array',
'partner' => 'array'
];
foreach ($this->request->get('name') as $key => $name) {
$rules += [
'email.'.$key => 'required_unless:number.'.$key.'|email'
'number.'.$key => 'required_unless:email.'.$key.'|numeric|regex:#336\d{8}',
'partner.'.$key => 'string|fieldin:name'
];
}
foreach ($this->request->get('name') as $key => $name) {
$rules += [
'email.'.$key => 'required_if:number.'.$key.',null|email',
'number.'.$key => 'required_if:email.'.$key.',null|numeric|regex:#336\d{8}',
'partner.'.$key => 'sometimes|string|fieldin:name'
];
}
return $rules;
return $rules;
}
}
<?php
namespace App\Http\Requests;
namespace Korko\SecretSanta\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
......
......@@ -13,6 +13,3 @@
Route::get('/', 'RandomFormController@view');
Route::post('/', 'RandomFormController@handle');
Route::get('/recovery', 'RecoveryFormController@view');
Route::post('/recovery', 'RecoveryFormController@handle');
<?php
namespace App\Jobs;
namespace Korko\SecretSanta\Jobs;
use Illuminate\Bus\Queueable;
......
<?php
namespace App\Libs;
namespace Korko\SecretSanta\Libs;
use Exception;
......
<?php
namespace App\Providers;
namespace Korko\SecretSanta\Providers;
use Illuminate\Support\ServiceProvider;
use Validator;
class AppServiceProvider extends ServiceProvider
{
......@@ -13,7 +14,29 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Validator::extend('arrayunique', function ($attribute, $value, $parameters, $validator) {
return is_array($value) && count($value) === count(array_unique($value));
});
Validator::extend('fieldin', function ($attribute, $value, $parameters, $validator) {
return (count($parameters) === 1 &&
isset($validator->getData()[$parameters[0]]) &&
is_array($validator->getData()[$parameters[0]]) &&
array_key_exists($value, $validator->getData()[$parameters[0]])
);
});
Validator::replacer('fieldin', function ($message, $attribute, $rule, $parameters) {
return str_replace(':other', $parameters[0], $message);
});
Validator::extend('contains', function ($attribute, $value, $parameters, $validator) {
return is_string($value) && array_reduce((array) $parameters, function ($return, $find) use ($value) {
return $return && strpos($value, $find) !== false;
}, true);
});
Validator::replacer('contains', function ($message, $attribute, $rule, $parameters) {
return str_replace(':values', implode(', ', $parameters), $message);
});
}
/**
......
<?php
namespace App\Providers;
namespace Korko\SecretSanta\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
......@@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
'KorkoSecretSanta\Events\SomeEvent' => [
'KorkoSecretSanta\Listeners\EventListener',
],
];
......
<?php
namespace App\Providers;
namespace Korko\SecretSanta\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
......@@ -14,7 +14,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
protected $namespace = 'KorkoSecretSanta\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
......
......@@ -28,17 +28,17 @@ $app = new Illuminate\Foundation\Application(
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
KorkoSecretSanta\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
KorkoSecretSanta\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
KorkoSecretSanta\Exceptions\Handler::class
);
/*
......
......@@ -19,7 +19,7 @@
"database"
],
"psr-4": {
"App\\": "app/"
"Korko\\SecretSanta\\": "app/"
}
},
"autoload-dev": {
......
......@@ -137,9 +137,9 @@ return [
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
KorkoSecretSanta\Providers\AppServiceProvider::class,
KorkoSecretSanta\Providers\EventServiceProvider::class,
KorkoSecretSanta\Providers\RouteServiceProvider::class,
],
......
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