CompetitorController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 130
ccs 40
cts 48
cp 0.8333
rs 10
wmc 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Championship;
6
use App\Competitor;
7
use App\Country;
8
use App\Grade;
9
use App\Http\Requests\CompetitorRequest;
10
use App\Invite;
11
use App\Notifications\InviteCompetitor;
12
use App\Tournament;
13
use App\User;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Auth;
16
use Illuminate\Support\Facades\View;
17
use Response;
18
use URL;
19
20
class CompetitorController extends Controller
21
{
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @param Tournament $tournament
26
     * @return View
27
     */
28 3
    public function index(Tournament $tournament)
29
    {
30 3
        $tournament = Tournament::with('championships.users', 'championships.teams', 'championships.category')->find($tournament->id);
31 3
        $settingSize = $tournament->championshipSettings()->count();
32 3
        $categorySize = $tournament->categories->count();
33 3
        $grades = Grade::getAllPlucked();
34 3
        $countries = Country::getAll();
35 3
        return view("tournaments.users", compact('tournament', 'settingSize', 'categorySize', 'grades', 'countries'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('tournaments...'grades', 'countries')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
36
37
    }
38
39
    /**
40
     * Show the form for creating a new competitor.
41
     *
42
     * @param Request $request
43
     * @param Tournament $tournament
44
     * @return View
45
     */
46
    public function create(Request $request, Tournament $tournament)
47
    {
48
        $championshipId = $request->get('categoryId');
49
50
        return view("tournaments/users/create", compact('tournament', 'championshipId')); //, compact()
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('tournaments...nt', 'championshipId')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
51
    }
52
53
    /**
54
     * Store a newly created resource in storage.
55
     *
56
     * @param CompetitorRequest $request
57
     * @param Tournament $tournament
58
     * @return \Illuminate\Http\Response
59
     */
60 2
    public function store(CompetitorRequest $request, Tournament $tournament)
61
    {
62
63 2
        $championshipId = $request->championshipId;
64 2
        $championship = Championship::findOrFail($championshipId);
65
66 2
        foreach ($request->firstnames as $id => $firstname) {
67 2
            $email = $request->emails[$id] ?? Auth::user()->id . sha1(rand(1, 999999999999)) . (User::count() + 1) . "@kendozone.com";
68 2
            $lastname = $request->lastnames[$id] ?? '';
69
70 2
            $user = Competitor::createUser([
71 2
                'firstname' => $firstname,
72 2
                'lastname' => $lastname,
73 2
                'name' => $firstname . " " . $lastname,
74 2
                'email' => $email
75
            ]);
76
77 2
            $championships = $user->championships();
78
            // If user has not registered yet this championship
79 2
            if (!$championships->get()->contains($championship)) {
80
                // Get Competitor Short ID
81 2
                $categories = $tournament->championships->pluck('id');
82 2
                $shortId = Competitor::getShortId($categories, $tournament);
83 2
                $championships->attach($championshipId, ['confirmed' => 0, 'short_id' => $shortId]);
84
            }
85
            //TODO Should add a test for this
86
            // We send him an email with detail (and user /password if new)
87 2
            if (strpos($email, '@kendozone.com') === -1) { // Substring is not present
88
                $code = resolve(Invite::class)->generateTournamentInvite($user->email, $tournament);
89 2
                $user->notify(new InviteCompetitor($user, $tournament, $code, $championship->category->name));
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on App\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
90
            }
91
        }
92 2
        flash()->success(trans('msg.user_registered_successful', ['tournament' => $tournament->name]));
0 ignored issues
show
Bug introduced by
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...
93 2
        return redirect(URL::action('CompetitorController@index', $tournament->slug));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(URL::act...x', $tournament->slug)) returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
94
95
96
    }
97
98
    /**
99
     * @param $tournamentSlug
100
     * @param $tcId
101
     * @param $userSlug
102
     * @return \Illuminate\Http\JsonResponse
103
     */
104 1
    public function confirmUser($tournamentSlug, $tcId, $userSlug)
0 ignored issues
show
Unused Code introduced by
The parameter $tournamentSlug is not used and could be removed. ( Ignorable by Annotation )

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

104
    public function confirmUser(/** @scrutinizer ignore-unused */ $tournamentSlug, $tcId, $userSlug)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106 1
        $user = User::where('slug', $userSlug)->first();
107 1
        $ctu = Competitor::where('championship_id', $tcId)
108 1
            ->where('user_id', $user->id)->first();
109
110 1
        $ctu->confirmed ? $ctu->confirmed = 0 : $ctu->confirmed = 1;
0 ignored issues
show
Bug Best Practice introduced by
The property confirmed does not exist on App\Competitor. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property confirmed does not exist on App\Competitor. Since you implemented __get, consider adding a @property annotation.
Loading history...
111 1
        if ($ctu->save()) {
112 1
            return Response::json(['msg' => trans('msg.user_status_successful'), 'status' => 'success']);
113
        } else {
114
            return Response::json(['msg' => trans('msg.user_status_error'), 'status' => 'error']);
115
        }
116
117
    }
118
119
    /**
120
     * @param $tournamentSlug
121
     * @param $tcId
122
     * @param $userSlug
123
     * @return \Illuminate\Http\JsonResponse
124
     */
125 2
    public function deleteUser($tournamentSlug, $tcId, $userSlug)
0 ignored issues
show
Unused Code introduced by
The parameter $tournamentSlug is not used and could be removed. ( Ignorable by Annotation )

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

125
    public function deleteUser(/** @scrutinizer ignore-unused */ $tournamentSlug, $tcId, $userSlug)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {
127
128 2
        $user = User::where('slug', $userSlug)->first();
129 2
        $ctu = Competitor::where('championship_id', $tcId)
130 2
            ->where('user_id', $user->id);
131
132 2
        if ($ctu->forceDelete()) {
133 2
            return Response::json(['msg' => trans('msg.user_delete_successful'), 'status' => 'success']);
134
        } else {
135
            return Response::json(['msg' => trans('msg.user_delete_error'), 'status' => 'error']);
136
        }
137
    }
138
139
140
    /**
141
     * Display the specified resource.
142
     *
143
     * @param Tournament $tournament
144
     * @param User $user
145
     * @return View
146
     */
147
    public function show(Tournament $tournament, User $user)
148
    {
149
        return view('users.show', compact('tournament', 'user'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('users.show'...('tournament', 'user')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Facades\View.
Loading history...
150
    }
151
}
152