EloquentMatchRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 129
rs 10
c 8
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A __construct() 0 5 1
A getEditable() 0 4 1
A getEditableMatches() 0 5 1
A getYears() 0 10 1
A find() 0 4 1
B create() 0 25 2
B update() 0 37 2
A all() 0 4 1
A getByDate() 0 5 1
A delete() 0 4 1
1
<?php namespace GolfLeague\Storage\Match;
2
3
use Carbon\Carbon;
4
use \Match as Match;
5
use \Round as Round;
6
use \Player as Player;
7
8
class EloquentMatchRepository implements MatchRepository
9
{
10
  /*Return Score collections that include:
11
   * Player Name
12
   * Date
13
   * Total
14
   * Course
15
   * Multideminsional Array of Hole Numbers and scores   *
16
   **/
17
	public function __construct(Match $match, Player $player)
18
    {
19
        $this->match = $match;
0 ignored issues
show
Bug introduced by
The property match does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
		$this->player = $player;
0 ignored issues
show
Bug introduced by
The property player does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    public function all()
24
    {
25
		return Match::orderBy('date', 'DESC')->get();
26
    }
27
28
	public function get($matchid)
29
	{
30
		 return Match::with('course', 'players', 'course.holes')->find($matchid);
31
	}
32
33
	public function getEditable($matchid)
34
	{
35
		return Match::with('course', 'playersEdit', 'course.holes')->find($matchid);
36
	}
37
38
	public function getEditableMatches()
39
	{
40
		$today = Carbon::today();
41
		return Match::with('course', 'players')->where('date', '>=', $today)->get();
42
	}
43
44
45
	public function getByDate($startDate, $endDate)
46
	{
47
		 //dd($startDate);
48
		 return Match::with('course')->where('date', '>=', $startDate)->where('date', '<=', $endDate)->get();
49
	}
50
51
	public function getYears()
52
	{
53
		$dates =  Match::orderBy('date', 'DESC')->get(['date']);
54
		//return $dates;
55
		$years = $dates->map(function($years){
56
			return substr($years->date, 0, 4);
57
		})->toArray();
58
59
		return array_unique($years);
60
	}
61
    //Find Scores by Player Id
62
    public function find($playerId)
63
    {
64
        //return Round::with('player', 'holescores')->where('player_id', '=', $playerId)->get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
    }
66
67
    public function create($matchdata)
68
    {
69
		//enter match data to Matches table
70
		$this->match->date = $matchdata['date'];
71
		$this->match->course_id = $matchdata['course'];
72
		$this->match->purse = $matchdata['purse'];
73
		$this->match->skinsamoney = $matchdata['skinsamoney'];
74
		$this->match->skinsbmoney = $matchdata['skinsbmoney'];
75
		$this->match->grossmoney = $matchdata['grossmoney'];
76
		$this->match->netmoney = $matchdata['netmoney'];
77
78
		$this->match->save(); //save to match table
79
80
		foreach ($matchdata['player'] as $key => $player){
81
			$currentPlayer = $this->player->find($player['player_id']);
82
			$attributes = array(
83
				"level_id" => $player['level_id'],
84
				"group" => $player['group'],
85
				"handicap" => $player['handicap'],
86
				"winnings" => $player['winnings'],
87
			);
88
			$currentPlayer->matches()->save($this->match, $attributes); //save match_player pivot data
89
		}// End foreach
90
		return $this->match->id;
91
    }
92
93
	public function update($matchdata)
94
	{
95
		//update course
96
		$currentMatch = $this->match->find($matchdata['match']);
97
		$currentMatch->course_id = $matchdata['course']; //update course
98
		$currentMatch->update();
99
100
		//Remove players from match_players
101
		$currentMatch->players()->detach();
102
103
		//Remove rounds
104
		Round::where('match_id', '=', $currentMatch->id)->delete();
105
106
		//Add players to  match_players
107
		foreach ($matchdata['player'] as $key => $player) {
108
			$currentPlayer = $this->player->find($player['player_id']);
109
			$attributes = array(
110
				"level_id" => $player['level_id'],
111
				"group" => $player['group'],
112
				"handicap" => $currentPlayer->handicap,
113
				"winnings" => 0,
114
			);
115
			$currentPlayer->matches()->attach($currentMatch, $attributes); //save match_player pivot data
116
117
			$newRound = array(
118
				'date' => $currentMatch->date,
119
				"player_id" => $player['player_id'],
120
				"course_id" => $player['group'],
121
				"match_id" => $currentMatch->course,
122
				"score" => 0,
123
				"esc" => 0
124
			);
125
			//Create New round for player
126
			Round::create($newRound);
127
128
		}// End foreach
129
	}
130
131
	public function delete($matchId)
132
	{
133
		return 'deleted';
134
	}
135
136
}
137