Completed
Push — master ( b13aa8...0a3a64 )
by Julien
11:57
created

TreeController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 1
cbo 7
dl 0
loc 94
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 1
B store() 0 24 2
A deleteEverything() 0 10 1
A provisionObjects() 0 21 3
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\Fight;
14
use Xoco70\KendoTournaments\Models\FightersGroup;
15
use Xoco70\KendoTournaments\Models\Team;
16
use Xoco70\KendoTournaments\Models\Tournament;
17
18
class TreeController extends Controller
19
{
20
    /**
21
     * Display a listing of trees.
22
     *
23
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
24
     */
25
    public function index()
26
    {
27
        $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...
28
            'competitors',
29
            'championshipSettings',
30
            'championships.settings',
31
            'championships.category')->first();
32
33
        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...
34
            ->with('tournament', $tournament)
35
            ->with('championship', $tournament->championships[0])
36
            ->with('settings', $tournament->championships[0]->setting);
37
    }
38
39
    /**
40
     * Build Tree.
41
     *
42
     * @param Request $request
43
     *
44
     * @return \Illuminate\Http\Response|string
45
     */
46
    public function store(Request $request, $championshipId)
0 ignored issues
show
Unused Code introduced by
The parameter $championshipId is not used and could be removed.

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

Loading history...
47
    {
48
        $this->deleteEverything();
49
        $numFighters = $request->numFighters;
50
        $isTeam = $request->isTeam ?? 0;
51
        $championship = $this->provisionObjects($request, $isTeam, $numFighters);
52
        $generation = $championship->chooseGenerationStrategy();
53
        try {
54
            $generation->run();
55
        } catch (TreeGenerationException $e) {
56
            redirect()->back()
57
                ->withErrors([$numFighters . "-" . $e->getMessage()]);
58
        }
59
60
        $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...
61
            'competitors',
62
            'championshipSettings',
63
            'championships.settings',
64
            'championships.category')->first();
65
66
        return view('kendo-tournaments::tree.index',
67
            compact('tournament', 'championship', 'numFighters', 'isTeam'));
68
69
    }
70
71
    private function deleteEverything()
72
    {
73
        DB::table('fight')->delete();
74
        DB::table('fighters_groups')->delete();
75
        DB::table('fighters_group_competitor')->delete();
76
        DB::table('fighters_group_team')->delete();
77
        DB::table('competitor')->delete();
78
        DB::table('team')->delete();
79
        DB::table('users')->where('id', '<>', 1)->delete();
80
    }
81
82
    /**
83
     * @param Request $request
84
     * @param $isTeam
85
     * @param $numFighters
86
     * @return mixed
87
     */
88
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
89
    {
90
        if ($isTeam) {
91
            $championship = Championship::find(2);
92
            factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
93
        } else {
94
            $championship = Championship::find(1);
95
            $users = factory(User::class, (int)$numFighters)->create();
96
            foreach ($users as $user) {
97
                factory(Competitor::class)->create(
98
                    ['championship_id' => $championship->id,
99
                        'user_id' => $user->id,
100
                        'confirmed' => 1,
101
                        'short_id' => $user->id
102
                    ]
103
                );
104
            }
105
        }
106
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
107
        return $championship;
108
    }
109
110
111
}
112