|
1
|
|
|
<?php namespace GolfLeague\Statistics\Course; |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use \Round; |
|
5
|
|
|
|
|
6
|
|
|
class CourseStatisticsEloquent implements CourseStatistics |
|
7
|
|
|
{ |
|
8
|
|
|
private $date1 = '-01-01'; |
|
9
|
|
|
private $date2 = '-12-31'; |
|
10
|
|
|
|
|
11
|
|
View Code Duplication |
public function averageScore($courseId) |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
$today = Carbon::today()->toDateString(); |
|
14
|
|
|
$rounds = Round::where('course_id', '=', $courseId) |
|
15
|
|
|
->where('date', '<', $today)->get(); |
|
16
|
|
|
$scores = $rounds->map(function($round) |
|
17
|
|
|
{ |
|
18
|
|
|
return $round->score; |
|
19
|
|
|
}); |
|
20
|
|
|
if($rounds->count() > 0) { |
|
21
|
|
|
return round(array_sum($scores->toArray())/($rounds->count()), 2); |
|
22
|
|
|
} else { |
|
23
|
|
|
return $rounds; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
View Code Duplication |
public function averageScoreByPlayer($courseId, $playerId) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$today = Carbon::today()->toDateString(); |
|
29
|
|
|
$rounds = Round::where('course_id', '=', $courseId) |
|
30
|
|
|
->where('date', '<', $today) |
|
31
|
|
|
->where('player_id', '=', $playerId) |
|
32
|
|
|
->get(); |
|
33
|
|
|
$scores = $rounds->map(function($round) |
|
34
|
|
|
{ |
|
35
|
|
|
return $round->score; |
|
36
|
|
|
}); |
|
37
|
|
|
if($rounds->count() > 0) { |
|
38
|
|
|
return round(array_sum($scores->toArray())/($rounds->count()), 2); |
|
39
|
|
|
} else { |
|
40
|
|
|
return $rounds; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
View Code Duplication |
public function averageScoreByYear($courseId, $year) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$rounds = Round::where('course_id', '=', $courseId) |
|
46
|
|
|
->where('date', '>=', $year . $this->date1) |
|
47
|
|
|
->where('date', '<=', $year . $this->date2) |
|
48
|
|
|
->get(); |
|
49
|
|
|
$scores = $rounds->map(function($round) |
|
50
|
|
|
{ |
|
51
|
|
|
return $round->score; |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
if($rounds->count() > 0) { |
|
55
|
|
|
return round(array_sum($scores->toArray())/($rounds->count()), 2); |
|
56
|
|
|
} else { |
|
57
|
|
|
return $rounds; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
View Code Duplication |
public function averageScoreByPlayerAndYear($courseId, $playerId, $year) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$rounds = Round::where('course_id', '=', $courseId) |
|
63
|
|
|
->where('player_id', '=', $playerId) |
|
64
|
|
|
->where('date', '>=', $year . $this->date1) |
|
65
|
|
|
->where('date', '<=', $year . $this->date2) |
|
66
|
|
|
->get(); |
|
67
|
|
|
$scores = $rounds->map(function($round) |
|
68
|
|
|
{ |
|
69
|
|
|
return $round->score; |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
if($rounds->count() > 0) { |
|
73
|
|
|
return round(array_sum($scores->toArray())/($rounds->count()), 2); |
|
74
|
|
|
} else { |
|
75
|
|
|
return $rounds; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
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.