1
|
|
|
<?php
|
2
|
|
|
namespace App\Lib\DsManager\Models\Orm;
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Class Team
|
7
|
|
|
* @package App\Lib\DsManager\Models\Orm
|
8
|
|
|
*/
|
9
|
|
|
class Team extends DsManagerOrm
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
*
|
13
|
|
|
*/
|
14
|
|
|
const PLAYED_LIMIT = 5;
|
15
|
|
|
/**
|
16
|
|
|
*
|
17
|
|
|
*/
|
18
|
|
|
const FUTURE_LIMIT = 3;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
*
|
22
|
|
|
*/
|
23
|
|
|
const TEAM_STATS_LIMIT = 5;
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* @var string
|
27
|
|
|
*/
|
28
|
|
|
protected $table = 'teams';
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* @var array
|
32
|
|
|
*/
|
33
|
|
|
protected $fillable = [
|
34
|
|
|
'name',
|
35
|
|
|
'nationality'
|
36
|
|
|
];
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
40
|
|
|
*/
|
41
|
|
|
public function roster()
|
42
|
|
|
{
|
43
|
|
|
return $this->hasMany(Player::class);
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
48
|
|
|
*/
|
49
|
|
|
public function coach()
|
50
|
|
|
{
|
51
|
|
|
return $this->hasOne(Coach::class);
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* @return mixed
|
56
|
|
|
*/
|
57
|
|
View Code Duplication |
public function playedMatchesHome()
|
|
|
|
|
58
|
|
|
{
|
59
|
|
|
return $this->hasMany(
|
60
|
|
|
MatchResult::class,
|
61
|
|
|
'home_team_id'
|
62
|
|
|
)->where('simulated', true)
|
63
|
|
|
->orderBy('updated_at', 'DESC')
|
64
|
|
|
->limit(self::PLAYED_LIMIT);
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
/**
|
68
|
|
|
* @return mixed
|
69
|
|
|
*/
|
70
|
|
View Code Duplication |
public function futureMatchesHome()
|
|
|
|
|
71
|
|
|
{
|
72
|
|
|
return $this->hasMany(
|
73
|
|
|
MatchResult::class,
|
74
|
|
|
'home_team_id'
|
75
|
|
|
)->where('simulated', false)
|
76
|
|
|
->orderBy('updated_at', 'DESC')
|
77
|
|
|
->limit(self::FUTURE_LIMIT);
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* @return mixed
|
82
|
|
|
*/
|
83
|
|
View Code Duplication |
public function playedMatchesAway()
|
|
|
|
|
84
|
|
|
{
|
85
|
|
|
return $this->hasMany(
|
86
|
|
|
MatchResult::class,
|
87
|
|
|
'away_team_id'
|
88
|
|
|
)->where('simulated', true)
|
89
|
|
|
->orderBy('updated_at', 'DESC')
|
90
|
|
|
->limit(self::PLAYED_LIMIT);
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* @return mixed
|
95
|
|
|
*/
|
96
|
|
View Code Duplication |
public function futureMatchesAway()
|
|
|
|
|
97
|
|
|
{
|
98
|
|
|
return $this->hasMany(
|
99
|
|
|
MatchResult::class,
|
100
|
|
|
'away_team_id'
|
101
|
|
|
)->where('simulated', false)
|
102
|
|
|
->orderBy('updated_at', 'DESC')
|
103
|
|
|
->limit(self::FUTURE_LIMIT);
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/**
|
108
|
|
|
* @param $query
|
109
|
|
|
* @return mixed
|
110
|
|
|
*/
|
111
|
|
|
public function scopeComplete($query)
|
112
|
|
|
{
|
113
|
|
|
return $query->with(
|
114
|
|
|
'roster',
|
115
|
|
|
'coach',
|
116
|
|
|
'playedMatchesHome',
|
117
|
|
|
'playedMatchesHome.awayTeam',
|
118
|
|
|
'futureMatchesHome',
|
119
|
|
|
'futureMatchesHome.awayTeam',
|
120
|
|
|
'playedMatchesAway',
|
121
|
|
|
'playedMatchesAway.homeTeam',
|
122
|
|
|
'futureMatchesAway',
|
123
|
|
|
'futureMatchesAway.homeTeam'
|
124
|
|
|
);
|
125
|
|
|
}
|
126
|
|
|
|
127
|
|
|
/**
|
128
|
|
|
* @return array
|
129
|
|
|
*/
|
130
|
|
View Code Duplication |
public static function getBest()
|
|
|
|
|
131
|
|
|
{
|
132
|
|
|
$result = Match::selectRaw('winner_id, COUNT(*) as won')
|
133
|
|
|
->whereNotNull('winner_id')->where('winner_id', '!=', 0)
|
134
|
|
|
->orderByRaw('COUNT(*) DESC')->groupBy('winner_id')
|
135
|
|
|
->take(self::TEAM_STATS_LIMIT)->get()->keyBy('winner_id')->toArray();
|
136
|
|
|
$teams = Team::whereIn('id', array_keys($result))->get()->toArray();
|
137
|
|
|
$result = array_map(function ($team) use ($result) {
|
138
|
|
|
return array_merge($team, $result[$team['id']]);
|
139
|
|
|
|
140
|
|
|
}, $teams);
|
141
|
|
|
|
142
|
|
|
return $result;
|
143
|
|
|
}
|
144
|
|
|
|
145
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.