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

DepthChartPositionRepository::Save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
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\Pick;
8
use PhpDraft\Domain\Models\DepthChartPositionCreateModel;
9
10
class DepthChartPositionRepository {
11
  private $app;
12
13
  public function __construct(Application $app) {
14
    $this->app = $app;
15
  }
16
17
  public function LoadAll($draft_id) {
18
    $positions = array();
19
    
20
    $stmt = $this->app['db']->prepare("SELECT d.* ".
21
            "FROM depth_chart_positions d " .
22
            "WHERE d.draft_id = ? " .
23
            "ORDER BY d.display_order");
24
    
25
    $stmt->bindParam(1, $draft_id, \PDO::PARAM_INT);
26
    
27
    $stmt->setFetchMode(\PDO::FETCH_CLASS, '\PhpDraft\Domain\Entities\DepthChartPosition');
28
    
29
    if(!$stmt->execute()) {
30
      throw new \Exception("Unable to load updated positions.");
31
    }
32
    
33
    while($position = $stmt->fetch()) {
34
      $positions[] = $position;
35
    }
36
    
37
    return $positions;
38
  }
39
40
  public function Save(DepthChartPositionCreateModel $depthChartPositionCreateModel, $draft_id) {
41
    $insertPositionStmt = $this->app['db']->prepare("INSERT INTO depth_chart_positions 
42
      (draft_id, position, slots, display_order)
43
      VALUES
44
      (:draft_id, :position, :slots, :display_order)");
45
46
    $newPositions = array();
47
48
    foreach ($depthChartPositionCreateModel->positions as $position) {
49
      $insertPositionStmt->bindValue(":draft_id", $draft_id);
50
      $insertPositionStmt->bindValue(":position", $position->position);
51
      $insertPositionStmt->bindValue(":slots", $position->slots);
52
      $insertPositionStmt->bindValue(":display_order", $position->display_order);
53
54
      if (!$insertPositionStmt->execute()) {
55
        throw new \Exception("Unable to create depth chart positions for $position->draft_id");
56
      }
57
58
      $position->round_time_id = (int)$this->app['db']->lastInsertId();
59
      $newPositions[] = $position;
60
    }
61
62
    return $newPositions;
63
  }
64
65
  public function DeleteAllDepthChartPositions($draft_id) {
66
    $delete_stmt = $this->app['db']->prepare("DELETE FROM depth_chart_positions WHERE draft_id = ?");
67
    $delete_stmt->bindParam(1, $draft_id);
68
69
    if(!$delete_stmt->execute()) {
70
      throw new \Exception("Unable to delete existing depth chart positions.");
71
    }
72
  }
73
}