UserController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 66
dl 0
loc 118
rs 10
c 2
b 1
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A delete() 0 20 3
A show() 0 61 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers;
6
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Hash;
11
use Illuminate\Support\Facades\Route;
12
use Illuminate\Support\Str;
13
use Illuminate\View\View;
14
use Xetaravel\Models\Badge;
15
use Xetaravel\Models\User;
16
use Xetaravel\Utility\UserUtility;
17
18
class UserController extends Controller
19
{
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
27
        $action = Route::getFacadeRoot()->current()->getActionMethod();
28
29
        if ($action === 'show') {
30
            $this->breadcrumbs->addCrumb(
31
                '<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5.121 17.804A13.937 13.937 0 0112 16c2.5 0 4.847.655 6.879 1.804M15 10a3 3 0 11-6 0 3 3 0 016 0zm6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
32
                Profile',
33
                route('page.index')
34
            );
35
        }
36
    }
37
38
    /**
39
     * Show the user profile page.
40
     *
41
     * @param string $slug The slug of the user.
42
     *
43
     * @return RedirectResponse|View
44
     */
45
    public function show(string $slug)
46
    {
47
        $user = User::with('articles', 'comments')
48
            ->where('slug', Str::lower($slug))
49
            ->first();
50
51
        if (is_null($user)) {
52
            return redirect()
53
                ->route('page.index')
0 ignored issues
show
Bug introduced by
The method route() does not exist on Illuminate\Routing\Redirector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
                ->/** @scrutinizer ignore-call */ route('page.index')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
                ->error('This user does not exist or has been deleted !');
55
        }
56
        $articles = $user->articles()
57
            ->latest()
58
            ->take(config('xetaravel.pagination.user.articles_profile_page'))
59
            ->get();
60
61
        $comments = $user->comments()
62
            ->with('article')
63
            ->latest()
64
            ->take(config('xetaravel.pagination.user.comments_profile_page'))
65
            ->get();
66
67
        $discussPosts = $user->discussPosts()
68
            ->join('discuss_conversations', 'discuss_posts.conversation_id', '=', 'discuss_conversations.id')
69
            ->where('discuss_conversations.first_post_id', '!=', 'discuss_posts.id')
70
            ->select(
71
                'discuss_posts.*',
72
                'discuss_conversations.first_post_id AS conversation_first_post_id',
73
                'discuss_conversations.title AS conversation_title',
74
                'discuss_conversations.slug AS conversation_slug',
75
                'discuss_conversations.id AS conversation_id'
76
            )
77
            ->orderBy('discuss_posts.created_at', 'DESC')
78
            ->take(config('xetaravel.pagination.user.posts_profile_page'))
79
            ->get();
80
81
        $breadcrumbs = $this->breadcrumbs->addCrumb(
82
            e($user->username),
83
            $user->show_url
0 ignored issues
show
Bug introduced by
The property show_url does not seem to exist on Xetaravel\Models\User.
Loading history...
84
        );
85
86
        $badges = Badge::all();
87
        $userBadges = $user->badges->keyBy('id');
88
89
        $articles = collect($articles);
90
91
        $activities = $articles->merge($comments)->merge($discussPosts)->sortBy('created_at', SORT_NATURAL, true);
92
93
        $level = UserUtility::getLevel($user->experiences_total);
94
95
        if ($level['maxLevel'] === true) {
96
            $level['currentProgression'] = 100;
97
        } elseif ($level['matchExactXPLevel'] === true) {
98
            $level['currentProgression'] = 0;
99
        } else {
100
            $level['currentProgression'] = ($level['currentUserExperience'] / $level['nextLevelExperience']) * 100;
101
        }
102
103
        return view(
104
            'user.show',
105
            compact('user', 'activities', 'articles', 'comments', 'breadcrumbs', 'level', 'badges', 'userBadges')
106
        );
107
    }
108
109
    /**
110
     * Handle the delete request for the user.
111
     *
112
     * @param Request $request
113
     *
114
     * @return RedirectResponse
115
     */
116
    public function delete(Request $request): RedirectResponse
117
    {
118
        $user = Auth::user();
119
120
        if (!Hash::check($request->input('password'), $user->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
121
            return redirect()
122
                ->route('user.setting.index')
123
                ->error('Your Password does not match !');
124
        }
125
        Auth::logout();
126
127
        if ($user->delete()) {
128
            return redirect()
129
                ->route('page.index')
130
                ->success('Your account has been deleted successfully !');
131
        }
132
133
        return redirect()
134
            ->route('page.index')
135
            ->error('An error occurred while deleting your account !');
136
    }
137
}
138