Issues (232)

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

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Championship;
6
use App\FightersGroup;
7
use App\Grade;
8
use Illuminate\Auth\Access\AuthorizationException;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException;
12
13
class TreeController extends Controller
14
{
15
    /**
16
     * Display a listing of trees.
17
     *
18
     * @param Request $request
19
     * @return \Illuminate\Http\Response
20
     */
21
    public function index(Request $request)
22
    {
23
24
        $grades = Grade::getAllPlucked();
25
        $tournament = FightersGroup::getTournament($request);
26
        return view('trees.index', compact('tournament', 'grades'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('trees.index...tournament', 'grades')) returns the type Illuminate\Contracts\Vie...ry|Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
27
    }
28
29
    /**
30
     * Build Tree
31
     *
32
     * @param Request $request
33
     * @return \Illuminate\Http\Response|string
34
     * @throws AuthorizationException
35
     */
36
    public function store(Request $request)
37
    {
38
39
        $tournament = FightersGroup::getTournament($request); // Builder
40
41
        if (Auth::user()->cannot('store', [FightersGroup::class, $tournament])) {
42
            throw new AuthorizationException();
43
        }
44
        foreach ($tournament->championships as $championship) {
0 ignored issues
show
The property championships does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
45
            $generation = $championship->chooseGenerationStrategy();
46
47
            try {
48
                $generation->run();
49
                flash()->success(trans('msg.championships_tree_generation_success'));
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...
50
            } catch (TreeGenerationException $e) {
51
                flash()->error($e->message);
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...
52
            }
53
        }
54
55
        return redirect(route('tree.index', $tournament->slug))->with('activeTreeTab', $request->activeTreeTab);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('t...request->activeTreeTab) also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response|string.
Loading history...
The property slug does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
56
    }
57
58
    public function single(Request $request)
59
    {
60
        $championship = Championship::find($request->championship);
61
        $grades = Grade::getAllPlucked();
62
        return view('pdf.tree', compact('championship', 'grades'));
63
    }
64
65
    /**
66
     * @param Request $request
67
     * @return \Illuminate\Http\RedirectResponse
68
     */
69
    public function update(Request $request, $championshipId)
70
    {
71
        $championship = Championship::find($championshipId);
72
        $numFighter = 0;
73
        $query = FightersGroup::with('fights')
74
            ->where('championship_id', $championship->id);
75
76
        $fighters = $request->singleElimination_fighters;
77
        $scores = $request->score;
78
        if ($championship->hasPreliminary()) {
79
            $query = $query->where('round', '>', 1);
80
            $fighters = $request->preliminary_fighters;
81
        }
82
        $groups = $query->get();
83
84
        foreach ($groups as $group) {
85
            foreach ($group->fights as $fight) {
0 ignored issues
show
Bug Best Practice introduced by
The property fights does not exist on App\FightersGroup. Since you implemented __get, consider adding a @property annotation.
Loading history...
86
                // Find the fight in array, and update order
87
                $fight->c1 = $fighters[$numFighter];
88
                $scores[$numFighter] != null
89
                    ? $fight->winner_id = $fighters[$numFighter]
90
                    : $fight->winner_id = null;
91
                $numFighter++;
92
93
                $fight->c2 = $fighters[$numFighter];
94
                if ($fight->winner_id == null) {
95
                    $scores[$numFighter] != null
96
                        ? $fight->winner_id = $fighters[$numFighter]
97
                        : $fight->winner_id = null;
98
                }
99
100
                $numFighter++;
101
                $fight->save();
102
            }
103
        }
104
        return back();
105
    }
106
107
}
108