Completed
Push — master ( 698ab0...d7a406 )
by Michael
05:43
created

EloquentMatchRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace GolfLeague\Storage\Match;
2
3
use Carbon\Carbon;
4
use \Match as Match;
5
use \Player as Player;
6
7
class EloquentMatchRepository implements MatchRepository
8
{
9
  /*Return Score collections that include:
10
   * Player Name
11
   * Date
12
   * Total
13
   * Course
14
   * Multideminsional Array of Hole Numbers and scores   *
15
   **/
16
	public function __construct(Match $match, Player $player)
17
    {
18
        $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...
19
		$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...
20
    }
21
22
    public function all()
23
    {
24
		return Match::orderBy('date', 'DESC')->get();
25
    }
26
27
	public function get($matchid)
28
	{
29
		 return Match::with('course', 'players', 'course.holes')->find($matchid);
30
	}
31
32
	public function getEditable($matchid)
33
	{
34
		return Match::with('course', 'playersEdit', 'course.holes')->find($matchid);
35
	}
36
37
	public function getEditableMatches()
38
	{
39
		$today = Carbon::now();
40
		return Match::with('course', 'players')->where('date', '>=', $today)->get();
41
	}
42
43
44
	public function getByDate($startDate, $endDate)
45
	{
46
		 //dd($startDate);
47
		 return Match::with('course')->where('date', '>=', $startDate)->where('date', '<=', $endDate)->get();
48
	}
49
50
	public function getYears()
51
	{
52
		$dates =  Match::orderBy('date', 'DESC')->get(['date']);
53
		//return $dates;
54
		$years = $dates->map(function($years){
55
			return substr($years->date, 0, 4);
56
		})->toArray();
57
58
		return array_unique($years);
59
	}
60
    //Find Scores by Player Id
61
    public function find($playerId)
62
    {
63
        //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...
64
    }
65
66
    public function create($matchdata)
67
    {
68
		//enter match data to Matches table
69
		$this->match->date = $matchdata['date'];
70
		$this->match->course_id = $matchdata['course'];
71
		$this->match->purse = $matchdata['purse'];
72
		$this->match->skinsamoney = $matchdata['skinsamoney'];
73
		$this->match->skinsbmoney = $matchdata['skinsbmoney'];
74
		$this->match->grossmoney = $matchdata['grossmoney'];
75
		$this->match->netmoney = $matchdata['netmoney'];
76
77
		$this->match->save(); //save to match table
78
79
		foreach ($matchdata['player'] as $key => $player){
80
			$currentPlayer = $this->player->find($player['player_id']);
81
			$attributes = array(
82
				"level_id" => $player['level_id'],
83
				"group" => $player['group'],
84
				"handicap" => $player['handicap'],
85
				"winnings" => $player['winnings'],
86
			);
87
			$currentPlayer->matches()->save($this->match, $attributes); //save match_player pivot data
88
		}// End foreach
89
		return $this->match->id;
90
    }
91
92
	public function update($matchdata)
93
	{
94
		//return $matchdata;
95
		foreach ($matchdata['player'] as $player) {
96
			// update match_player pivot tables
97
			Match::find($matchdata['match'])
98
				->players()
99
				->updateExistingPivot(
100
					$player['player_id'],
101
					[
102
						'level_id' => $player['level_id'],
103
						'group' => $player['group']
104
					]
105
				);
106
			//delete rounds for the match
107
			Round::where('match_id', '=', $matchdata['match'])
108
				->where('player_id', '=', $player['player_id']);
109
		}
110
111
	}
112
113
	public function delete($matchId)
114
	{
115
		return 'deleted';
116
	}
117
118
}
119