Passed
Branch master (182172)
by Matthew
04:03 queued 01:23
created

ProPlayerRepository   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
dl 0
loc 158
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A SaveProPlayers() 0 14 3
D SearchPlayersManual() 0 58 10
A SearchPlayersByAssumedName() 0 16 2
A SearchPlayers() 0 16 2
A _saveProPlayer() 0 22 3
A __construct() 0 2 1
1
<?php
2
namespace PhpDraft\Domain\Repositories;
3
4
use Silex\Application;
5
use Symfony\Component\HttpFoundation\Request;
6
use PhpDraft\Domain\Entities\Draft;
7
use PhpDraft\Domain\Entities\ProPlayer;
8
9
class ProPlayerRepository {
10
  private $app;
11
12
  public function __construct(Application $app) {
13
    $this->app = $app;
14
  }
15
16
  /**
17
   * Search players for autocomplete feature on pro_players table
18
   * @param type $league Required - the league to search on
0 ignored issues
show
Bug introduced by
The type PhpDraft\Domain\Repositories\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
   * @param type $first Player first name search term
20
   * @param type $last Player last name search term
21
   * @param type $team Player team (abbreviation)
22
   * @param type $position Player position (abbreviation)
23
   * @return array
24
   */
25
  public function SearchPlayersManual($league, $first = "NA", $last = "NA", $team = "NA", $position = "NA") {
26
    //Approach taken from: http://stackoverflow.com/a/4540085/324527
27
28
    $searchSql = "SELECT * FROM pro_players WHERE league = :league";
29
30
    $searchParams = array();
31
    $regularParams = array();
32
33
    if ($first != "NA") {
34
      $searchParams['first_name'] = $first;
35
    }
36
37
    if ($last != "NA") {
38
      $searchParams['last_name'] = $last;
39
    }
40
41
    if ($team != "NA") {
42
      $regularParams['team'] = $team;
43
    }
44
45
    if ($position != "NA") {
46
      $regularParams['position'] = $position;
47
    }
48
49
    //Finish building PDO SQL with dynamic values:
50
    foreach ($searchParams as $key => $value) {
51
      $searchSql .= sprintf(' AND %s LIKE :%s', $key, $key);
52
    }
53
54
    foreach ($regularParams as $key => $value) {
55
      $searchSql .= sprintf(' AND %s = :%s', $key, $key);
56
    }
57
58
    //Limit the amount of searches to just 15 to cut down on unnecessary network traffic
59
    $searchSql .= ' LIMIT 15';
60
61
    $stmt = $this->app['db']->prepare($searchSql);
62
    $stmt->setFetchMode(\PDO::FETCH_CLASS, '\PhpDraft\Domain\Entities\ProPlayer');
63
    $stmt->bindValue(':league', $league);
64
65
    //Assign values to those parameters:
66
    foreach ($searchParams as $key => $value) {
67
      $stmt->bindValue(':' . $key, "%" . $value . "%");
68
    }
69
70
    foreach ($regularParams as $key => $value) {
71
      $stmt->bindValue(':' . $key, $value);
72
    }
73
74
    $stmt->execute();
75
76
    $foundPlayers = array();
77
78
    while ($newPlayer = $stmt->fetch()) {
79
      $foundPlayers[] = $newPlayer;
80
    }
81
82
    return array_slice($foundPlayers, 0, 15);
83
  }
84
85
  public function SearchPlayers($league, $searchTerm) {
86
    //Limit the amount of searches to just 15 to cut down on unnecessary network traffic
87
    $stmt = $this->app['db']->prepare("SELECT * FROM pro_players WHERE league = :league AND (first_name LIKE :search_term OR last_name LIKE :search_term) LIMIT 15");
88
    $stmt->setFetchMode(\PDO::FETCH_CLASS, 'PhpDraft\Domain\Entities\ProPlayer');
89
    $stmt->bindValue(':league', $league);
90
    $stmt->bindValue(':search_term', "%" . $searchTerm . "%");
91
92
    $stmt->execute();
93
94
    $foundPlayers = array();
95
96
    while ($newPlayer = $stmt->fetch()) {
97
      $foundPlayers[] = $newPlayer;
98
    }
99
100
    return $foundPlayers;
101
  }
102
103
  public function SearchPlayersByAssumedName($league, $firstName, $lastName) {
104
    $stmt = $this->app['db']->prepare("SELECT * FROM pro_players WHERE league = :league AND (first_name LIKE :first_name AND last_name LIKE :last_name) LIMIT 15");
105
    $stmt->setFetchMode(\PDO::FETCH_CLASS, 'PhpDraft\Domain\Entities\ProPlayer');
106
    $stmt->bindValue(':league', $league);
107
    $stmt->bindValue(':first_name', "%" . $firstName . "%");
108
    $stmt->bindValue(':last_name', "%" . $lastName . "%");
109
110
    $stmt->execute();
111
112
    $foundPlayers = array();
113
114
    while ($newPlayer = $stmt->fetch()) {
115
      $foundPlayers[] = $newPlayer;
116
    }
117
118
    return $foundPlayers;
119
  }
120
121
  /**
122
   * Delete existing players for a given league, upload new players
123
   * @param array $players Array of pro_player_object's
124
   */
125
  public function SaveProPlayers($league, $proPlayers) {
126
    $delete_sql = "DELETE FROM pro_players WHERE league = '" . $league . "'";
127
128
    $delete_success = $this->app['db']->exec($delete_sql);
129
130
    if ($delete_success === false) {
131
      throw new \Exception("Unable to empty existing pro players first.");
132
    }
133
134
    foreach ($proPlayers as $proPlayer) {
135
      $this->_saveProPlayer($proPlayer);
136
    }
137
138
    return;
139
  }
140
141
   /**
142
   * Adds a new pro player to the DB
143
   * @return boolean success whether or not the database operation succeeded.
144
   */
145
  private function _saveProPlayer(ProPlayer $proPlayer) {
146
    if ($proPlayer->pro_player_id > 0) {
147
      throw new \Exception("Unable to save pro player: invalid ID.");
148
    } else {
149
      $insertStmt = $this->app['db']->prepare("INSERT INTO pro_players 
150
        (pro_player_id, league, first_name, last_name, position, team) 
151
        VALUES 
152
        (NULL, ?, ?, ?, ?, ?)");
153
154
      $insertStmt->bindParam(1, $proPlayer->league);
155
      $insertStmt->bindParam(2, $proPlayer->first_name);
156
      $insertStmt->bindParam(3, $proPlayer->last_name);
157
      $insertStmt->bindParam(4, $proPlayer->position);
158
      $insertStmt->bindParam(5, $proPlayer->team);
159
160
      if (!$insertStmt->execute()) {
161
        throw new \Exception("Unable to save pro player.");
162
      }
163
164
      $proPlayer->draft_id = (int) $this->app['db']->lastInsertId();
0 ignored issues
show
Bug introduced by
The property draft_id does not seem to exist on PhpDraft\Domain\Entities\ProPlayer.
Loading history...
165
166
      return $proPlayer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $proPlayer returns the type PhpDraft\Domain\Entities\ProPlayer which is incompatible with the documented return type boolean.
Loading history...
167
    }
168
  }
169
}