UserController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Http\Controllers\Web;
4
5
use App\Entities\User;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\User\EditRequest;
8
use App\Http\Requests\User\PasswordRequest;
9
use App\Repositories\Criteria\OrderBy;
10
use App\Repositories\Criteria\Where;
11
use App\Repositories\UserRepository;
12
use App\Services\User\UserSolutions;
13
use App\Services\UserService;
14
use Illuminate\Support\MessageBag;
15
16
class UserController extends Controller
17
{
18
    private $repository;
19
20
    /**
21
     * UserController constructor.
22
     *
23
     * @param UserRepository $repository
24
     */
25
    public function __construct(UserRepository $repository)
26
    {
27
        $this->repository = $repository;
28
    }
29
30
    public function index()
31
    {
32
        $per_page = 100;
33
        $offset = (request('page', 1) - 1) * $per_page + 1;
34
35
        $this->repository->pushCriteria(new Where('status', User::ST_ACTIVE));
36
        $this->repository->pushCriteria(new OrderBy('solved', 'desc'));
37
        $this->repository->pushCriteria(new OrderBy('submit', 'asc'));
38
39
        $users = $this->repository->paginate($per_page);
40
41
        return view('web.user.index', ['users' => $users, 'offset' => $offset]);
42
    }
43
44
    public function show($username)
45
    {
46
        /** @var User $user */
47
        $user = app(UserService::class)->findByName($username);
48
        if (! $user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
49
            // user may not exist
50
            return redirect(route('home'))->withErrors('user is not exist!');
51
        }
52
53
        if (! $user->isActive()) {
54
            return back()->withErrors('User is not found!');
55
        }
56
57
        $problems = app(UserSolutions::class)->getResolvedProblems($user);
58
59
        return view('web.user.view')->with('user', $user)
60
            ->with('problems', $problems);
61
    }
62
63
    public function profile()
64
    {
65
        $user = auth()->user();
66
67
        if ($user) {
68
            return view('web.user.edit')->with('user', $user);
69
        }
70
71
        return redirect(route('home'));
72
    }
73
74
    public function edit(EditRequest $request)
75
    {
76
        /** @var User $user */
77
        $user = auth()->user();
78
79
        if ($user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
80
            $user->fill($request->all());
81
            $emailChanged = $user->isDirty('email');
82
            if ($emailChanged) {
83
                $user->email_verified_at = null;
84
            }
85
            $user->save();
86
            if ($emailChanged) {
87
                $user->sendEmailVerificationNotification();
88
89
                return redirect()->back()->with('warning', __('Email has been modified, should verify again'));
90
            }
91
92
            return redirect()->back()->with('success', __('Profile Update Success!'));
93
        }
94
95
        return redirect(route('home'));
96
    }
97
98
    public function editPassword()
99
    {
100
        $user = auth()->user();
101
        if ($user) {
102
            return view('web.user.password')->with('user', $user);
103
        }
104
105
        return redirect(route('home'));
106
    }
107
108
    public function password(PasswordRequest $request)
109
    {
110
        /** @var User $user */
111
        $user = $request->user();
112
113
        if (app('hash')->make($request->input('password')) === $user->password) {
114
            $user->password = app('hash')->make($request->input('password_new'));
115
            $user->save();
116
117
            return redirect()->back()->with('success', trans('user.message.password_change_success'));
118
        }
119
        $errors = new MessageBag(['password' => trans('user.message.password_not_match')]);
120
121
        return redirect()->back()->withErrors($errors);
122
    }
123
}
124