Completed
Push — master ( 50705e...9b1b9b )
by Julien
02:40
created

TreeController::deleteEverything()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Xoco70\KendoTournaments;
4
5
use Illuminate\Foundation\Auth\User;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Support\Facades\DB;
9
use Xoco70\KendoTournaments\Exceptions\TreeGenerationException;
10
use Xoco70\KendoTournaments\Models\Championship;
11
use Xoco70\KendoTournaments\Models\ChampionshipSettings;
12
use Xoco70\KendoTournaments\Models\Competitor;
13
use Xoco70\KendoTournaments\Models\FightersGroup;
14
use Xoco70\KendoTournaments\Models\Tournament;
15
16
class TreeController extends Controller
17
{
18
    /**
19
     * Display a listing of trees.
20
     *
21
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
22
     */
23
    public function index()
24
    {
25
        $tournament = Tournament::with(
0 ignored issues
show
Bug introduced by
The method first does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
26
            'competitors',
27
            'championshipSettings',
28
            'championships.settings',
29
            'championships.category')->first();
30
31
        return view('kendo-tournaments::tree.index')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
32
            ->with('tournament', $tournament)
33
            ->with('settings', $tournament->championships[0]->setting);
34
    }
35
36
    /**
37
     * Build Tree.
38
     *
39
     * @param Request $request
40
     * @param Championship $championship
41
     *
42
     * @return \Illuminate\Http\Response|string
43
     */
44
    public function store(Request $request, Championship $championship)
45
    {
46
        $this->deleteEverything();
47
        $numFighters = $request->numFighters;
48
49
        $users = factory(User::class, (int)$numFighters)->create();
50
51
        foreach ($users as $user) {
52
            factory(Competitor::class)->create(
53
                ['championship_id' => $championship->id,
54
                    'user_id' => $user->id,
55
                    'confirmed' => 1,
56
                    'short_id' => $user->id
57
                ]
58
            );
59
        }
60
61
        $championship->settings =  ChampionshipSettings::createOrUpdate($request, $championship);
62
        $generation = $championship->chooseGenerationStrategy();
63
64
        try {
65
            $generation->run();
66
            FightersGroup::generateFights($championship);
67
            // For Now, We don't generate fights when Preliminary
68
            if ($championship->isDirectEliminationType() && !$championship->hasPreliminary()) {
69
                FightersGroup::generateNextRoundsFights($championship);
70
            }
71
        } catch (TreeGenerationException $e) {
72
            redirect()->back()
73
                ->withErrors([$numFighters . "-" . $e->getMessage()]);
74
        }
75
        return redirect()->back()
76
            ->with('numFighters', $numFighters)
77
            ->with('hasPreliminary', $championship->settings->hasPreliminary)
78
            ->with(['success', "Success"]);
79
    }
80
81
    private function deleteEverything()
82
    {
83
        DB::table('fight')->delete();
84
        DB::table('fighters_groups')->delete();
85
        DB::table('fighters_group_competitor')->delete();
86
        DB::table('fighters_group_team')->delete();
87
        DB::table('competitor')->delete();
88
        DB::table('users')->where('id', '<>', 1)->delete();
89
    }
90
91
92
}
93