Completed
Push — master ( d1869e...d8483a )
by Julien
41:12
created

CompetitorController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 132
ccs 40
cts 48
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 10
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);
0 ignored issues
show
Bug introduced by
The method find 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...
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'));
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()
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;
0 ignored issues
show
Bug introduced by
The property championshipId does not seem to exist in App\Http\Requests\CompetitorRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
64 2
        $championship = Championship::findOrFail($championshipId);
65
66 2
        foreach ($request->firstnames as $id => $firstname) {
0 ignored issues
show
Documentation introduced by
The property firstnames does not exist on object<App\Http\Requests\CompetitorRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67 2
            $email = $request->emails[$id] ?? Auth::user()->id . sha1(rand(1, 999999999999)) . (User::count() + 1) . "@kendozone.com";
0 ignored issues
show
Documentation introduced by
The property emails does not exist on object<App\Http\Requests\CompetitorRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
68 2
            $lastname = $request->lastnames[$id] ?? '';
0 ignored issues
show
Documentation introduced by
The property lastnames does not exist on object<App\Http\Requests\CompetitorRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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));
90
            }
91
        }
92 2
        flash()->success(trans('msg.user_registered_successful', ['tournament' => $tournament->name]));
0 ignored issues
show
Bug introduced by
The method success cannot be called on flash() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
93 2
        return redirect(URL::action('CompetitorController@index', $tournament->slug));
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.

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...
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;
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.

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...
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'));
150
    }
151
}
152