Passed
Push — 5.0.0 ( f10804...44b42e )
by Fèvre
15:04 queued 07:40
created

UserController::createPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
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\Repositories\UserRepository;
16
use Xetaravel\Models\User;
17
use Xetaravel\Models\Validators\UserValidator;
18
use Xetaravel\Utility\UserUtility;
19
20
class UserController extends Controller
21
{
22
    /**
23
     * Constructor
24
     */
25
    public function __construct()
26
    {
27
        parent::__construct();
28
29
        $action = Route::getFacadeRoot()->current()->getActionMethod();
30
31
        if ($action === 'show') {
32
            $this->breadcrumbs->addCrumb('<i class="fa-regular fa-id-card mr-2"></i> Profile', route('page.index'));
33
        }
34
    }
35
36
    /**
37
     * Show the user profile page.
38
     *
39
     * @param string $slug The slug of the user.
40
     *
41
     * @return RedirectResponse|View
42
     */
43
    public function show(string $slug)
44
    {
45
        $user = User::with('articles', 'comments')
46
            ->where('slug', Str::lower($slug))
47
            ->first();
48
49
        if (is_null($user)) {
50
            return redirect()
51
                ->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

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