Skip to content

Commit 262525b

Browse files
Nima8FTgomzyakov
andauthored
Replace auth()->user() with Dependency Injection (#474)
Co-authored-by: Alexander Gomzyakov <alexander.gomzyakov@gmail.com>
1 parent f8b5700 commit 262525b

File tree

8 files changed

+25
-18
lines changed

8 files changed

+25
-18
lines changed

app/Http/Controllers/Personal/Comment/IndexController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace App\Http\Controllers\Personal\Comment;
44

55
use App\Http\Controllers\Controller;
6+
use Illuminate\Contracts\Auth\Authenticatable;
67
use Illuminate\Contracts\View\Factory as ViewFactory;
78

89
class IndexController extends Controller
910
{
10-
public function __invoke(ViewFactory $view_factory)
11+
public function __invoke(ViewFactory $view_factory, Authenticatable $user)
1112
{
1213
/** @phpstan-ignore-next-line */
13-
$comments = auth()->user()->comments;
14+
$comments = $user->comments;
1415

1516
return $view_factory->make('personal.comment.index', ['comments' => $comments]);
1617
}

app/Http/Controllers/Personal/Liked/DeleteController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Models\Post;
7+
use Illuminate\Contracts\Auth\Authenticatable;
78

89
class DeleteController extends Controller
910
{
10-
public function __invoke(Post $post)
11+
public function __invoke(Post $post, Authenticatable $user)
1112
{
1213
/** @phpstan-ignore-next-line */
13-
auth()->user()->likedPosts()->detach($post->id);
14+
$user->likedPosts()->detach($post->id);
1415

1516
return redirect()->route('personal.liked.index');
1617
}

app/Http/Controllers/Personal/Liked/IndexController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace App\Http\Controllers\Personal\Liked;
44

55
use App\Http\Controllers\Controller;
6+
use Illuminate\Contracts\Auth\Authenticatable;
67
use Illuminate\Contracts\View\Factory as ViewFactory;
78

89
class IndexController extends Controller
910
{
10-
public function __invoke(ViewFactory $view_factory)
11+
public function __invoke(ViewFactory $view_factory, Authenticatable $user)
1112
{
1213
/** @phpstan-ignore-next-line */
13-
$posts = auth()->user()->likedPosts;
14+
$posts = $user->likedPosts;
1415

1516
return $view_factory->make('personal.liked.index', ['posts' => $posts]);
1617
}

app/Http/Controllers/Personal/Main/IndexController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
namespace App\Http\Controllers\Personal\Main;
44

55
use App\Http\Controllers\Controller;
6+
use Illuminate\Contracts\Auth\Authenticatable;
67
use Illuminate\Contracts\View\Factory as ViewFactory;
78

89
class IndexController extends Controller
910
{
10-
public function __invoke(ViewFactory $view_factory)
11+
public function __invoke(ViewFactory $view_factory, Authenticatable $user)
1112
{
1213
/** @phpstan-ignore-next-line */
13-
$data['countComments'] = count(auth()->user()->comments);
14+
$data['countComments'] = count($user->comments);
1415
/** @phpstan-ignore-next-line */
15-
$data['countLiked'] = count(auth()->user()->likedPosts);
16+
$data['countLiked'] = count($user->likedPosts);
1617

1718
return $view_factory->make('personal.main.index', ['data' => $data]);
1819
}

app/Http/Controllers/Post/Comment/StoreController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
use App\Http\Requests\Post\Comment\StoreRequest;
77
use App\Models\Comment;
88
use App\Models\Post;
9+
use Illuminate\Contracts\Auth\Authenticatable;
910

1011
class StoreController extends Controller
1112
{
12-
public function __invoke(StoreRequest $request, Post $post)
13+
public function __invoke(StoreRequest $request, Post $post, Authenticatable $user)
1314
{
1415
$data = $request->validated();
1516
$data['post_id'] = $post->id;
16-
/** @phpstan-ignore-next-line */
17-
$data['user_id'] = auth()->user()->id;
17+
/** @phpstan-ignore-next-line */
18+
$data['user_id'] = $user->id;
1819
/** @phpstan-ignore-next-line */
1920
Comment::create($data);
2021

app/Http/Controllers/Post/Like/StoreController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Models\Post;
7+
use Illuminate\Contracts\Auth\Authenticatable;
78

89
class StoreController extends Controller
910
{
10-
public function __invoke(Post $post)
11+
public function __invoke(Post $post, Authenticatable $user)
1112
{
1213
/** @phpstan-ignore-next-line */
13-
auth()->user()->likedPosts()->toggle($post->id);
14+
$user->likedPosts()->toggle($post->id);
1415

1516
return redirect()->back();
1617
}

app/Http/Middleware/AdminMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ class AdminMiddleware
1515
*/
1616
public function handle(Request $request, Closure $next): Response
1717
{
18-
/** @phpstan-ignore-next-line */
19-
if (auth()->user()->isReader()) {
18+
$user = $request->user();
19+
20+
if (! $user || ! $user->isAdministrator()) {
2021
abort(404);
2122
}
2223

routes/web.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
Route::prefix('post')->namespace('')->group(function () {
4949
Route::get('/{post}', [PostController::class, 'show'])->name('post.show');
5050

51-
Route::prefix('{post}/comments')->group(function () {
51+
Route::prefix('{post}/comments')->middleware('auth')->group(function () {
5252
Route::post('/', 'App\Http\Controllers\Post\Comment\StoreController')->name('post.comments.store');
5353
});
54-
Route::prefix('{post}/likes')->group(function () {
54+
Route::prefix('{post}/likes')->middleware('auth')->group(function () {
5555
Route::post('/', 'App\Http\Controllers\Post\Like\StoreController')->name('post.likes.store');
5656
});
5757
});

0 commit comments

Comments
 (0)