Issues (232)

app/Http/Controllers/ChampionshipController.php (7 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Competitor;
6
use App\Exceptions\InvitationExpiredException;
7
use App\Exceptions\InvitationNeededException;
8
use App\Exceptions\InvitationNotActiveException;
9
use App\Grade;
10
use App\Http\Requests\ChampionshipRequest;
11
use App\Invite;
12
use App\Notifications\RegisteredToChampionship;
13
use App\Tournament;
14
use App\User;
15
use Auth;
16
use Illuminate\Auth\Access\AuthorizationException;
17
use Illuminate\Support\Facades\URL;
18
use Illuminate\Support\Facades\View;
19
20
class ChampionshipController extends Controller
21
{
22
    /**
23
     * Register a User to a Tournament
24
     * Triggered when User click Activation Link received in mail
25
     *
26
     * @param $tournamentSlug
27
     * @param $token
28
     * @return View
29
     * @throws AuthorizationException
30
     * @throws InvitationExpiredException
31
     * @throws InvitationNeededException
32
     * @throws InvitationNotActiveException
33
     */
34 1
    public function create($tournamentSlug, $token) //TODO Should not put token as part as URL, but as param
35
    {
36 1
        $tournament = Tournament::where('slug', $tournamentSlug)->first();
37 1
        $grades = Grade::getAllPlucked();
38 1
        $invite = Invite::getInviteFromToken($token);
39
40 1
        if (is_null($invite)) throw new InvitationNeededException();
41
        if ($invite->hasExpired()) throw new InvitationExpiredException();
42
        if ($invite->active != 1) throw new InvitationNotActiveException();
43
44
        // Check if user is already registered
45
        $user = User::where('email', $invite->email)->first();
46
        if (is_null($user)) { // Redirect to user creation
47
            return view('auth/invite', compact('token'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('auth/invite', compact('token')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
48
        }
49
        // If user is not confirmed, auto confirm him
50
        if ($user->verified == 0) {
51
            $user->verified = 1;
52
            $user->save();
53
        }
54
        // Redirect to register category Screen
55
56
        Auth::loginUsingId($user->id);
57
        return view("categories.register", compact('tournament', 'invite', 'grades'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('categories....', 'invite', 'grades')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
58
    }
59
60
    /**
61
     * Store a new championship, when registering categories
62
     * Invoked on save() after create() is called
63
     *
64
     * @param Tournament $tournament
65
     * @param ChampionshipRequest $request
66
     * @return \Illuminate\Http\Response
67
     */
68 1
    public function store(Tournament $tournament, ChampionshipRequest $request)
69
    {
70 1
        $categories = $request->get('cat');
71
72 1
        if ($categories == null) {
73
            flash()->error(trans('msg.you_must_choose_at_least_one_championship'));
0 ignored issues
show
Are you sure the usage of flash() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
            return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
75
        }
76 1
        Auth::user()->updateUserFullName($request->firstname, $request->lastname);
77 1
        Auth::user()->championships()->detach();
78
79 1
        $shortId = Competitor::getShortId($categories, $tournament);
80
81 1
        foreach ($categories as $category) {
82 1
            Auth::user()->championships()->attach($category, ['confirmed' => 0, 'short_id' => $shortId]);
83
        }
84
85 1
        $tournament->owner->notify(new RegisteredToChampionship(Auth::user(), $tournament));
0 ignored issues
show
It seems like Auth::user() can also be of type null; however, parameter $user of App\Notifications\Regist...pionship::__construct() does only seem to accept App\User, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        $tournament->owner->notify(new RegisteredToChampionship(/** @scrutinizer ignore-type */ Auth::user(), $tournament));
Loading history...
86
87 1
        flash()->success(trans('msg.tx_for_register_tournament', ['tournament' => $tournament->name]));
0 ignored issues
show
Are you sure the usage of flash() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88 1
        return redirect(URL::action('UserController@getMyTournaments', Auth::user()));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(Illumina...aments', Auth::user())) returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
89
    }
90
}
91