Completed
Push — master ( 2ab3b9...3652cb )
by Michael
02:59
created

LeagueStatisticsEloquent::netScoresByPlayerYear()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 41
Code Lines 24

Duplication

Lines 22
Ratio 53.66 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 41
rs 8.5806
cc 4
eloc 24
nc 1
nop 2
1
<?php namespace GolfLeague\Statistics\League;
2
3
use Illuminate\Support\Collection;
4
use \Round;
5
use \Player;
6
use \Skin;
7
use \Netwinner;
8
use \Holescore;
9
use \Match;
10
11
class LeagueStatisticsEloquent implements LeagueStatistics
12
{
13
    public function topFiveLowestScores()
14
    {
15
        return Round::with('player','course')->orderBy('score')->take(5)->get();
16
    }
17
    public function topFiveLowestScoresByYear($year)
18
    {
19
        $date1 = $year . '-01-01';
20
        $date2 = $year . '-12-31';
21
        return Round::with('player', 'course')
22
            ->where('match_id', '>', '0')
23
            ->where('date', '>=', $date1)
24
            ->where('date', '<=', $date2)
25
            ->orderBy('score')
26
            ->take(5)
27
            ->get();
28
    }
29
    public function topFiveScoringAverageByYear($year)
30
    {
31
        //Get players
32
        //For each player where match > 0 select scores and average them
33
        //Store in players array
34
        $date1 = $year . '-01-01';
35
        $date2 = $year . '-12-31';
36
        $players = Player::all();
37
        $average = array();
38
        $i = 0;
39
        foreach($players as $key => $player) {
40
            $rounds = Round::where('match_id', '>', '0')
41
                ->where('player_id', '=', $player->id)
42
                ->where('date', '>=', $date1)
43
                ->where('date', '<=', $date2)
44
                ->get();
45
            $scores = array();
46
			foreach($rounds as $round) {
47
				$scores[] = $round->score;
48
			}
49
            if(count($scores) > 0) {
50
                $average[$i]['average'] = round((array_sum($scores) / count($scores)) ,2);
51
                $average[$i]['player_id'] = $player->id;
52
                $average[$i]['name'] = $player->name;
53
                $average[$i]['rounds'] = count($scores);
54
                $i++;
55
            }
56
        }
57
        array_multisort($average);
58
        return $average;
59
    }
60
    public function topFiveNetScoresByYear($year)
61
    {
62
		$date1 = $year . '-01-01';
63
		$date2 = $year . '-12-31';
64
65
		return Netwinner::with('player','match','match.course')
66
				->where('created_at', '>=', $date1)
67
				->where('created_at', '<=', $date2)
68
				->orderBy('score')->take(5)->get();
69
    }
70
    public function mostSkinsByYear($year)
71
    {
72
        $year = $year . '-01-01';
73
        $players = Player::all();
74
        $i = 0;
75
        $skinsCount = array();
76
        foreach($players as $key => $player) {
77
            $skins = Skin::with('player','level')
78
                ->where('player_id', '=', $player->id)
79
                ->where('created_at', '>', $year)
80
                ->get();
81
            if(count($skins) > 0) {
82
                $skinsCount[$i]['skins'] = $skins->count();
83
                $skinsCount[$i]['name'] = $player->name;
84
                $i++;
85
            }
86
        }
87
        array_multisort($skinsCount, SORT_DESC);
88
        return $skinsCount;
89
    }
90
    public function totalEagles($year){}
91 View Code Duplication
    public function totalBirdies($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
92
	{
93
		$year = $year . '-01-01';
94
		$holescores = Holescore::with('round.player','hole')
95
			->where('created_at', '>', $year)
96
			->get();
97
		$allBirdies = array();
98
		foreach($holescores as $key => $holescore) {
99
			if($holescore['round']['match_id'] != null){
100
				if( ($holescore['hole']['par'] - $holescore['score']) === 1) {
101
						$allBirdies[]= $holescore['round']['player']['name'];
102
				}
103
			}
104
		}
105
		$i =0;
106
		$newArray = array_count_values($allBirdies);
107
			$birds = array();
108
			foreach ($newArray as $key => $value) {
109
				$birds[$i]['name'] = $key;
110
				$birds[$i]['birds'] =$value;
111
				$i++;
112
			}
113
			return $birds;
114
	}
115 View Code Duplication
    public function totalPars($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
116
	{
117
		$year = $year . '-01-01';
118
		$holescores = Holescore::with('round.player','hole')
119
			->where('created_at', '>', $year)
120
			->get();
121
		$allPars = array();
122
		foreach($holescores as $key => $holescore) {
123
			if($holescore['round']['match_id'] != null){
124
				if( ($holescore['hole']['par'] - $holescore['score']) === 0) {
125
						$allPars[]= $holescore['round']['player']['name'];
126
				}
127
			}
128
		}
129
		$i =0;
130
		$newArray = array_count_values($allPars);
131
			$pars = array();
132
			foreach ($newArray as $key => $value) {
133
				$pars[$i]['name'] = $key;
134
				$pars[$i]['pars'] =$value;
135
				$i++;
136
			}
137
			return $pars;
138
	}
139 View Code Duplication
    public function totalBogeys($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
140
	{
141
		$year = $year . '-01-01';
142
		$holescores = Holescore::with('round.player','hole')
143
			->where('created_at', '>', $year)
144
			->get();
145
		$allBogeys = array();
146
		foreach($holescores as $key => $holescore) {
147
			if($holescore['round']['match_id'] != null){
148
				if( ($holescore['score'] - $holescore['hole']['par']) === 1) {
149
						$allBogeys[]= $holescore['round']['player']['name'];
150
				}
151
			}
152
		}
153
		$i = 0;
154
		$newArray = array_count_values($allBogeys);
155
		$bogeys = array();
156
			foreach ($newArray as $key => $value) {
157
				$bogeys[$i]['name'] = $key;
158
				$bogeys[$i]['bogeys'] =$value;
159
				$i++;
160
			}
161
		return $bogeys;
162
	}
163 View Code Duplication
    public function totalDoubles($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
164
	{
165
		$year = $year . '-01-01';
166
		$holescores = Holescore::with('round.player','hole')
167
			->where('created_at', '>', $year)
168
			->get();
169
		$allDoubles = array();
170
		foreach($holescores as $key => $holescore) {
171
			if($holescore['round']['match_id'] != null){
172
				if( ($holescore['score'] - $holescore['hole']['par']) === 2) {
173
						$allDoubles[]= $holescore['round']['player']['name'];
174
				}
175
			}
176
		}
177
		$i = 0;
178
		$newArray = array_count_values($allDoubles);
179
			$doubles = array();
180
			foreach ($newArray as $key => $value) {
181
				$doubles[$i]['name'] = $key;
182
				$doubles[$i]['doubles'] =$value;
183
				$i++;
184
			}
185
		return $doubles;
186
	}
187 View Code Duplication
    public function totalOthers($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
188
	{
189
		$year = $year . '-01-01';
190
		$holescores = Holescore::with('round.player','hole')
191
			->where('created_at', '>', $year)
192
			->get();
193
		$allOthers = array();
194
		foreach($holescores as $key => $holescore) {
195
			if($holescore['round']['match_id'] != null){
196
				if( ($holescore['score'] - $holescore['hole']['par']) > 2) {
197
					$allOthers[]= $holescore['round']['player']['name'];
198
				}
199
			}
200
		}
201
		$i = 0;
202
		$newArray = array_count_values($allOthers);
203
		$others = array();
204
			foreach ($newArray as $key => $value) {
205
				$others[$i]['name'] = $key;
206
				$others[$i]['others'] =$value;
207
				$i++;
208
			}
209
		return $others;
210
	}
211
212
	public function netScoresByPlayer($playerId)
213
	{
214
		// Get players match scores
215
		$rounds = Round::with('player')
216
			->where('player_id', '=', $playerId)
217
			->where('date', '>', '2016-01-01')
218
			->whereNotNull('match_id')->get();
219
220
		//for each round get match handicap
221 View Code Duplication
		$rounds->map(function($round)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
222
		{
223
			$playerMatch = Player::find($round->player_id)->matches()->where('match_player.match_id','=', $round->match_id)->get();
224
			foreach($playerMatch as $player){
225
				$handicap = $player['pivot']['handicap'];
226
				$round->handicap = $handicap;
227
			}
228
		});
229
		//Calculate net score using match handicap
230 View Code Duplication
		$rounds->map(function($round)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
231
		{
232
			$net = ($round->score - round($round->handicap));
233
			return $round->netScore = ($net - $round->course->par);
234
		});
235
236
		// Sort by netScore
237 View Code Duplication
		$rounds->sort(function($a, $b)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
238
		{
239
			$a = $a->netScore;
240
			$b = $b->netScore;
241
			if ($a === $b) {
242
				return 0;
243
			}
244
			return ($a > $b) ? 1 : -1;
245
		});
246
247
		return $rounds;
248
249
	}
250
	public function netScoresByPlayerTop($playerId,$top)
251
	{
252
		$netScores = $this->netScoresByPlayer($playerId);
253
		return $netScores->take($top);
254
	}
255
	public function netScoresByPlayerYear($playerId, $year)
256
	{
257
		$date1 = $year . '-01-01';
258
		$date2 = $date1 -1;
259
		$date2 .= '-01-01';
260
		// Get players match scores
261
		$rounds = Round::with('player')
262
			->where('player_id', '=', $playerId)
263
			->where('date', '>', $date1)
264
			->where('date','<', $date2)
265
			->whereNotNull('match_id')->get();
266
267
		//for each round get match handicap
268 View Code Duplication
		$rounds->map(function($round)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
269
		{
270
			$playerMatch = Player::find($round->player_id)->matches()->where('match_player.match_id','=', $round->match_id)->get();
271
			foreach($playerMatch as $player){
272
				$handicap = $player['pivot']['handicap'];
273
				$round->handicap = $handicap;
274
			}
275
		});
276
		//Calculate net score using match handicap
277 View Code Duplication
		$rounds->map(function($round)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
278
		{
279
			$net = ($round->score - round($round->handicap));
280
			return $round->netScore = ($net - $round->course->par);
281
		});
282
283
		// Sort by netScore
284 View Code Duplication
		$rounds->sort(function($a, $b)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
285
		{
286
			$a = $a->netScore;
287
			$b = $b->netScore;
288
			if ($a === $b) {
289
				return 0;
290
			}
291
			return ($a > $b) ? 1 : -1;
292
		});
293
294
		return $rounds;
295
	}
296
	public function netScoresByPlayerTopYear($playerId,$top, $year)
297
	{
298
		$netScores = $this->netScoresByPlayerYear($playerId, $year);
299
		return $netScores->take($top);
300
	}
301
302 View Code Duplication
	public function netScoresLeague()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
303
	{
304
		//get players
305
		$players = Player::all();
306
307
		$netScores = new \Illuminate\Support\Collection();
308
		foreach($players as $player){
309
			$netScores->push($this->netScoresByPlayer($player->id));
310
		}
311
		return $netScores;
312
313
	}
314 View Code Duplication
	public function netScoresLeagueTop($number)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
315
	{
316
		//get players
317
		$players = Player::all();
318
319
		$netScores = new \Illuminate\Support\Collection();
320
		foreach($players as $player){
321
			$netScore = $this->netScoresByPlayerTop($player->id, $number);
322
			if(!$netScore->isEmpty()){
323
				$netScores->push($netScore);
324
			}
325
		}
326
327
		return $netScores;
328
	}
329 View Code Duplication
	public function netScoresLeagueYear($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
330
	{
331
		//get players
332
		$players = Player::all();
333
334
		$netScores = new \Illuminate\Support\Collection();
335
		foreach($players as $player){
336
			$netScores->push($this->netScoresByPlayerYear($player->id,$year));
337
		}
338
		return $netScores;
339
	}
340 View Code Duplication
	public function netScoresLeagueTopYear($top,$year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
341
	{
342
		//get players
343
		$players = Player::all();
344
345
		$netScores = new \Illuminate\Support\Collection();
346
		foreach($players as $player){
347
			$netScore = $this->netScoresByPlayerTopYear($player->id, $top, $year);
348
			if(!$netScore->isEmpty()){
349
				$netScores->push($netScore);
350
			}
351
		}
352
353
		return $netScores;
354
	}
355
356 View Code Duplication
	public function netCumulative()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
357
	{
358
		//get players
359
		$players = Player::all();
360
361
		$cumulativeNetScores = new \Illuminate\Support\Collection();
362
		foreach($players as $player){
363
			$cumulativeNetScores->push($this->netCumulativeByPlayer($player->id));
364
		}
365
		return $cumulativeNetScores;
366
	}
367 View Code Duplication
	public function netCumulativeTop($top)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
368
	{
369
		//get players
370
		$players = Player::all();
371
372
		$cumulativeNetScores = new \Illuminate\Support\Collection();
373
		foreach($players as $player){
374
			$netScore = $this->netCumulativeByPlayerTop($player->id, $top);
375
			if($netScore->isEmpty() == false){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
376
				$cumulativeNetScores->push($netScore);
377
			}
378
		}
379
380
		return $cumulativeNetScores;
381
	}
382 View Code Duplication
	public function netCumulativeByPlayer($playerId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
383
	{
384
		$netScores = $this->netScoresByPlayer($playerId);
385
		$cumulativeNet= new \Illuminate\Support\Collection();
386
387
		if($netScores->count() > 0){
388
			$netScore = $netScores->first();
389
390
			$totalNetScore = $netScores->sum('netScore');
391
			$cumulativeNet->put('player', $netScore->player->name);
392
			$cumulativeNet->put('score', $totalNetScore);
393
			$cumulativeNet->put('rounds', $netScores->count());
394
		}
395
396
		return $cumulativeNet;
397
	}
398 View Code Duplication
	public function netCumulativeByPlayerTop($playerId, $top)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
399
	{
400
		$netScores = $this->netScoresByPlayerTop($playerId, $top);
401
		$cumulativeNet= new \Illuminate\Support\Collection();
402
403
		if($netScores->count() > 0){
404
			$netScore = $netScores->first();
405
406
			$totalNetScore = $netScores->sum('netScore');
407
			$cumulativeNet->put('player', $netScore->player->name);
408
			$cumulativeNet->put('score', $totalNetScore);
409
			$cumulativeNet->put('rounds', $netScores->count());
410
		}
411
412
		return $cumulativeNet;
413
	}
414 View Code Duplication
	public function netCumulativeYear($year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
415
	{
416
		//get players
417
		$players = Player::all();
418
419
		$cumulativeNetScores = new \Illuminate\Support\Collection();
420
		foreach($players as $player){
421
			$cumulativeNetScores->push($this->netCumulativeByPlayerYear($player->id,$year));
422
		}
423
		return $cumulativeNetScores;
424
	}
425 View Code Duplication
	public function netCumulativeTopYear($top,$year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
426
	{
427
		//get players
428
		$players = Player::all();
429
430
		$cumulativeNetScores = new \Illuminate\Support\Collection();
431
		foreach($players as $player){
432
			$netScore = $this->netCumulativeByPlayerTopYear($player->id, $top, $year);
433
			if($netScore->isEmpty() == false){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
434
				$cumulativeNetScores->push($netScore);
435
			}
436
		}
437
438
		return $cumulativeNetScores;
439
	}
440 View Code Duplication
	public function netCumulativeByPlayerYear($playerId,$year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
441
	{
442
		$netScores = $this->netScoresByPlayerYear($playerId, $year);
443
		$cumulativeNet= new \Illuminate\Support\Collection();
444
445
		if($netScores->count() > 0){
446
			$netScore = $netScores->first();
447
448
			$totalNetScore = $netScores->sum('netScore');
449
			$cumulativeNet->put('player', $netScore->player->name);
450
			$cumulativeNet->put('score', $totalNetScore);
451
			$cumulativeNet->put('rounds', $netScores->count());
452
		}
453
454
		return $cumulativeNet;
455
	}
456 View Code Duplication
	public function netCumulativeByPlayerTopYear($playerId,$top,$year)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
457
	{
458
		$netScores = $this->netScoresByPlayerTopYear($playerId, $top, $year);
459
		$cumulativeNet= new \Illuminate\Support\Collection();
460
461
		if($netScores->count() > 0){
462
			$netScore = $netScores->first();
463
464
			$totalNetScore = $netScores->sum('netScore');
465
			$cumulativeNet->put('player', $netScore->player->name);
466
			$cumulativeNet->put('score', $totalNetScore);
467
			$cumulativeNet->put('rounds', $netScores->count());
468
		}
469
470
		return $cumulativeNet;
471
	}
472
473
474
}
475