From c6ffadae35360c5944c14b435b23efabbe2f1de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 23 Jul 2021 12:06:08 +0200 Subject: [PATCH] phpcs confirmity --- app/Console/Commands/CreateUser.php | 11 +---- app/Console/Kernel.php | 9 ++-- app/Http/Controllers/Controller.php | 4 +- app/Http/Controllers/LoginController.php | 18 +++++--- app/Http/Controllers/TicketController.php | 3 +- app/Http/Kernel.php | 3 +- app/Http/Middleware/EncryptCookies.php | 1 + .../PreventRequestsDuringMaintenance.php | 1 + app/Http/Middleware/TrimStrings.php | 1 + app/Http/Middleware/TrustProxies.php | 1 + app/Http/Middleware/VerifyCsrfToken.php | 1 + app/Lib/Utils.php | 1 + app/Mail/NewTicket.php | 6 ++- app/Mail/TicketUpdate.php | 3 +- app/Models/Message.php | 27 +----------- app/Models/Ticket.php | 43 +++++++++++++++++++ app/Providers/RouteServiceProvider.php | 9 ---- config/app.php | 3 +- config/auth.php | 7 +-- config/cache.php | 6 +-- config/database.php | 2 +- config/filesystems.php | 2 +- config/session.php | 2 +- config/trustedproxy.php | 5 +-- database/factories/UserFactory.php | 1 + routes/channels.php | 2 +- tests/CreatesApplication.php | 2 +- 27 files changed, 90 insertions(+), 84 deletions(-) diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php index 8003d3b..b2f2617 100644 --- a/app/Console/Commands/CreateUser.php +++ b/app/Console/Commands/CreateUser.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Hash; class CreateUser extends Command { + /** * The name and signature of the console command. * @@ -25,16 +26,6 @@ class CreateUser extends Command */ protected $description = 'Create a user'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. * diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 69914e9..7c889cb 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -7,14 +7,13 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { + /** * The Artisan commands provided by your application. * * @var array */ - protected $commands = [ - // - ]; + protected $commands = []; /** * Define the application's command schedule. @@ -24,7 +23,6 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire')->hourly(); } /** @@ -34,8 +32,7 @@ class Kernel extends ConsoleKernel */ protected function commands() { - $this->load(__DIR__.'/Commands'); - + $this->load(__DIR__ . '/Commands'); require base_path('routes/console.php'); } } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index a0a2a8a..ce1176d 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -9,5 +9,7 @@ use Illuminate\Routing\Controller as BaseController; class Controller extends BaseController { - use AuthorizesRequests, DispatchesJobs, ValidatesRequests; + use AuthorizesRequests; + use DispatchesJobs; + use ValidatesRequests; } diff --git a/app/Http/Controllers/LoginController.php b/app/Http/Controllers/LoginController.php index 12f4c27..38af354 100644 --- a/app/Http/Controllers/LoginController.php +++ b/app/Http/Controllers/LoginController.php @@ -7,19 +7,27 @@ use Illuminate\Support\Facades\Auth; class LoginController extends Controller { - public function index() { + public function index() + { return view('login'); } - public function authenticate(Request $request) { - if (Auth::attempt(["email" => $request->email, "password" => $request->password], $request->remember !== null ? true : false)) { + public function authenticate(Request $request) + { + if ( + Auth::attempt([ + "email" => $request->email, "password" => $request->password + ], $request->remember !== null ? true : false) + ) { return redirect()->route('home'); } else { - return back()->withInput($request->except("password"))->withErrors("Les identifiants entrés sont incorrects, veuillez réessayer."); + return back()->withInput($request->except("password")) + ->withErrors("Les identifiants entrés sont incorrects, veuillez réessayer."); } } - public function logout() { + public function logout() + { Auth::logout(); return redirect()->route('home'); } diff --git a/app/Http/Controllers/TicketController.php b/app/Http/Controllers/TicketController.php index 3c1c2bb..9104e69 100644 --- a/app/Http/Controllers/TicketController.php +++ b/app/Http/Controllers/TicketController.php @@ -249,7 +249,8 @@ class TicketController extends Controller if ($request->file_message) { $siteSlug = Str::slug($ticket->site->name); $fileMessage = $request->file_message; - $fileName = $siteSlug . '-ticket-' . $ticket->id . '-message-' . $message->id . '.' . $fileMessage->extension(); + $fileName = $siteSlug . '-ticket-' . $ticket->id . + '-message-' . $message->id . '.' . $fileMessage->extension(); $fileMessage->move(public_path('fichiers/' . $siteSlug . '/' . $ticket->id . '/'), $fileName); $filePath = 'fichiers/' . $siteSlug . '/' . $ticket->id . '/' . $fileName; $message->file = $filePath; diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 4efdce4..6ce05ef 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { + /** * The application's global HTTP middleware stack. * @@ -15,7 +16,6 @@ class Kernel extends HttpKernel */ protected $middleware = [ \Illuminate\Session\Middleware\StartSession::class, - // \App\Http\Middleware\TrustHosts::class, \App\Http\Middleware\TrustProxies::class, \Fruitcake\Cors\HandleCors::class, \App\Http\Middleware\PreventRequestsDuringMaintenance::class, @@ -33,7 +33,6 @@ class Kernel extends HttpKernel 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, - // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, diff --git a/app/Http/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php index 033136a..5cf30b3 100644 --- a/app/Http/Middleware/EncryptCookies.php +++ b/app/Http/Middleware/EncryptCookies.php @@ -6,6 +6,7 @@ use Illuminate\Cookie\Middleware\EncryptCookies as Middleware; class EncryptCookies extends Middleware { + /** * The names of the cookies that should not be encrypted. * diff --git a/app/Http/Middleware/PreventRequestsDuringMaintenance.php b/app/Http/Middleware/PreventRequestsDuringMaintenance.php index e4956d0..2a8072e 100644 --- a/app/Http/Middleware/PreventRequestsDuringMaintenance.php +++ b/app/Http/Middleware/PreventRequestsDuringMaintenance.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Mi class PreventRequestsDuringMaintenance extends Middleware { + /** * The URIs that should be reachable while maintenance mode is enabled. * diff --git a/app/Http/Middleware/TrimStrings.php b/app/Http/Middleware/TrimStrings.php index 5a50e7b..7eb899a 100644 --- a/app/Http/Middleware/TrimStrings.php +++ b/app/Http/Middleware/TrimStrings.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware; class TrimStrings extends Middleware { + /** * The names of the attributes that should not be trimmed. * diff --git a/app/Http/Middleware/TrustProxies.php b/app/Http/Middleware/TrustProxies.php index 14befce..919aec9 100644 --- a/app/Http/Middleware/TrustProxies.php +++ b/app/Http/Middleware/TrustProxies.php @@ -7,6 +7,7 @@ use Illuminate\Http\Request; class TrustProxies extends Middleware { + /** * The trusted proxies for this application. * diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 0c13b85..680465e 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware; class VerifyCsrfToken extends Middleware { + /** * The URIs that should be excluded from CSRF verification. * diff --git a/app/Lib/Utils.php b/app/Lib/Utils.php index d2f3bf7..419eaee 100644 --- a/app/Lib/Utils.php +++ b/app/Lib/Utils.php @@ -9,6 +9,7 @@ class Utils $text = e($text); // url replace and email replace $text = preg_replace_callback( + // phpcs:ignore Generic.Files.LineLength.TooLong '/(?P[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,})|(?P@\w*)|(?P(?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*))/', function ($matches) { $match = collect($matches)->first() ?? ''; diff --git a/app/Mail/NewTicket.php b/app/Mail/NewTicket.php index 6e17361..1188e29 100644 --- a/app/Mail/NewTicket.php +++ b/app/Mail/NewTicket.php @@ -9,7 +9,8 @@ use Illuminate\Queue\SerializesModels; class NewTicket extends Mailable { - use Queueable, SerializesModels; + use Queueable; + use SerializesModels; private $ticket; @@ -31,6 +32,7 @@ class NewTicket extends Mailable public function build() { $ticket = $this->ticket; - return $this->subject("[SupportWeb] Nouveau ticket n°" . $ticket->id)->view('emails.new-ticket', compact('ticket')); + return $this->subject("[SupportWeb] Nouveau ticket n°" . $ticket->id) + ->view('emails.new-ticket', compact('ticket')); } } diff --git a/app/Mail/TicketUpdate.php b/app/Mail/TicketUpdate.php index 13b89a7..253647c 100644 --- a/app/Mail/TicketUpdate.php +++ b/app/Mail/TicketUpdate.php @@ -40,6 +40,7 @@ class TicketUpdate extends Mailable { $ticket = $this->ticket; $corpsMaj = Utils::plainToHtml($this->corpsMaj); - return $this->subject("[SupportWeb] Le ticket n°" . $ticket->id . " à été mis à jour")->view('emails.ticket-update', compact('ticket', 'corpsMaj')); + return $this->subject("[SupportWeb] Le ticket n°" . $ticket->id . " à été mis à jour") + ->view('emails.ticket-update', compact('ticket', 'corpsMaj')); } } diff --git a/app/Models/Message.php b/app/Models/Message.php index 3af559f..978a728 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -35,32 +35,7 @@ class Message extends Model ]) )); } - // find @user + send mail si un seul user est trouvé - $usersNotified = collect([]); - $matches = []; - $ticket->load('site'); - \preg_match_all('/@\w*/', $message->message, $matches); - $matches = collect($matches[0] ?? [])->unique(); - foreach ($matches as $pseudo_usertag) { - try { - $user = User::where('name', 'LIKE', '%' . ltrim($pseudo_usertag, '@') . '%') - ->where('email', '!=', $ticket->user->email) - ->where(function ($q) use ($ticket) { - $q->whereHas('sites', function ($q) use ($ticket) { - $q->where('id', $ticket->site->id); - })->orWhere('role', Role::admin()->value) - ->orWhere('role', Role::developper()->value); - })->firstOrFail(); - $usersNotified->add($user); - SendTicketMentionMail::dispatchAfterResponse( - $user, - new TicketMention($ticket, $message->message) - ); - } catch (ModelNotFoundException $e) { - continue; - } - } - + $usersNotified = $ticket->sendBeenMentionnedToUser($message); event(new NotifySubscriptions( $message->ticket, trans('Nouvelle réponse de :name :
:message', [ diff --git a/app/Models/Ticket.php b/app/Models/Ticket.php index bca5c2f..314e7a9 100644 --- a/app/Models/Ticket.php +++ b/app/Models/Ticket.php @@ -2,13 +2,18 @@ namespace App\Models; +use App\Enums\Role; use App\Enums\TicketStatus; use App\Enums\TicketType; use App\Events\NotifySubscriptions; use App\Events\TicketStatusChanged; +use App\Jobs\SendTicketMentionMail; +use App\Mail\TicketMention; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Collection; use Kwaadpepper\Enum\Traits\CastsEnums; class Ticket extends Model @@ -83,4 +88,42 @@ class Ticket extends Model $file->move(sprintf('%s/%d', $siteAssetFullPath, $ticket->id), $fileName); return sprintf('%s/%d/%s', $fileAssetPath, $ticket->id, $fileName); } + + /** + * Parse message @user tags and send emails to concerned users + * returns and array with to users notified + * + * @param Message $message + * @return array[User] + */ + public function sendBeenMentionnedToUser(Message $message): Collection + { + $ticket = $this; + // find @user + send mail si un seul user est trouvé + $usersNotified = collect([]); + $matches = []; + $ticket->load('site'); + \preg_match_all('/@\w*/', $message->message, $matches); + $matches = collect($matches[0] ?? [])->unique(); + foreach ($matches as $pseudo_usertag) { + try { + $user = User::where('name', 'LIKE', '%' . ltrim($pseudo_usertag, '@') . '%') + ->where('email', '!=', $ticket->user->email) + ->where(function ($q) use ($ticket) { + $q->whereHas('sites', function ($q) use ($ticket) { + $q->where('id', $ticket->site->id); + })->orWhere('role', Role::admin()->value) + ->orWhere('role', Role::developper()->value); + })->firstOrFail(); + $usersNotified->add($user); + SendTicketMentionMail::dispatchAfterResponse( + $user, + new TicketMention($ticket, $message->message) + ); + } catch (ModelNotFoundException $e) { + continue; + } + } + return $usersNotified; + } } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 091d040..e554efd 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -22,15 +22,6 @@ class RouteServiceProvider extends ServiceProvider */ public const HOME = '/home'; - /** - * The controller namespace for the application. - * - * When present, controller route declarations will automatically be prefixed with this namespace. - * - * @var string|null - */ - // protected $namespace = 'App\\Http\\Controllers'; - /** * Define your route model bindings, pattern filters, etc. * diff --git a/config/app.php b/config/app.php index bba609a5..61aa02d 100644 --- a/config/app.php +++ b/config/app.php @@ -39,7 +39,7 @@ return [ | */ - 'debug' => (bool) env('APP_DEBUG', false), + 'debug' => (bool)env('APP_DEBUG', false), /* |-------------------------------------------------------------------------- @@ -171,7 +171,6 @@ return [ */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, - // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\TranslationServiceProvider::class diff --git a/config/auth.php b/config/auth.php index ba1a4d8..26e6ee0 100644 --- a/config/auth.php +++ b/config/auth.php @@ -69,12 +69,7 @@ return [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, - ], - - // 'users' => [ - // 'driver' => 'database', - // 'table' => 'users', - // ], + ] ], /* diff --git a/config/cache.php b/config/cache.php index 4f41fdf..9b4d7ab 100644 --- a/config/cache.php +++ b/config/cache.php @@ -60,9 +60,7 @@ return [ env('MEMCACHED_USERNAME'), env('MEMCACHED_PASSWORD'), ], - 'options' => [ - // Memcached::OPT_CONNECT_TIMEOUT => 2000, - ], + 'options' => [], 'servers' => [ [ 'host' => env('MEMCACHED_HOST', '127.0.0.1'), @@ -99,6 +97,6 @@ return [ | */ - 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'), + 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_cache'), ]; diff --git a/config/database.php b/config/database.php index b42d9b3..3bfc47a 100644 --- a/config/database.php +++ b/config/database.php @@ -123,7 +123,7 @@ return [ 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), - 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'), + 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_database_'), ], 'default' => [ diff --git a/config/filesystems.php b/config/filesystems.php index 94c8112..8257b8c 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -51,7 +51,7 @@ return [ 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), - 'url' => env('APP_URL').'/storage', + 'url' => env('APP_URL') . '/storage', 'visibility' => 'public', ], diff --git a/config/session.php b/config/session.php index 4e0f66c..c0e798b 100644 --- a/config/session.php +++ b/config/session.php @@ -128,7 +128,7 @@ return [ 'cookie' => env( 'SESSION_COOKIE', - Str::slug(env('APP_NAME', 'laravel'), '_').'_session' + Str::slug(env('APP_NAME', 'laravel'), '_') . '_session' ), /* diff --git a/config/trustedproxy.php b/config/trustedproxy.php index 8224727..fbc4604 100644 --- a/config/trustedproxy.php +++ b/config/trustedproxy.php @@ -15,20 +15,17 @@ return [ * of your proxy (e.g. if using ELB or similar). * */ - 'proxies' => '*', // [,], '*', ',' + 'proxies' => '*', /* * To trust one or more specific proxies that connect * directly to your server, use an array or a string separated by comma of IP addresses: */ - // 'proxies' => ['192.168.1.1'], - // 'proxies' => '192.168.1.1, 192.168.1.2', /* * Or, to trust all proxies that connect * directly to your server, use a "*" */ - // 'proxies' => '*', /* * Which headers to use to detect proxy related data (For, Host, Proto, Port) diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index bdea1a3..af316d5 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -8,6 +8,7 @@ use Illuminate\Support\Str; class UserFactory extends Factory { + /** * The name of the factory's corresponding model. * diff --git a/routes/channels.php b/routes/channels.php index 5d451e1..28b4539 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -14,5 +14,5 @@ use Illuminate\Support\Facades\Broadcast; */ Broadcast::channel('App.Models.User.{id}', function ($user, $id) { - return (int) $user->id === (int) $id; + return (int)$user->id === (int)$id; }); diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php index 547152f..ab92402 100644 --- a/tests/CreatesApplication.php +++ b/tests/CreatesApplication.php @@ -13,7 +13,7 @@ trait CreatesApplication */ public function createApplication() { - $app = require __DIR__.'/../bootstrap/app.php'; + $app = require __DIR__ . '/../bootstrap/app.php'; $app->make(Kernel::class)->bootstrap();