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