1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace App\Lib\DsManager\Models\Orm;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Class Player
|
7
|
|
|
* @package App\Lib\DsManager\Models\Orm
|
8
|
|
|
*/
|
9
|
|
|
class Player extends DsManagerOrm
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
*
|
13
|
|
|
*/
|
14
|
|
|
const PLAYER_STATS_LIMIT = 5;
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/**
|
18
|
|
|
* @var string
|
19
|
|
|
*/
|
20
|
|
|
protected $table = 'players';
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* @var array
|
24
|
|
|
*/
|
25
|
|
|
protected $fillable = [
|
26
|
|
|
'name',
|
27
|
|
|
'surname',
|
28
|
|
|
'age',
|
29
|
|
|
'nationality',
|
30
|
|
|
'skillAvg',
|
31
|
|
|
'wageReq',
|
32
|
|
|
'val',
|
33
|
|
|
'role',
|
34
|
|
|
'team_id'
|
35
|
|
|
];
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
39
|
|
|
*/
|
40
|
|
|
public function team()
|
41
|
|
|
{
|
42
|
|
|
return $this->belongsTo(Team::class);
|
43
|
|
|
}
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* @return mixed
|
47
|
|
|
*/
|
48
|
|
|
public function lastMatches()
|
49
|
|
|
{
|
50
|
|
|
return $this->hasMany(MatchPlayer::class)
|
51
|
|
|
->orderBy('updated_at', 'DESC')
|
52
|
|
|
->limit(5);
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @return mixed
|
57
|
|
|
*/
|
58
|
|
|
public function goals()
|
59
|
|
|
{
|
60
|
|
|
return $this->hasOne(MatchPlayer::class)
|
61
|
|
|
->selectRaw('player_id, sum(goals) as count')
|
62
|
|
|
->groupBy('player_id');
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* @return mixed
|
67
|
|
|
*/
|
68
|
|
|
public function appearances()
|
69
|
|
|
{
|
70
|
|
|
return $this->hasOne(MatchPlayer::class)
|
71
|
|
|
->selectRaw('player_id, count(match_id) as count')
|
72
|
|
|
->groupBy('player_id');
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* @return mixed
|
77
|
|
|
*/
|
78
|
|
|
public function avg()
|
79
|
|
|
{
|
80
|
|
|
return $this->hasOne(MatchPlayer::class)
|
81
|
|
|
->selectRaw('player_id, round(avg(vote),2) as avg')
|
82
|
|
|
->groupBy('player_id');
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* @param $query
|
87
|
|
|
* @return mixed
|
88
|
|
|
*/
|
89
|
|
|
public function scopeStatistics($query)
|
90
|
|
|
{
|
91
|
|
|
return $query->with(
|
92
|
|
|
'goals',
|
93
|
|
|
'appearances',
|
94
|
|
|
'avg',
|
95
|
|
|
'lastMatches',
|
96
|
|
|
'team'
|
97
|
|
|
);
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
/**
|
101
|
|
|
* @return array
|
102
|
|
|
*/
|
103
|
|
View Code Duplication |
public static function getBest()
|
|
|
|
|
104
|
|
|
{
|
105
|
|
|
$result = MatchPlayer::selectRaw(
|
106
|
|
|
'player_id, COUNT(*) as appearances , AVG(vote) avg, SUM(goals) goals'
|
107
|
|
|
)->where('goals', '>', 0)
|
108
|
|
|
->orderByRaw('SUM(goals) DESC,COUNT(*) DESC')
|
109
|
|
|
->groupBy('player_id')->take(self::PLAYER_STATS_LIMIT)->get()->keyBy('player_id')->toArray();
|
110
|
|
|
$players = Player::with('team')->whereIn('id', array_keys($result))->get()->toArray();
|
111
|
|
|
$result = array_map(function ($player) use ($result) {
|
112
|
|
|
return array_merge($player,$result[$player['id']]);
|
113
|
|
|
}, $players);
|
114
|
|
|
return $result;
|
115
|
|
|
}
|
116
|
|
|
} |
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.