MatchUpdate::rules()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Requests;
4
5
use App\Events\MatchWasFinished;
6
use App\Listeners\Match\UpdateResultType;
7
use App\Match;
8
use App\Tournament;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\Facades\Validator;
12
13
/**
14
 * Class MatchUpdate
15
 * @package App\Http\Requests
16
 */
17
class MatchUpdate extends Request
18
{
19
    /**
20
     * Determine if the user is authorized to make this request.
21
     *
22
     * @return bool
23
     */
24
    public function authorize()
25
    {
26
        return Auth::check();
27
    }
28
29
    /**
30
     * Get the validation rules that apply to the request.
31
     *
32
     * @return array
33
     */
34
    public function rules()
35
    {
36
        $matchId = $this->route('matchId');
37
38
        /**
39
         * Copy of match row
40
         *
41
         * @var $match Match
42
         */
43
        $match = Match::findOrFail($matchId)->replicate();
44
        $tournament = $match->tournament()->get()->first();
45
46
        Validator::extend('round_active', function ($attribute, $value, $parameters) use ($match, $tournament) {
0 ignored issues
show
Unused Code introduced by
The parameter $attribute 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...
Unused Code introduced by
The parameter $value 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...
Unused Code introduced by
The parameter $parameters 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
            return $this->isRoundActive($match, $tournament);
48
        });
49
50
        Validator::extend(
51
            'round_finished_for_pair',
52
            function ($attribute, $value, $parameters) use ($match, $tournament) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters 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...
53
                $match->status = $value;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<App\Match>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
54
                return $this->isRoundFinishedForPair($match, $tournament);
55
            }
56
        );
57
58
        $statuses = implode(',', Match::getAvailableStatuses());
59
60
        return [
61
            'match.homeScore' => 'required|integer',
62
            'match.awayScore' => 'required|integer',
63
            'match.status' => 'required|in:' . $statuses . '|round_active|round_finished_for_pair'
64
        ];
65
    }
66
67
    /**
68
     * @param $match
69
     * @param $tournament
70
     * @return bool
71
     */
72
    protected function isRoundActive($match, $tournament)
73
    {
74
75
        // rule is not applied for Leagues
76
        if (Tournament::TYPE_LEAGUE === $tournament->type) {
77
            return true;
78
        }
79
80
        // rule is not applied for group stage matches
81
        if (Match::GAME_TYPE_GROUP_STAGE === $match->gameType) {
82
            return true;
83
        }
84
85
        return $match->round === $tournament->getCurrentRound();
86
    }
87
88
    /**
89
     * @param $match
90
     * @param $tournament Tournament
91
     * @return bool
92
     */
93
    protected function isRoundFinishedForPair($match, $tournament)
94
    {
95
        // rule is not applied for Leagues
96
        if (Tournament::TYPE_LEAGUE === $tournament->type) {
97
            return true;
98
        }
99
100
        // rule is not applied for group stage matches
101
        if (Match::GAME_TYPE_GROUP_STAGE === $match->gameType) {
102
            return true;
103
        }
104
105
        $match->fill(array_get($this->only(['match.homeScore', 'match.awayScore', 'match.status']), 'match'));
106
107
        $event = new MatchWasFinished($match);
108
109
        $listener = new UpdateResultType();
110
        $listener->handle($event);
111
112
        $secondMatchForPair = Match::where([
113
            'round' => $match->round,
114
            'homeTournamentTeamId' => $match->awayTournamentTeamId,
115
            'awayTournamentTeamId' => $match->homeTournamentTeamId
116
        ])->first();
117
118
        $matches = new Collection([$event->match, $secondMatchForPair]);
119
120
        // all matches in pair should have `finished` status
121
        if ($matches->where('status', Match::STATUS_FINISHED)->count() < 2) {
122
            return true;
123
        }
124
125
        $score = $tournament->getScore($matches);
126
127
        return 2 === $score->unique('position')->count();
128
    }
129
}
130